开发者

Bit Shifting with a Count greater than 30

I am confused on the Bit Shift Operator开发者_运维知识库s (in c#).

Can you shed light on why the value of "a" below returns "1" instead of "4294967296":

ulong a = 1 << 32

I'm running a 32bit machine. Is that the reason why? i.e. If I were to run the same exact code on a 64bit machine, would the value "a" be 4294967296? I have high doubts that the 32bit vs 64bit architecture has anything to do with my question, but I had to ask anyway.

Regardless, is there a way to shift bits in such a way that the result of the twiddling will result in 32 bit (or greater) value?

Example of what I'm hoping to accomplish:

long a = 1 << 31; // I want the value 2147483648 not the value -2147483648
long b = 1 << 32; // I want the value 4294967296 not the value 1


The 1 is treated as an int, being shifted to the left, and the whole result (which is an int) then assigned to your ulong a.

Try this:

    ulong a = 1 << 32;
    Console.WriteLine(a);
    a = (ulong)1 << 32;
    Console.WriteLine(a);

You will get the correct answer when you cast the constant 1 to a ulong.

1

4294967296

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜