Bit operations defined by Java apply to integer types such as int, long, short, char and byte.
Bitwise operators are mainly used to perform operations on the binary bits of the operands. Bitwise operations represent calculations based on each binary digit. The operands and operation results are both integer values. If the operation result is a negative number, the complement representation is used. .
Bit operators in Java language are divided into two categories: bit logical operators and bit shift operators.
Bitwise AND operation:
The bitwise AND operation performs a bitwise operation on two integer data a and b, and the result of the operation is an integer data c. The algorithm is: if the corresponding bits of the two data a and b are both 1, then the bit of c is 1, otherwise it is 0.
For example:
a:0 1 0 1 0 0 1 1
b:0 0 1 1 1 0 1 0
c:0 0 0 1 0 0 1 0
Bitwise OR operation:
The bitwise OR operation performs a bitwise operation on two integer data a and b, and the result of the operation is an integer data c. The algorithm is: if the corresponding bits of the two data a and b are both 0, then the bit of c is 0, otherwise it is 1.
For example:
a:0 1 0 1 0 0 1 1
b:0 0 1 1 1 0 1 0
c: 0 1 1 1 1 0 1 1
Bitwise NOT operation:
The bitwise NOT operation performs a bitwise operation on an integer data a, and the result of the operation is an integer data c. The algorithm is: if the corresponding bit of the data a is 0, then the bit of c is 1, otherwise it is 0.
For example:
a:0 1 0 1 0 0 1 1
c:1 0 1 0 1 1 0 0
Bitwise XOR operation:
The bitwise XOR operation performs a bitwise operation on two integer data a and b, and the result of the operation is an integer data c. The algorithm is: if the corresponding bits of the two data a and b are the same, then the bit of c is 0, otherwise it is 1.
For example:
a:0 1 0 1 0 0 1 1
b:0 0 1 1 1 0 1 0
c:0 1 1 0 1 0 0 1
Bitwise right shift operation:
The algorithm of bitwise right shift operation is as follows: move all numbers to the right by the corresponding number of digits in binary form, shift out (discard) the low bits, and fill the empty bits in the high bits with zeros.
For example: shift the integer 83 to the right by 1 bit
Before shifting: 0 1 0 1 0 0 1 1
After shifting: 0 0 1 0 1 0 0 1
Bitwise left shift operation:
The algorithm of bitwise left shift operation is: shift all numbers to the left by the corresponding number of digits in binary form, shift out the high bits (discard), and fill the empty bits in the low bits with zeros.
For example: shift the integer 83 to the left by 1 bit
Before shifting: 0 1 0 1 0 0 1 1
After shifting: 1 0 1 0 0 1 1 0
Example:
publicclassMain{publicstaticvoidmain(String[]args){inta=83;/*83=01010011*/intb=58;/*58=00111010*/intc=0;c=a&b;/*18=00010010*/System.out .println(a&b=+c);c=a|b;/*123=01111011*/System.out.println(a|b=+c);c=~a;/*01010011 is 10101100 after negation. The complement of 10101100 is 11010100, -84=11010100*/System.out.println(~a=+c);c=a^b;/*105=01101001*/System.out.println(a^b=+ c);c=a>>1;/*41=00101001*/System.out.println(a>>2=+c);c=a<<1;/*166=10100110*/System.out. println(a<<2=+c);}}
The running results are as follows:
a&b=18a|b=123~a=-84a^b=105a>>2=41a<<2=166