bit operation in java

Because of work needs, I re-learned the relevant content of bitwise operators. When I encountered a problem, I searched Baidu and found that I didn't find what I wanted. After consulting the relevant documents and clarifying this part of the content, I will record my understanding, hoping to help new friends. Corrections are welcome for incorrect places.
First of all, it is pointed out that when performing bit operations, all operations are calculated using two's complement! !
Here is a brief introduction to the acquisition of complements to facilitate the understanding below.
First of all, there are three kinds of codes in the computer: the original code, the inverse code, and the complement code .
Original code : the "original binary code". That is, the code obtained after converting the decimal number to binary. For example, the decimal number 8, converted into binary, is "0000 1000", and this code is the original code of the number 8.
Complement code : The inverse code of a positive number is the original code itself.
The inverse code of a negative number is the new binary number obtained by inverting all other bits except the sign bit.
PS: Sign bit: In binary, the first bit is considered to be the sign bit, 0 for positive numbers and 1 for negative numbers.
Two's complement : The complement of a positive number is itself. (that is, three positive numbers are the same).
The complement of a negative number is its complement plus one.
For example, for a positive number 8, its three codes are "0000 1000";
for a negative number -5 (1000 0101), its complement is: "1111 1010", and its complement is: "1111 1011".
If you still don't know your classmates, you can refer to https://jingyan.baidu.com/article/1e5468f90a9568484861b77c.html .

There are seven kinds of bitwise operators in java, and I will explain them one by one below.

Basic Rules
of Bitwise Operators 1. Bitwise operators are all performed on the binary digital form of integers.
2. Basic rules of bitwise AND operation (rule table):
1 & 1 ==> 1
1 & 0 ==> 0
0 & 1 ==> 0
0 & 0 ==> 0;
3. Basic rules of bitwise OR operation
1 | 1 ==> 1;
1 | 0 ==> 1;
0 | 1 ==> 1
0 | 0 ==> 0
4, bitwise NOT:
~ 1 ==> 0
~ 0 ==> 1
5 , bitwise XOR operation:
1 ^ 1 ==> 0
0 ^ 0 ==> 0
1 ^ 0 ==> 1
0 ^ 1 ==> 1
Rule: the same is 0, the difference is 1
1.
Bitwise negation (~) Bitwise negation, that is, the number obtained by inverting each bit of the binary code. (Keep in mind that the two's complement code is used in the calculation.)
For example, the decimal number 60, its binary code is: "0011 1100", invert it to get "1100 0011", which is the number obtained after the bitwise negation of 60 (complement). code). Converted to decimal, the sign bit of "1100 0011" is 1, indicating that it is a negative number. The logic of taking the complement of a negative number is: first invert, add one, and the sign bit does not change. (The calculation process of taking the complement code and taking the original code is the same). Therefore, the inversion of "1100 0011" is "1011 1100", and the last digit is added to get "1011 1101", which is converted to -61 in decimal.
2. Bitwise AND (&) form
: A & B; //A and B are both numbers, which are actually regular decimal numbers, but will be calculated in binary internally
The result of the bitwise AND (&) operation of a binary number on a bit.
Explanation: The binary form of a number is usually a combination of 32 bits of 0s and 1s, and it may be 64 bits, but for illustration, we usually only use 8 bits.
Example:
result = 9 & 13; //The binary of 9 is "1001", the binary of 13 is "1101" and the
result is "00001001", which is 9
3. Bitwise OR (|)
form: A | B; //A Both A and B are numbers, but they are actually regular decimal numbers, but they are internally calculated in binary.
Meaning : The result of performing a bitwise OR (|) operation on the binary numbers in each bit of the binary form of A and B .
Example:
result = 18 | 10; //The binary of 18 is "10010", the binary of 10 is "1010" and the
result is "00011010", which is 26
4. The result of bitwise exclusive OR (^)
is , the number obtained by XORing each bit of the two numbers operated on.
5. Left shift (<<)
form: A << n; //A is a number to be shifted, it can also be a common number, but it is operated in binary, n is a specified number of digits to be shifted
Meaning : to The number on each bit of the binary form of the number A is shifted to the left by the specified number of bits n, then the leftmost n is shifted out, and it is ignored .
Example:
int a=60;"0011 1100", a<<2 to get "1111 0000", which is 240.
6. Right shift (>>)
form: A >> n; //A is a number to be shifted , it can also be an ordinary number, but it is operated in binary, n is a specified number of digits to be moved
Meaning : move the number on each digit of the binary form of the number A to the right by the specified number of digits n, the rightmost n After the displacement is removed, it is directly removed, and the leftmost n bits are filled according to the number of the original sign bit.
For example:
When shifting to the right in java, because it involves the left-hand complement, it should be understood by referring to the memory length occupied by the data type in java (personal understanding). If the abbreviation is expressed in 8 digits, it is easy to be confused.
For example, find the value of -5>>2.
The binary representation of -5 is "10000000 00000000 00000000 00000101", and its complement is: "11111111 11111111 11111111 00001011", after shifting two bits to the right, the two bits on the right are directly removed, and the two complement sign bits on the left are "11111111 11111110 111111111000 11111". ”, which is -2.
7. Unsigned right shift (>>>)
The difference between unsigned right shift and right shift is that after the right shift, the bit vacated on the left of the right shift (>>) uses the sign bit to pad, and unsigned right shift (>>>) pads with 0 regardless of the sign bit.
Example:
Find the value of -5>>>2.
The binary representation of -5 is "10000000 00000000 00000000 00000101", and its complement is: "11111111 11111111 11111111 00001011", after unsigned right shift by two bits, we get: "00111111 11111111 11111111111111110101", its value is 28111111101010. (So ​​it is recommended in java to fill up the 4 bytes of int for calculation).

**总结**:在进行位运算的时候,首先需要了解位运算的计算逻辑,如1|0=1等。第二点是比较容易忘记的,计算机中进行位运算时,使用的均是**补码**,在进行实际运算时,要时刻牢记补码的概念,牢记对数值进行补码运算,尤其是对负数进行运算的时候。

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325948891&siteId=291194637