Type size

Hello,

I have really simple question that comes from wierd compiler behaviour.

A variable of type unsigned long long is 64 bit length, but if I store a value bigger than 32 bit compiler reports a warning and then code doesn't work. Here an example:

1
2
3
4
5
6
7
8
9
10
#include <iostream>

using namespace std;

int main (int argc, char *argv[])
{
        cout << "sizeof(unsigned long long) " << sizeof(unsigned long long) << endl;
        cout << "sizeof(1 << 34) "  << sizeof((1 << 34)) << endl;
        return 0;
}



Compiler says:
test.cpp: In function 'int main(int, char**)':
test.cpp:8: warning: left shift count >= width of type


And here the result when running the program:

sizeof(unsigned long long) 8
sizeof(1 << 34) 4

Why is shift operation if just 32 bit long?

Thank your for your help ;)
The sizeof() operator works on static information. We could argue that the compiler should be smarter, but what you are testing is the same as saying sizeof(1), which for the native machine size for an int is four bytes, or 32 bits.

Make sure you start with the proper size object:
1LL << 34

Hope this helps.
Can i know why you require the size of the shift variable ?
There is no "shift variable" -- and he isn't interested in it.

He is interested in the value created by shifting 1 to the left 34 times, which is 0x400000000, or 17179869184.

Such a value does not fit into a 32-bit variable. He presumed that after the shift it would, but bit shifting doesn't generally work like that.

You need to start with the proper size variable in order to have that many bits to shift through.

Hope this helps.
Topic archived. No new replies allowed.