Java Bitwise operators

Source: Internet
Author: User
Tags arithmetic bitwise bitwise operators

Objective

Recently, looking at the source code of HashMap, I saw some bit operators and decided to study the bit operators in depth.

Bit arithmetic

Definition: All the numbers in a program are stored in binary form in the computer's memory. Bitwise arithmetic is the direct operation of an integer in memory bits.

Bitwise operators

The bitwise operators in Java have:&, |, ^, <<, >>, >>>, ~ 7

1.& (with), | (or), ^ (XOR)

 Public Static voidMain (string[] args) {intA = 10; System.out.println ("A's binary representation:" +integer.tobinarystring (a)); intb = 11; System.out.println ("B binary means:" +integer.tobinarystring (b)); inti = A &b; System.out.println ("A & B =" + i + ", the binary representation of I:" +integer.tobinarystring (i)); I= A |b; System.out.println ("A | b = "+ i +", the binary representation of I: "+integer.tobinarystring (i)); I= a ^b; System.out.println ("a ^ b =" + i + ", the binary representation of I:" +integer.tobinarystring (i)); }

Run results

Binary representation of a: 1010bbinary representation: 1011& b = 10,i binary representation: 1010 | b = binary representation of the 11,i: 1011^ b = 1,i Binary representation: 1

You can view binary by using the integer class in Java to convert int into binary representation

The binary representation of a ^ B here can be seen as 0001

Can see

& (with): When A and B binary representations of the same number of bits are 1 o'clock, I the corresponding number of digits on the value is 1 (both 11-->1-->0 01-->0 00-->0), it is not difficult to see I <= min (A, b)

| (OR): When both A and B binary representations have the same number of bits 0 o'clock, the value of I corresponds to the number of digits is 0 (both 11-->1-->1 01-->1 00-->0), it is not difficult to see I >= Max (A, b)

^ (XOR): When the same bit number is the same on the A and B binary representations, the paper value on the corresponding digit of I is 0 (both 11-->0-->1 01-->1 00-->0)

2. << (left shift), >> (with symbol right shift), >>> (unsigned Right shift)

 Public Static voidMain (string[] args) {inti = 1 << 2; System.out.println ("1 << 2 =" +i); I= 1 << 30; System.out.println ("1 << 30 =" +i); I= 1 << 31; System.out.println ("1 << 31 =" +i); I= 1 >> 1; System.out.println ("1 >> 1 =" +i); I= 1 >> 32; System.out.println ("1 >> 32 =" +i); I=-1 >> 1; System.out.println ("-1 >> 1 =" +i); I=-1 >>> 1; System.out.println ("-1 >>> 1 =" +i); }

Run results

1 << 2 = 1 = 10737418241 << = -21474836481 >> 1 = 1-1 >> = 1-1 >>> 1 = 2147483647

Under normal circumstances

A << n = (A * (2 of the n-th square))

A >> n= (a-a%2)/(2 of the n-th square)

However, you can see that there are a number of return examples listed in the above example

This is mainly because the range of int in Java

First, the range of int in Java is-2 of 32 times the 32 times-1

In addition, we have not considered the boundary problem of I, when i = 2 of 32 times-1, and then +1, what will happen?

public static void Main (string[] args) {
int i = Integer.max_value +1;
System.out.println (i);
System.out.println (integer.tobinarystring (i));
}

Run results

-2147483648 (-2 of 32 parties. The minimum value of int)
10000000000000000000000000000000

We know that the length of int is 32 bits, to represent the positive or negative of int, the practice in Java is to mark on 32nd bit, the 32nd bit =0, int is positive, 32nd bit =-1, int is negative

So

Binary: 00000000000000000000000000000000, representing the smallest positive number 0

Binary: 10000000000000000000000000000000, representing the smallest negative number-2147483648

From the angle of the binary calculation is not difficult to understand, integer.max_value+1 = Integer.min_value

That's why 1<<31 =-2147483648

We'll see.

1 >> 1 = 0 and 1 >> 32 = 1

Here I understand that the shift operation in binary is actually a binary number with a length of 63, where the continuous 32 bits represent the value of int, and the remainder is all 0.

And then say this whole 63-bit binary number as a circle (circular queue)

So, whether a number is shifted to the left or the right 32 bits, it is always the number itself.

Then, let's take a look at -1 >>1 = 1 and -1>>>1 = 2147483647

First, literally understand,>> signed right shift,>>> unsigned right shift

Further explanation is easy, 63-bit-length binary number, where the continuous 32-bit represents the value of int, the remaining 31-bit redo >> operation, maintain the same value as the int symbol (that is, int is negative, the remaining 31 bits are 1,int, the remaining 31 bits are 0)

>>> unsigned Right shift, no matter what int, the remaining 31 bits are all 0

3.~ (Take the reverse)

     Public Static void Main (string[] args) {        int i = ten;        System.out.println (integer.tobinarystring (i));        System.out.println (integer.tobinarystring (~i));    }

Run results

101011111111111111111111111111110101

Inversion is easy to understand, replace 0 with 1, replace 1 with 0

Complements the 10 binary representation to the full 32-bit

00000000000000000000000000001010 the inverse equals 11111111111111111111111111110101.

You can see i + ~i =-1

--------------------------------------------------------------------------------------------------------------- ------

The first time to write a blog, many places are personal understanding, if the writing is not the place, please for taking correct, thank you!

Java Bitwise operators

Related Article

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.