Bitwise Shift - Getting different results in C#.ne

2019-07-31 15:13发布

问题:

When I run this command in PHP, I get:

Code: 2269495617392648 >> 24
Result: 32

When I run it in C#.net or vb.net, I get:

Code: 2269495617392648 >> 24
Result: 135272480

PHP is correct.

The interesting thing is, that when I try to shift any number greater than int32 in .net, it yields bad results..

Every number under int32 (2147483647) yields the same results from php and c#.net or vb.net

Is there a workaround for this in .net?

回答1:

Strictly speaking, PHP is wrong.

The entire bit pattern of the number 2269495617392648 is:

1000 0001 0000 0001 1000 0010 0000 0001 1000 0001 0000 0000 1000 (2269495617392648)

Right shifting this 24 times gets you:

0000 0000 0000 0000 0000 0000 1000 0001 0000 0001 1000 0010 0000 (135272480)

This is the bit pattern for 135272480, not 32.

What's going on in PHP apparently is that the number 2269495617392648 is being truncated to 538447880 by perserving only the lower 32 bits. Note that the number 2269495617392648 is much too big to fit in a 32-bit integer, signed or unsigned.

Right shifting the truncated bits 24 times gives us 32.

Before truncation to 32-bits:
1000 0001 0000 0001 1000 0010 0000 0001 1000 0001 0000 0000 1000 (2269495617392648)

After truncation to 32-bits:
0010 0000 0001 1000 0001 0000 0000 1000 (538447880)

Right shifting the truncated bits by 24 bits:
0000 0000 0000 0000 0000 0000 0010 0000 (32)

You have alluded to this problem when you said:

The interesting thing is, that when I try to shift any number greater then int32 in .net it yields bad results..

It gives you bad results because some bits are being chopped off in order to fit in 32 bits.

If you're porting from PHP to C# and need to preserve this behavior, you need to manually truncate the bits by using 2269495617392648 & 0xffffffff instead of just 2269495617392648 (see jcomeau_ictx's answer). But be aware that there is an issue of bit truncation going on in your PHP code. I'm not certain if it's intentional or not.



回答2:

bitwise-AND your number with 0xffffffff before shifting.

in Python:


>>> 2269495617392648 >> 24
135272480L
>>> (2269495617392648 & 0xffffffff) >> 24
32L

I rarely use .net, but fairly sure the syntax will be very similar.