bit shifting with unsigned long type produces wron

2019-01-04 13:58发布

I'm a bit confused because I wanted to initialize a variable of type unsigned long whose size is 8 bytes on my system (on every modern system I suppose). When I want to assign 1 << 63 to the variable, I get a compiler warning however and the number is in fact 0. When I do 1 << 30 I get the expected result of 2 ^ 30 = 1073741824. Yet when I do 1 << 31, I get the result of 2 ^ 64 (I think; actually this shouldn't be possible) which prints 18446744071562067968.

Can anyone explain this behaviour to me?

4条回答
beautiful°
2楼-- · 2019-01-04 14:13

1 << 63 will be computed in int arithmetic, and your int is probably 32 bit.

Remedy this by promoting one of the arguments: 1ULL << 63 will do it.

ULL means the expression will be at least 64 bits.

查看更多
老娘就宠你
3楼-- · 2019-01-04 14:26

The 1 here, is called an integer constant. By the norms specified in the standard, C11, chapter §6.4.4.1 the syntax for the same is

integer-constant:
     decimal-constant integer-suffixopt
     octal-constant integer-suffixopt
      hexadecimal-constant integer-suffixopt

and regarding the Semantics,

The type of an integer constant is the first of the corresponding list in which its value can be represented.

and the table says, if there is no suffix, and the value is representable in the int range it should be considered as int. So, 1 here, is considered an int, which is of generally 4 bytes, or 32 bits, also same in your case.

To explicitly specify the 1 as unsigned long (64) bit, we can use the suffix, like

1UL << 63

should solve your issue.

Please note: unsigned long is not guaranteed to be of 64 bits. unsigned long long is guaranteed to have at least 64 bits. However, as long as you're using the platform where unsigned long is of 64 bits , you should be fine

查看更多
时光不老,我们不散
4楼-- · 2019-01-04 14:33

I recommend that you use 1ULL since this will give you a 64-bit unsigned integer on 32 and 64 bit architecture. On 32 Bit architecture unsigned long (and therefor UL) is only 32 bit long and will not solve the problem.

1ULL << 63
查看更多
狗以群分
5楼-- · 2019-01-04 14:34

The expression 1 << 63 has type int. The range of an int is -231 … 231 - 1 on most systems, 263 is too large for that. Try either (unsigned long)1 << 63 or 1UL << 63 to shift a value of type unsigned long left by 63 places.

查看更多
登录 后发表回答