Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » U++ Library support » U++ Core » [SOLVED] Why long long int seems to be 32 bit longer?
[SOLVED] Why long long int seems to be 32 bit longer? [message #52144] Mon, 29 July 2019 08:19 Go to next message
forlano is currently offline  forlano
Messages: 1185
Registered: March 2006
Location: Italy
Senior Contributor
Hello,

perhaps this is not U++ related but it depends by some compiler flag I am not aware of (MSVC 2017).
According to wikipedia https://en.wikipedia.org/wiki/C_data_types

long long int

should be 64 bit longer. Now I want to set the bits of such number.
Here is a simple code that set the kth bit in a long long int

#include <Core/Core.h>
using namespace Upp;
#include <climits>

// set the kth bit 
long long int setKthBit(int n, int k) 
{ 
    return ((1 << k) | n); 
} 
 
int getKthBit(int n, int k)
{
  return (n & ( 1 << k )) >> k;
}

CONSOLE_APP_MAIN
{ unsigned long long  num = 0, n;
  int k = 30;
  n = setKthBit(num, k);
  printf("%llu \n", n);
  Cout()<<getKthBit(n,k);
  //Cout() << "unsigned long long max = " << ULLONG_MAX ;
}


What I observe is that for k>30 the code does not work. It seems it cannot use a proper 64 bit number.
what am I missing?

Thanks,
Luigi

[Updated on: Tue, 30 July 2019 12:12]

Report message to a moderator

Re: Why long long int seems to be 32 bit longer? [message #52145 is a reply to message #52144] Mon, 29 July 2019 09:59 Go to previous messageGo to next message
Tom1
Messages: 1212
Registered: March 2007
Senior Contributor
Hi Luigi,

It seems you have your function parameter 'n' defined as int instead of long long int.

Best regards,

Tom
Re: Why long long int seems to be 32 bit longer? [message #52148 is a reply to message #52145] Mon, 29 July 2019 10:17 Go to previous messageGo to next message
koldo is currently offline  koldo
Messages: 3356
Registered: August 2008
Senior Veteran
Hi Luigi

Yes. I would declare n like this:
long long int setKthBit(unsigned long long n, int k) 
int getKthBit(unsigned long long n, int k)

In addition, maybe it should be used
1ULL << k

instead of
1 << k


Best regards
Iñaki

[Updated on: Mon, 29 July 2019 10:18]

Report message to a moderator

Re: Why long long int seems to be 32 bit longer? [message #52152 is a reply to message #52148] Mon, 29 July 2019 20:24 Go to previous message
forlano is currently offline  forlano
Messages: 1185
Registered: March 2006
Location: Italy
Senior Contributor
Hello Tom and Iñaki,

thanks a lot for fixing my code. Only three terrible bugs in less than 10 row, almost a record! Smile
Now it works perfectly.

I hope to be able to save the long long int in a xml file and read it without loss of bits.

Best regards,
Luigi

PS: corrected code

#include <Core/Core.h>
using namespace Upp;

// set the kth bit 
unsigned long long int setKthBit(unsigned long long int n, int k) 
{ 
    return ((1ULL << k) | n); 
} 
 
int getKthBit(unsigned long long int n, int k)
{
  return (n & ( 1ULL << k )) >> k;
}

CONSOLE_APP_MAIN
{ unsigned long long int num = 0, n;
  int k = 60;
  n = setKthBit(num, k);
  printf("%llu \n", n);
  
  Cout()<<getKthBit(n,k); 
}

[Updated on: Mon, 29 July 2019 20:35]

Report message to a moderator

Previous Topic: Problem with LanguageInfo
Next Topic: Implementation of Vector::Add()
Goto Forum:
  


Current Time: Fri Apr 19 04:18:48 CEST 2024

Total time taken to generate the page: 0.02187 seconds