Bitwise operators for Java (>>,<<,>>>,&,|)

Source: Internet
Author: User
Tags bitwise bitwise operators

The main conclusion Java operation of the bitwise operator. java bitwise operators are not tight can improve operational efficiency, but also have unexpected effects (java.util.ArrayDeque has a good embodiment), in the follow-up will be illustrated.

Before you start talking about bitwise operators, simply review the knowledge of the complement and then give a simple example of the complement operation in the computer (addition and subtraction in the computer).   

Complement

When the computer does the addition and subtraction operation, it is all in the complement operation, so the bit operation in Java is performed in the complement. The complement of a positive number is itself, and the complement of a negative number is its inverse code +1. The following example.

Because the int type in Java is 4 bytes and requires 32 bits, it is more intuitive to write and look, using 8 bits for simulation.

For positive 2, the second binary form is 00000010, and its complement is 00000010.

For negative-2, the second binary form is 10000010, and its complement is 11111110.

Why does the computer use the complement of the operation, I use the decimal to simulate a bit. Give two numbers, one is a=9145, the other is b=0645. (The first bit is the sign bit, 9 is a negative number, and 0 indicates a positive number)

For binary, 1 is reversed to 0, because 1+0=1 (less than 2 small 1), for the decimal, 1 is reversed to 8, because 1+8=9 (10 smaller than 1), 16 for 1 is 14, because 1+14=15 (16 small 1);

So for the decimal a=9155, his anti-code is 9844, and its complement is 9845 (anti-code + 1), for B, because it is positive, so the complement is the same as the original code. So A+b (complement) =9845+0645=10500. Because we are all four-bit positive numbers to add, so the first bit of 10500 will be given away, the result is 0500. and 0 represents a positive number, so the result is 500;

One more example, such as a=9645,b=0145 (the first bit is the sign bit, 9 is negative, and 0 is a positive number)

A's complement is 9355,b's complement is the same as the original code. a+b=9355+0145=9500. So the result is-500.

Why does the computer use the complement operation, instead of the original code, because the computer is not like human, he will not do subtraction, but in a way to change the subtraction into an addition operation, that is, the so-called complement.

Bit operations in Java

After the introduction of the complement, the following is the Java bit operation (by means of complement).

Example 1 Right-shift operation

5>>1,-5>>1; The results were 2,-3;

Analyze 5>>1 first, because 5 of the complement is 00000101. Move one to the right after 00000010 (the result is the complement), then the original code (the same as the complement), the result is 2. Because 5 is a positive number, so the highest bit is 0,

Re-analyze -5>>1, because 5 of the complement is 11111011. Move right One after 11111101 (result is the complement), in the original code, the result is 10000011, converted to decimal-3. Because 5 is a negative number, the highest bit is 1.

Example 2 unsigned Right shift

The 5>>>1,-5>>>1; results were 2,2147483645

In Example 1 right-shift operation, the positive movement, the highest bit of 0, while negative movement, the highest bit 1, and for unsigned right shift, whether positive or negative, moving the highest bit will be 0;

5>>>1 similar to Example 1, the direct analysis of -5>>>1. -5 of the complement is 11111011, unsigned right shift one after 01111101, then the original code (moved to become a positive number, so the original code and the same complement) the final result is 2147483645.

Example 3 & operation5&7,5&-7,-5&-7. The result is 5,1,-7.Analyze 5&7 first, because 5 of the complement of 00000101,7 is 00000111, so two and after 00000101, that is, 5. (& operation is every one to operate, is 1 words result is 1)Re-analysis 5&-7,5 's complement to 00000101,-7 's complement is 11111001. So two and after 00000001 (because it is a positive number, its original code with the same complement), converted to decimal 1.Re-analysis -5&-7,-5 's complement for the 11111011,-7 of the complement of 11111001, two and after 11111001, because it is negative, so the original code, 10000111, the result is-7.Java's ' << ' and ' | ' operations are similar to ' >> ' and ' & ' and are not illustrative.
Application of Java bit operation in JDKThere is a Arraydeque class in the Java.util package to implement the operation of the queue. A e[] elements array is maintained in the Arraydeque<e> class to store the object.The Arraydeque class provides a different construction method, with a constructor method that defines the size of the elements array, as follows:
Public arraydeque (int numelements) {allocateelements (numelements);        } private void allocateelements (int numelements) {int initialcapacity = min_initial_capacity;        Find the best power of the elements.        Tests "<=" because arrays aren ' t kept full.            if (numelements >= initialcapacity) {initialcapacity = numelements;            Initialcapacity |= (initialcapacity >>> 1);            Initialcapacity |= (initialcapacity >>> 2);            Initialcapacity |= (initialcapacity >>> 4);            Initialcapacity |= (initialcapacity >>> 8);            Initialcapacity |= (initialcapacity >>> 16);            initialcapacity++; if (initialcapacity < 0)//Too Many elements, must back off initialcapacity >>>= 1;//good L    Uck allocating 2 ^ elements} elements = (e[]) new object[initialcapacity]; }

In the Allocateelements method, the size of elements is tested by a series of operations. For example, you give an initial size of 6, after the Allocateelements method, the size of the change is 8. If it is 14, the size of the last elements is 16. In the end, no matter what, will become 2 of the n-th square.
Finally, the main is to say Arraydeque into the team and out of the team. Two variables were maintained in the Arraydeque class, namely head and tail. Points to the first incoming team element and the last incoming team element.
Here's how to queue
    Public boolean Add (E e) {        addlast (e);        return true;    }    public void AddLast (e e) {        if (E = = null)            throw new NullPointerException ();        Elements[tail] = e;        if (tail = (tail + 1) & (elements.length-1) = = head)            doublecapacity ();//This method is explained in a follow-up blog.    

The next code is the operation of the team.
    Public E pop () {        return Removefirst ();    }    Public E Removefirst () {        e x = Pollfirst ();        if (x = = null)            throw new Nosuchelementexception ();        return x;    }

Next, draw a description of the changes in head and tail as you enter and exit the team. At the beginning of the elements array is empty
When an element is inserted
When an element is ejected

When the queue is 6 consecutive times, this time tail points to the last element of the array, and when another element is queued, the tail should be 8. The tail has exceeded the length of the array, and this
(tail = (tail + 1) & (ELEMENTS.LENGTH-1)
operation, tail points to the first position. When tail and head point to the same location, this indicates that the queue is full and will be expanded. is the following method.
Doublecapacity ();
If the queue has been dissatisfied. The array is continually recycled. These operations are done through bit operations.

The main summary is the operation of the bitwise operators in Java. Java bitwise operators are not tight can improve operational efficiency, but also have unexpected effects (Java.util.ArrayDeque has a good embodiment), in the follow-up will be illustrated.

Bitwise operators for Java (>>,<<,>>>,&,|)

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.