0x, shift left, shift right

0x代表的是十六进制,表示的是补码,默认为int类型,即32位,不足高位补0。
如:0xff=0x000000ff,每一位代表一个十六进制,
-2表示的补码为1111 1111 1111 1111 1111 1111 1111 1110。十六进制为 0xfffffffe

Java's >> is signed and shifted. A signed right shift means that after the right shift, the sign bit on the left is supplemented, the positive number is supplemented with 0, and the negative number is supplemented with 1

>>> is unsigned and shifted. Unsigned right shift means that after all the digits are shifted to the right, no matter the number is positive or negative, 0 is added to the left after the right shift.

<< is a left shift. There is no distinction between signed and unsigned. After the left shift, 0 is added to the right, and the leftmost sign bit is also moved directly.

 

When converting byte to int, you need to perform an & operation with 0xff first. This is because an int is 4 bytes, and 4 bytes respectively constitute the 4 bits of the int. This requires 4 bytes for the | operation. When converting to int, in addition to its own Positions other than the position should be 0 so as not to affect other bits. For example, byte is -1 because the 8 bytes extracted from int are 11111111 (complement code). This only represents the value of this bit, except for the highest value. The other bits should be positive numbers, so the automatically complemented bits should be 0, and only the highest bit represents the sign. If you directly force-convert int, 1 will be added to the high 24 bits, but when we assemble it into int, we don’t need its other bits. You can use or add when assembling, because there is a 1 somewhere else. It must be 0 so 0+1 is the same as 0|1.

For example: byte b[]={1,1,-1,1}. This byte array will perform the following operations when forming an int

0000 0001 0000 0000 0000 0000 0000 0000

|

0000 0000 0000 0001 0000 0000 0000 0000

|

1111 1111 1111 1111 1111 1111 0000 0000

|

0000 0000 0000 0000 0000 0000 0000 0001

This will become

1111 1111 1111 1111 1111 1111 0000 0001 which is -255 and the actual complement should be 0000 0001 0000 0001 1111 1111 0000 0001. That is 16842497

Therefore, you should clear all 1's outside your own position, that is, combine it with 0xff, remove the high bits, and then perform the shift operation.

Note: When the system detects that a negative byte may be converted into an int or that a byte is operated with an int type, it will add 1 to the high bit of the byte memory space (that is, fill in the sign bit) to 32 bits, and then participate. Operation.

 

 

Guess you like

Origin blog.csdn.net/cjc000/article/details/89947117