二进制操作long vs int

时间:2018-05-13 19:39:52

标签: c# binary bit-manipulation

我正在进行DES实现,我遇到了二进制操作的问题。

我无法弄清楚为什么有32位掩码(0xFFFFFFFFL)以及为什么在删除它之后它会给出其他值。

int leftHalf = (int)(input >> 28); 
int rightHalf = (int)(input & 0x0FFFFFFF);

long connectedHalves = ((leftHalf & 0xFFFFFFFFL) << 28) | (rightHalf & 0xFFFFFFFFL);
long notMasked = (leftHalf << 28) | rightHalf;

例如:

int leftHalf = 1048560
int rightHalf = 109436546

leftHalfBinary =              11111111111111110000
rightHalfBinary =      110100001011101111010000010

leftHalfMasked =  00000000000011111111111111110000
rightHalfMasked = 00000110100001011101111010000010

LeftHalfShifted =                   11111111111111110000 0000000000000000000000000000
LeftHalfMaskedShifted = 00000000000011111111111111110000 0000000000000000000000000000

看起来给出相同的结果,但是对于这个例子:

connectedHalves = 281470791179906
notMasked = 109436546

为什么呢?

代码:

https://github.com/poltak/DataEncryptionStandard/blob/master/src/jpsam3hklam9/des/RoundKeyGenerator.java

方法:long [] generateRoundKeys(长输入)

1 个答案:

答案 0 :(得分:0)

如果是int,则溢出为&lt;&lt; 28。