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 |
|
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 #52152 is a reply to message #52148] |
Mon, 29 July 2019 20:24 |
|
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!
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
|
|
|
Goto Forum:
Current Time: Fri Apr 19 04:18:48 CEST 2024
Total time taken to generate the page: 0.02187 seconds
|