java in 7 Bitwise Operators

Bit arithmetic shift operation means is performed for a binary integers.

Bit computing to provide a higher efficiency than the arithmetic, but bit operation code readability is poor, it is recommended to use bit computing where all write comments.

Java provides seven bits for bitwise operations.

Left (<<)

Left shift operation is a bitwise binary value of the operand a number of bits left, left unchanged during the sign bit, the upper overflow is discarded, the lower 0s.

example result example result
00000001<<2 00000100 10000001<<2 10000100
01100001<<2 00000100 11100001<<2 10000100

Right shift (>>)

A right shift operation is a bitwise binary value of the operand a number of bits to the right, during the symbol bit right shift change, the lower overflow is discarded, and supplemented with upper overflow sign bit, i.e., make a negative, a positive number of 0s.

example result example result
00000100>>2 00000001 10000100>>2 11100001
00000111>>2 00000001 10000111>>2 11100001

Unsigned right shift (>>>)

Unsigned right shift operation is all binary operand values ​​bit by bit to the right a number of bits, the most significant bit comprises the sign bit, followed right, and discard the overflow low, high 0s. Note that the sign bit (MSB) unsigned right shift (>>>) will follow the change.

example result example result
00000100>>>2 00000001 10000100>>>2 00100001
00000111>>>2 00000001 10000111>>>2 00100001

versus(&)

& Performed with two binary arithmetic operation is involved in the operation, if the two bits are 1, the result of the operation is 1 and all others are 0.

example result example result
0&0 0 0&1 0
1&0 0 1&1 1

Or (|)

Or operation of two binary numbers will be involved in operations carried out | operation, if the two bits are 0, the operation and the result is 0, 1 other all. I.e., as long as a digital bit is 1, the bit computation result is 1. Note that the sign bit is the same operation.

example result example result
0|0 0 0|1 1
1|0 1 1|1 1

Non (~)

Non-operator is also called the negation operator, to operate only for a binary number, if the bit is 0, then the inverted bit 1; if bit is 1, bit 0 is inverted. Note that the sign bit is the same operation.

example result example result
~0 1 ~1 0
~0000 0001 1111 1110 ~1111 1111 0000 0000

XOR (^)

The XOR operator is involved in two of the binary XOR operation, if the two bits are the same, the result is 1, and 0 otherwise.

example result example result
0^0 0 0^1 1
1^0 1 1^1 0

 

"The more a story of simple people more calm, more superficial thin people more restless."

Guess you like

Origin www.cnblogs.com/yanggb/p/10649984.html