Before doing the project when the use of bit operation is not many, today in the brush leetcode on the topic of the use of bit operation, is leetcode in the 29th question divide two integers.
One, the bit operation of Java:
A bitwise operation expression consists of an operand and a bitwise operator that implements a bitwise operation on a binary number of integer types. Bitwise operators can be divided into logical operators (including ~, &, | and ^) and shift operators (including >>, <<, and >>>).
1) The left shift operator (<<) moves the operand to the left of the operator to the left by the number of digits specified to the right of the operator (in the low 0). Shift left one (in the case of no overflow) is equal to multiplying by 2.
2) The "signed" Right Shift operator (>>) moves the operand to the left of the operator to the right by the number of digits specified to the right of the operator. The "signed" right shift operator uses "symbol extension": If the value is positive, insert 0 at the high, and if the value is negative, insert 1 at the high. Shift right one is equivalent to dividing by 2.
3) Java has also added an "unsigned" right shift operator (>>>) that uses the "0 extension": both positive and negative, inserting 0 in the high position.
4) If the Char,byte or short are shifted, they are automatically converted to an int before the shift is made. Only the 5 lows on the right will be used. This prevents us from moving the impractical number of digits in an int number. If a long value is processed, the last result is a long. The type that needs to be used in the leetcode problem is long.
5) In these 6 kinds of operators, only ~ is the monocular operator, the other 5 are binocular operators.
Second, the Java bit operation of the actual operation:
Examples of bit operations we see from other blogs are often given to a number and then output via SYSO.
Program: Public class plusrightmoving{ publicstaticvoid main (string[] args) { System. out. println ("5>>1="+ (5>>1)) ; }} Output results:5>>1=2
It is important to note, however, that the assignment operation is not the same as in C + +.
To move left and right in C + +:
Div << 1<< 1;
However, the displacement operation in Java requires a mate assignment:
long div = 1; long res = 1; while (Div < dived) { = div << 1; = Res << 1;} if (div! = dived) {= div >>1 ; = Res >> 1;}
If you just do the same thing in C + +, you won't be able to compile.
Three, bit operation of some application questions:
1) Judging parity
As long as the least bit is 0 or one decision, 0 is an even number, 1 is an odd number. You can therefore use if (A & 1) = = 0) instead of if (a% 2 = = 0) To determine if a is an even number. The following program outputs all the even numbers between 0 and 100.
2) Exchange two number
int c = 1, d = 2;
C ^= D;
D ^= C;
C ^= D;
System.out.println ("c=" + C);
System.out.println ("d=" + D);
3) Transform Symbols
For 11 and 11, you can change-11 to 11 by the following transformation method
1111 0101 (binary) – Reverse, 0000 1010 (binary) – Add 1-> 0000 1011 (binary)
It's also possible to turn 11 into a-11
0000 1011 (binary) – Reverse, 0000 0100 (binary) – Add 1-> 1111 0101 (binary)
Other applications can refer to the blog post:
http://blog.csdn.net/wfzczangpeng/article/details/51819471
There are many application descriptions of Java bit operations.
Bit operations in Java