开发者

Unsigned long and bit shifting

I have a problem with bit shifting and unsigned longs. Here's my test code:

char header[4];
header[0] = 0x80;
header[1] = 0x00;
header[2] = 0x00;
header[3] = 0x00;

unsigned long l1 = 0x80000000UL;
unsigned long l2 = ((unsigned long) header[0] << 24) + ((unsigned long) header[1] << 16) + ((unsigned long) header[2] << 8) + (unsigned long) header[3];

cout << l1 << endl;
cout << l2 << endl;

I would expect l2 to also have a value of 2147483648 but instead it prints 18446744071562067968. I assume the bit shifting of the first byte causes problems?

Hopefully somebody can explain why this fails and how开发者_如何学Go I modify the calculation of l2 so that it returns the correct value.

Thanks in advance.


Your value of 0x80 stored in a char is a signed quantity. When you cast this into a wider type, the value is being signed extended to keep the same value as a larger type.

Change the type of char in the first line to unsigned char and you will not get the sign extension happening.

To simplify what is happening in your case, run this:

char c = 0x80
unsigned long l = c
cout << l << endl;

You get this output:

18446744073709551488

which is -128 as a 64-bit integer (0x80 is -128 as a 8-bit integer).


Same result here (Linux/x86-64, GCC 4.4.5). The behavior depends on the size of unsigned long, which is at least 32 bits, but may be larger.

If you want exactly 32 bits, use a uint32_t instead (from the header <stdint.h>; not in C++03 but in the upcoming standard and widely supported).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜