Java bitwise operations (SHIFT, bit and, or, XOR, non)

Source: Internet
Author: User

publicclassTest {    publicstaticvoidmain(String[] args) {        // 1、左移( << )        // 0000 0000 0000 0000 0000 0000 0000 0101 然后左移2位后,低位补0://        // 0000 0000 0000 0000 0000 0000 0001 0100 换算成10进制为20        System.out.println(5<< 2);// 运行结果是20        // 2、右移( >> ) 高位补符号位        // 0000 0000 0000 0000 0000 0000 0000 0101 然后右移2位,高位补0:        // 0000 0000 0000 0000 0000 0000 0000 0001        System.out.println(5>> 2);// 运行结果是1        // 3、无符号右移( >>> ) 高位补0        // 例如 -5换算成二进制后为:0101 取反加1为1011        // 1111 1111 1111 1111 1111 1111 1111 1011        // 我们分别对5进行右移3位、 -5进行右移3位和无符号右移3位:        System.out.println(5>> 3);// 结果是0        System.out.println(-5>> 3);// 结果是-1        System.out.println(-5>>> 3);// 结果是536870911         // 4、位与( & )        // 位与:第一个操作数的的第n位于第二个操作数的第n位如果都是1,那么结果的第n为也为1,否则为0        System.out.println(5& 3);// 结果为1        System.out.println(4 & 1);// 结果为0        // 5、位或( | )        // 第一个操作数的的第n位于第二个操作数的第n位 只要有一个是1,那么结果的第n为也为1,否则为0        System.out.println(5| 3);// 结果为7         // 6、位异或( ^ )        // 第一个操作数的的第n位于第二个操作数的第n位 相反,那么结果的第n为也为1,否则为0         System.out.println(5^ 3);//结果为6         // 7、位非( ~ )        // 操作数的第n位为1,那么结果的第n位为0,反之。        System.out.println(~5);// 结果为-6     } }

Java bitwise operations (SHIFT, bit and, or, XOR, non)

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.