Java bit operator notes, Java operator notes

Source: Internet
Author: User

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

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.