[Imported from the blog of Prince heima] bit operations in Java include: <br/> ~ By bit not (not) <br/> & by bit and (and) <br/> | by bit or (OR) <br/> ^ by bit or (XOR) <br/> right shift <br/> unsigned right shift <br/> <left shift <br/> the preceding steps are very simple, shift operations are prone to errors. <br/> first, you must understand the number of digits involved in the calculation, for example, the int value is 32 bits. Long is 64-bit. <Br/> for example, int I = 1; <br/> the binary original code of I is: <br/> 00000000000000000000000000000001 <br/> long l = 1; <br/> L binary original code: <br/> 0000000000000000000000000000000000000000000000000000000000000001 <br/> 2. </P> <p> positive numbers do not have backcodes or complementary codes, it can also be said that positive anticode and complement code are the same as the original code. <Br/> the Negative Inverse code is the bitwise inverse of the original code. <br/> for example, int I =-1; <br/> 10000000000000000000000000000001, the highest digit is the symbol bit. The positive value is 0, and the negative value is 1. <Br/> bitwise reverse: <br/> 01111111111111111111111111111110 indicates the reverse code. <Br/> Anti-code plus 1: <br/> 01111111111111111111111111111111 is the supplemental code. <Br/> negative numbers are calculated using the complement code. The obtained code is also a complementary code. You need to subtract 1 to obtain the original code. </P> <p> 3. Common bitwise operators-0 in bitwise operations are special. </P> <p> ^ exclusive or. If the number is the same as 0, the difference is 1. If the number is different from 0, the value is equal to or equal to the original value. <Br/>. All 1 is 1, and 0 is 0; any number is different from 0 or equal to 0. <Br/> | or. 1 is 1, and all 0 is 0. Any number is equal to zero or equal to the original value. <Br/> <move left. Add 0. <Br/> right shift. The sign bit is 0 to 0, and 1 to 1. <Br/> unsigned shift to the right. Add 0. <Br/> ~ Non-bitwise inversion </P> <p> 4. If a negative number is involved in the operation, the complement code is obtained. You must first subtract 1 from the complement code and then obtain the original code by bitwise inversion. The operation result. </P> <p> 0. If the result is 0, the value does not need to be reduced by 1 or reversed. <Br/> In addition, after two positive numbers, the original code is obtained without the need to subtract 1 or reverse. <Br/> example: <br/> 1 ^-1, <br/>-1 <br/> 10000000000000000000000000000001 -- original Code <br/> 01111111111111111111111111111110 -- reverse Code <br/> 01111111111111111111111111111111 -- complement <br/> 1 <br/> 00000000000000000000000000000001 -- original Code <br/> then 1 ^-1 equals <br/> 01111111111111111111111111111111 ^ <br/> 00000000000000000000000000000001 = <br/> 01111111111111111111111111111110 -- complement <br/> 01111111111111111111111111111101 -- reverse Code <br/> 10000000000000000 000000000000010 -- original code =-2 <br/> that is, 1 ^-1 =-2 <br/> example: <br/> 1 ^-2 <br/>-2 <br/> 10000000000000000000000000000010 -- original Code <br/> 01111111111111111111111111111101 -- reverse Code <br/> 01111111111111111111111111111110 -- complement <br/> 1 <br/> 00000000000000000000000000000001 -- original Code <br/> 1 ^-2 equals <br/> 01111111111111111111111111111110 ^ <br/> 00000000000000000000000000000001 = <br/> 01111111111111111111111111111111 -- supplemental Code <br/> 0111111111111111111111 1111111110 -- reverse Code <br/> 10000000000000000000000000000001 -- original code =-1 <br/> 1. <br/> move the logic left, add 0 to the right, and the symbol bit is the same as other bits. <br/> positive value: <br/> x <1 is generally equivalent to 2x, but may overflow. <br/> overflow range: 230 ~ (231-1) binary 010000... 000 to 01111 .... 1111. After the shift, the maximum value is 1, and the number is negative. <br/> negative number: <br/> x <1 is generally equivalent to 2x, and may overflow. therefore, x * 32 can be written as x <5 <br/> overflow range:-231 ~ -(230 + 1) binary 10000... 000 to 101111... 1111. After the shift, the highest value is 0 and the value is positive. <br/> 2.> <br/> right shift of arithmetic, which does not correspond to the preceding one. If it is a positive number, the left side is supplemented with 0. If it is a negative number, the left side is supplemented with 1. <br/> x> 1, equivalent to X/2. The remainder is discarded because it is reduced, so it will not overflow. <br/> However, note that-1 shifts the number of places to the right-1. <br/> In addition, the remainder discarded is positive. 3> 1 = 1. The remainder discarded is 1. <br/>-3> 1 =-2 The remainder discarded is also 1, rather than-1. <br/> for positive numbers x> 1 and X/2 are equal <br/> for negative numbers x> 1 and X/2 are not necessarily equal. <br/> 3.> <br/> logical right shift. This is the same as <br/>. <br/> This moves the symbol bits together and adds 0 to the left. <br/> for positive numbers, >>> it is the same as> <br/> for negative numbers, after the right shift, it becomes a positive number. <br/> you can use integer. tobinary String (int I) is more intuitive than 01 bits. <br/> consider the following <SPAN class = 'wp _ keywordlink '> Code </span>: <br/> for (val = 0; Val <100000; val + = 5) {alterx = Val * 8; myresult = Val * 2 ;}< br/> using the shift operation instead of the multiplication operation can greatly improve the performance. The modified code is as follows: <br/> for (val = 0; Val <100000; Val + = 5) {alterx = Val <3; myresult = Val <1 ;}< br/> after modification, the Code is not multiplied by 8. Instead, the equivalent three-bit left shift operation is used, with 1 shifted to 2. Correspondingly, the one-bit operation on the right is equivalent to dividing by 2. It is worth mentioning that, although the shift operation is fast, it may make the code more difficult to understand, so it is best to add some comments. </P> <p> the unsigned right shift operator ">" fills 0 at the leftmost of the bit string during the right shift of the bit string, this is different from the signed right shift operator ">. ">" Fills in the original leftmost bits from the leftmost of the bit string when the bit string is shifted to the right. That is to say, the leftmost bit of the bit string is the symbol bit. If it is 1, the leftmost is always filled with 1 when the symbol is shifted to the right; if it is 0, the leftmost is always filled with 0 when the symbol is shifted to the right. </P> <p> The following table lists the examples of shift operators. </P> <p> operation result description <br/> 00110010 <2 11001000 the right side is always filled with 0 <br/> 00110010> 2 00001100 the results are the same <br/> 00110010>> 2 00001100 <br/> 10110010> 2 11101100 different results <br/> 10110010> 2 00101100 </P> <p> "bitwise AND" operator "& "logical and, the bitwise OR operator "|" is logical for two bits, and the bitwise XOR operator "^" is an exclusive or operation for two bits. The calculation rules are shown in the following table. </P> <p> the bitwise AND bitwise are different or <br/> 0 & 0 = 0 0 | 0 = 0 0 ^ 0 = 0 <br/> 0 & 1 = 0 0 | 1 = 1 0 ^ 1 = 1 <br/> 1 & 0 = 0 1 | 0 = 1 1 ^ 0 = 1 <br/> 1 & 1 = 1 1 | 1 = 1 1 ^ 1 = 0 </P> <p>