"Thinking in Java" bit computing

Bitwise Operators:

First thing to remember, easy to understand: if the corresponding positive and negative corresponds to 10.

1. AND (&): 11 to give to give 0,00 1,10 0 too.

2. or (|): 11 was obtained 1,00 1,10 0 too.

3. XOR (^): 11 to give to give 1,00 0,10 0 to give (the same is 0, 1 is different).

4. Non (~): This is a unary operator, the above three unary 2, 0 to give to give 1,1 0 (inverted).

Shift operators:

Remember: left shift operation is operator integer type (int) is the operand, the number of bits to be shifted to the right.

1. Left displacement operator (<<): to the left operand operator the number of bits specified by the operator the right to left movement (0s at low, low is the right, the left is high)

2. "signed" right shift operator (>>): the number of bits specified by the operator the right operator moves to the right to the left operand ( "signed" using right-shift operators "sign extension" : If the sign is positive, 0 is inserted at a high level; if the sign bit is negative, the insert 1 at a high level).

3. "unsigned" right shift operator (>>>): It uses the "zero-extended": regardless of the sign, are inserted in the high 0.

4. Then the left shift operator only one kind, and not as a right shift operator.

Negative bit computing:


Remember: int type is 8 bytes, 32-bit, high-order 0 is not displayed. And the most significant bit 0 or 1 is represented by a positive or negative, so the range is represented as an int: -1 to 31 th 31 th negative (most significant bit is the sign 2 of 2, only 31 bits represents a number, and because there are a positive number of 0, it is a power of 2 to 31 minus 1).

A positive integer arithmetic is in binary bit into 32-bit source code, do not show high 0 (Integer.toBinaryString (digital))

As an example the following code to -6, then (-6) is output Integer.toBinaryString (1,111,111,111,111,111 1,111,111,111,111,010)

Integer.toBinaryString(6) = 110;

1. Switch Source: 1 highest bit is negative, its absolute value of the other (1,000,000,000,000,000 0,000,000,000,000,110)

2. anti-code: MSB unchanged, the other bit is inverted (1,111,111,111,111,111 1,111,111,111,111,001).

3. Complement: +1 to the inverse code (1,111,111,111,111,111 1,111,111,111,111,010) Description: The last 001 represents 1,1 + 1 = 2, the last three should be converted to binary 010 2,2

Re-converted to decimal, just opposite the operation on the line:

1. Anti-complement code becomes -1

2. anti-code: MSB unchanged, other bits become inverted source

3: Binary Code Decimal calculated by

Even if the shift operation is a negative number, the complement becomes only adding a step between the counter code, the complement displacement into a new complement, then the new code is reversed complement -1

Negative bit computing Source: https: //blog.csdn.net/weixin_37322501/article/details/85759105

 

Guess you like

Origin www.cnblogs.com/woyujiezhen/p/11488922.html