Java-bit computing basics

When learning Java operations, supplementary learning Java-bit computing.

Java-bit computing

Bit-bit arithmetic operations are performed for a complement integers.

& Bitwise AND

First integer to complement, and then performs a bitwise AND operation, the final result will be returned as a decimal, which has the following rules:

(1) positive number = & other positive number considering most significant bit is 0, the bit are 0

(2) even-number = even consider other & lowest bit is 0, the bit is 0

(3) = 0 0 Number of Other & bitwise and the digits are all 0

(4) = 1 & 1,1 & even-odd = 0 it can be quickly determined by a number is odd or even

| Bitwise OR

First integer to complement, and then performs a bitwise OR operation, the final result will be returned as a decimal, which has the following rules:

(1) negative | = number of other negative consider the most significant bit is 1 bit or both 1

(2) an odd | = odd number of other considerations lowest bit is 1 bit or both 1

(3) 0 | = original number of other Bitwise or after the same number of digits

^ Bitwise exclusive-OR operation

First integer to complement, or ectopic then performed in operation, the final result will be returned as a decimal, which has the following rules:

(1) even-even-^ = even considered the lowest bit is 0, the bitwise XOR to zero

(2) even-odd ^ = 1 is odd and considering the lowest bit 0, bit exclusive OR is 1

(3) = even odd odd ^ considered the lowest bit is 1, bitwise XOR to zero

(4) a ^ b ^ b or b ^ a ^ b = ab ^ b = 0, then the other a number 0 or exclusive, is the original number

Sample Code

. 1  public  class OperatorAndOrXor {
 2      public  static  void main (String [] args) {
 . 3          // test bitwise AND operation 
. 4          int A =. 9 ;
 . 5          int B =. 11 ;
 . 6          System.out.println (A & B); // . 9
 . 7  
. 8          // test bitwise oR operation 
. 9          System.out.println (A | B); // . 11
 10  
. 11          // test bitwise exclusive oR operation 
12 is          System.out.println (A ^ B); // 2 
13 is          System.out.println (A ^ B ^ B); //9
14     }
15 }

Test Results

<< Left

Data left, first convert it to complement code and corresponding rank moved leftward, the removed portion discarded, the low air 0s, and finally converted to decimal, left movement corresponding to several times by 2 the side, the type 1 byte left by 3 bits, that is, from 0000 0001 → 0000 1000, becomes 8.

>> Right

Data to the right, first convert complement, and then move to the right the appropriate rank, the removed portion discarded, the upper space, there are two cases, if the high bit 0 is a positive number, the negative high bit 1, and finally converted to decimal , within a certain range, the right movement corresponds to several times divided by 2 side, such as the right three 8 byte type, that is, from 0000 1000 → 0000 0001, it becomes 1.

Further positive right movement, the movement more 0, such as the type of the last byte complement becomes 00000000, it is converted into the original code 00000000, 0 becomes the minimum.

Negative right movement, the more a movement, such as a byte, and finally becomes complement 1111 1111, is converted into the anti-code 1111 1110, then converted into the original code 10000001, the final result becomes -1.

>>> unsigned right movement

Unsigned right shift, first convert complement, and then move to the right the appropriate rank, the removed portion discarded, whether negative or positive high number of empty all 0s, and finally converted to decimal. It can be seen that the unsigned positive right and the right movement is no difference, there is a negative difference, negative >>> unsigned right shift operation becomes positive, and the larger the negative number, the more the 1's complement, the larger the positive number unsigned right shift.

~ Bitwise

After the data is converted to two's complement, all variations 0,0 1 becomes 1, the final result will be converted to decimal bitwise complementary symmetry formula -n = ~ n + 1.

Code section

. 1  public  class MoveMethod {
 2      public  static  void main (String [] args) {
 . 3          // test left, right, unsigned shift right, the negation operator
 4          // shift left 
. 5          int I =. 1 ;
 . 6          the System.out .println (I <<. 3); // . 8
 . 7  
. 8          // right 
. 9          int J =. 8 ;
 10          System.out.println (J >>. 3); // . 1
 . 11  
12 is          // left right large bits 
13 is          System.out.println (I << 35); // . 8 
14         System.out.println (J >> 35); // . 1
 15  
16          // unsigned right shift 
. 17          System.out.println (J >>>. 3); // . 1 
18 is          int K = -1 ;
 . 19          the System. Out.println (K >>>. 1); // negative becomes positive
 20 is  
21 is          // inverted n-+. 1 ~ = -n 
22 is          System.out.println (~ J); // -9 
23 is          the System.out .println (~ K); // 0 
24      }
 25 }

Test Results

Note that, the shift operation is not directly move the corresponding bits, but the first 32 bits of the modulo, a remainder corresponding to the number of performed move the corresponding rank. The << 1 << 1 35 = (32 35%) = 1 << 3 = 8.

Bitwise XOR for exchanging integer

Bitwise exclusive or features may be used in exchange integer above, except bitwise XOR exchange, there are two other exchange.

. 1  public  class ChangeMethod {
 2      public  static  void {main (String [] args)
 . 3          // three ways exchange value
 . 4          // the Option1 bitwise exclusive or exchanged for an integer 
. 5          int A =. 5 ;
 . 6          int B =. 7 ;
 . 7          A = A ^ B;
 . 8          B = A ^ B; // B = A B = A ^ B ^ 
. 9          A = A ^ B; // A = B ^ A ^ A ^ A ^ A = B = B 
10          System.out.println (A);
 . 11          System.out.println (B);
 12 is  
13 is          // option2 subtraction exchange, for all values, can also be used in addition to several other integer
14          int X =. 5 ;
 15          int Y =. 7 ;
 16          X = X + Y;
 . 17          Y = XY; // Y = X = X + YY 
18 is          X = XY; // X = Y = X + YX 
. 19          the System.out .println (X);
 20 is          System.out.println (Y);
 21 is  
22 is          // Option3 intermediate value exchange, also called rear-end exchange method, applicable to all types 
23 is          int I =. 5 ;
 24          int J =. 7 ;
 25          int TEMP = I;
 26 is          I = J;
 27          J = TEMP;
28         System.out.println(i);
29         System.out.println(j);
30 
31     }
32 }

Exchange results

These three methods have their advantages and disadvantages of the exchange, the following is a summary, XOR most efficient method, the least efficient rear-end method, using different methods or minimum frequency, maximum rear-end method.

to sum up

Are required to convert a mobile complement (1) of a bit operation to learn the current total of 7, it is operated.

(2) bitwise XOR operation can be used to exchange two bit integers.

Reference Hirofumi:

(1) https://www.cnblogs.com/youngchaolin/p/10463887.html#_label4

(2) https://www.cnblogs.com/youngchaolin/p/11290467.html

Guess you like

Origin www.cnblogs.com/youngchaolin/p/11291809.html