Preface:
Why do we need to talk about lower bit operations? In fact, I found a lot of performance code has a lot of bit operation figure, has been mentioned before the high performance of bit operation, but the work of business code farming process used very little, has not been to understand it, today to see how this guy so cow! If the article has what incorrect place, welcome everybody points out ha, thanks thanks.
● Decimal to binary (positive and negative) ● Binary addition and subtraction ● bit operator ● Bit movement operator
Change from the decimal system to the two-level system
Bit operations are a two-level affair, but since we usually use ten-digit calculations, let’s go over the conversion from decimal to binary.
Methods: given in decimal divided by two, record the remainder (0 | 1), until the end, will the remainder obtained against reading and is the binary value, the value at the same time, also known as the original code. If it is negative, it is converted to binary as source -> inverse (the original 0 becomes 1, 1 becomes 0) -> complement (the inverse + 1).
(1) Positive decimal 29 is converted to binary value 11101.
(2) If the number is negative -29, the original code 11101 is obtained, and then the inverse code 00010 is obtained, and then the inverse code 00011 is obtained, indicating that the negative number is preceded by 1. If 32-bit binary data needs to be obtained, The binary of -29 is 1111 1111 1111 1111 1111 1111 1111 1110 0011.
Integer.tobinarystring (N); integer.tobinaryString (N); Can output converted binary results. Integer. Pase (“1100”, 2);
Binary addition and subtraction
Just get negative numbers, has found that binary also have addition calculation oh, a simple description of the process of addition and subtraction, the main attention to addition 1+ 1 result is 0, need to enter a, subtraction, 0-1 need to borrow a as 2 calculation, the result is 1.
The decimal values are in parentheses next to the binary text.
Addition: 0101 (5) + 1101 (13)
During the operation, the calculation is carried out bit by bit from right to left.
- 1 + 1 = 0; Into one.
- 0 plus 0 plus 1 is 1.
- 1 + 1 = 0; Into one.
- 0 +1 +1 = 0; Into one.
- The result is 1 0 0 1 0 (18).
Subtraction: 0101-1101
Operation process:
- 1 -1 = 0;
- 0-0 = 0;
- 1 -1 = 0;
- 0 minus 1 is minus 1 plus 2 is 1
- There’s no value here, so I’m going to keep borrowing it and it’s going to be 1. If the result is 32 bits, the value is 1111 1111 1111 1111 1111 1111 1111 1000 (-8);
An operator
Mainly includes the four operators: and (&), not (~), or (|), or (^)
The calculation method is simply described and summarized as follows
- And (&) : both are 1, only 1.
- Non-(~) : 0 becomes 1,1 becomes 0.
- Or (|) : 1 is 1.
- Xor (^) : same is 0, different is 1.
Bit shift operator
There are three main types of << (left move), >> (right move), >>> (unsigned right move), see the symbol here, the beginning of the interview question appeared, how to calculate the fastest number multiplied by 8 calculation method? The answer is the number << 3. Because computer language is binary ah, whether JAVA or other languages, are eventually converted to binary computing, if the direct is binary computing, not need to turn.
“Because shift instructions account for two machine cycles, while multiply and divide instructions account for four machine cycles.”
<< (left shift) N: To move the original value to the left by N bits, adding N zeros in the lower order, computationally multiplying the original value by N twos,
1001 << 3 :
(Right shift)>> N: Here is the distinction between positive and negative, the top 0 is positive, the top 1 is negative. Move the original value N to the right. If the value is positive, the high value needs to be replaced by 0. If the value is negative, the high value needs to be replaced by 1. So this is the same thing as dividing by 2 N times.
(unsigned right shift)>>> shape >> (right shift), but do not care about positive and negative numbers, the high level of all complement 0, and we are done.
Contrast:
-
1101 0101 >> 2 = 11110101
-
1101 0101 >>> 2 = 00110101