Java bit operator notes, Java operator notes
Java bitwise operators include: & and, | or, ^ Or ,~ Non, <arithmetic shift left,> arithmetic shift right,> logical shift right
1. & (and)
All are 1 --> 1
All are 0 --> 0
1 has 0 --> 0
Example: 000 111 010 101
000 111 101 101
----------------------------
111 101
1 int i = 1; //000000000000000000000000000000012 int j = 2; //000000000000000000000000000000103 int n = i&j;//000000000000000000000000000000004 System.out.println(n);//0
2. | (OR)
1 --> 1
All 0 --> 0
Example: 000 111 010 101
000 111 101 010
----------------------------
000 111 111 111
int i = 1; //00000000000000000000000000000001int j = 2; //00000000000000000000000000000010int n = i|j;//00000000000000000000000000000011Sys.out.println(n);//3
3. ^ (exclusive or)
All 0 --> 0
Full 1 --> 0
1 has 0 --> 1
Example: 000 111 010 101
000 111 101 010
----------------------------
000 000 111 111
int i = 1; //00000000000000000000000000000001int j = 2; //00000000000000000000000000000010int n = i^j;//00000000000000000000000000000011System.out.println(n);//3
4 .~ (Not)
Change 0 to 1
Change 1 to 0
Example: 000 111 010 101
----------------------------
111 000 101 010
int i = 1; //00000000000000000000000000000001int j = ~i;//11111111111111111111111111111110System.out.println(j);//-2
5. <(left shift)
X <n
Translate x to the left to n places.
int i = 2147483647; //01111111111111111111111111111111int j = i<<1; //111111111111111111111111111111110System.out.println(j);//-2
6.> (right shift)
X <n
Translate x to the Right to n places.
Int I = 2147483647; // required j = I> 1; // required o =-2147483647; // required bytes exceed 00000000000000000000001int k = o> 1; // 1110000000000000000000000000000000000system. out. print (j); // define 3741823system. out. print (k); //-bytes 3741823int n = I> 32; System. out. print (n); // 2147483647 >>>>>>>>and <. If you move a number greater than the maximum number of digits of this type, the number of digits to be moved is modulo. Actually moved 32% 32 = 0
7. >>> (logical right shift)
X> n
Translate x into n places to the right. Add 0 to both positive and negative values.
int i=2147483647;//01111111111111111111111111111111int j=i>>>1; //00111111111111111111111111111111int o=-2; //11111111111111111111111111111110int k=o>>>1; //01111111111111111111111111111111System.out.println(j);//1073741823System.out.println(k);//2147483647