Java bit Operation notes

Source: Internet
Author: User

Bit operations:
~ (non)-"binary number 0 and 1 interchange

Example:

public class Test {public static void main (string[] args) {System.out.println (~-2);//Output 1system.out.println (~-1);// Output 0system.out.println (~0),//Output -1system.out.println (To),//Output -2system.out.println (~);//Output -3system.out.println (~ 3);//Output-4}}






^ (XOR)--"12 ^ 10 = ... 01100^01010 = 00110 = 6

Example:

public class Test {public static void main (string[] args) {int a = 0;int b = 0;b = a = 12^10; System.out.println (a);//output is 6a = a^12; System.out.println (a);//output is 10b = b^10; System.out.println (b);//Output is 12}}


Application: Two different numbers to exchange

public class Test {public static void main (string[] args) {int a = 12;int B = 10; System.out.println (A + "---" + b);//output is---10a = a ^ B;b = a ^ B;a = a ^ b; System.out.println (A + "---" + b);//Output is---12}}



& (with)--"10 & = ... 01100 & 01010 = 01000 = 8

Example:

public class Test {public static void main (string[] args) {int a = 12;int b = 10;int C = a&b; System.out.println (c);//Output is 8}}


Application:

public class Test {public static void main (string[] args) {int[] a = new int[2];a[0] = 5;a[1] = 6;for (int i = 0; i < a . length; i++) {if ((A[i] & 1) = = 1) {//Determines whether it is an odd System.out.println (A[i] + "odd"),} else {System.out.println (A[i] + "even");}} Output://5 "odd"//6 "even"}}


| (or)--"12 | 10 = ... 01100 | ... 01010 = 01110 = 14

Example:

public class Test {public static void main (string[] args) {int a = 12;int B = 10;//... 01100 | ... 01010 = 01110 = 14int c = A | b System.out.println (c);//Output is 14}}


Application: An integer that can be packaged into a different number of digits together with the displacement

public class Test {public static void main (string[] args) {int a = 1;int b = 2;//256 | 2 = ... 01 0000 0000 | ... 0010 = ... 0000 0010 = 258int c = a << 8 | b System.out.println (c);//output is 258}}


>> (right Shift)--"12>>2 = 00...01100 >>2 = 00...00011 = 3

-1 >>>24 = 1111...111 >>>24 = 1111...1111 1111 = 1
>>> (unsigned)--" -1 >>>" = 1111...111 >>> 24 = 0000...1111 1111 = 255

public class Test {public static void main (string[] args) {int a = -1;//1111...111 >>> 24 = 0000...1111 1111 = 2 55int B = a>>>24; System.out.println (b);//output is 255}}
12>>2 = 00...01100 >>2 = 00...00011 = 3






Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Java bit Operation notes

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.