Java bitwise operations (shift, bitwise AND, Or, exclusive or, non)

Source: Internet
Author: User
Tags bitwise operators

The bitwise operators provided by Java include: shift left (<), shift right (>), unsigned shift right (>), bitwise AND (&), bitwise OR (|), non-bit (~ ), Bitwise OR (^), except for non (~ Except for the unary operator, all others are binary operators.

1. Shift left (<)

Test1. Remove 5 two places to the left:

Package com. xcy; public class test {public static void main (string [] ARGs) {system. Out. println (5 <2); // The running result is 20 }}

The running result is 20, but how is the Program executed?

First, convert 5 to a binary representation (in Java, integers are of the int type by default, that is, 32 bits ):

0000 0000 0000 0000 0000 0000 0000 and then shifted left after 2 places, the low position is supplemented with 0:

0000 0000 0000 0000 0000 0000 0001 0100 converted to 10 hexadecimal format 20

2. Right Shift (>). The same is true for right shift, but the direction is different (feeling the same as not saying)

System. Out. println (5> 2); // The running result is 1.

Or convert 5 to a binary representation first:

0000 0000 0000 0000 0000 0000 0000 then shifted to the right two places, and the high position was 0:

0000 0000 0000 0000 0000 0000 0000

3. unsigned right shift (>>>)

We know that the int type occupies 32 bits in Java, which can represent a positive number or a negative number. After a positive number is converted to a binary value, the highest bit is 0, and the highest bit of a negative number is 1.

For example, after-5 is converted to binary:

1111 1111 1111 1111 1111 1111 1111 (the highest bit was used to indicate positive and negative when I first came into contact with binary data. At that time, I couldn't figure it out .. The result is a positive number -_-)

We move 5 three places to the right,-5 three places to the right, and unsigned three places to the right, respectively:

Package COM. xcy; public class test {public static void main (string [] ARGs) {system. out. println (5> 3); // The result is 0 system. out. println (-5> 3); // The result is-1system. out. println (-5 >>> 3); // The result is 536870911 }}

Let's take a look at its shift process (the result can be converted to binary for comparison ):

5. Convert to binary: 0000 0000 0000 0000 0000 0000 0000 0101

5 after the three digits are shifted to the right, the result is 0. 0. The binary value is 0000 0000 0000 0000 0000 0000 0000 0000 // (use 0 to complete the bitwise)

-5 to binary: 1111 1111 1111 1111 1111 1111 1111 1011

-5 shifts three places to the right and the result is-1. The binary value of-1 is:
1111 1111 1111 1111 1111 1111 1111 1111 // (use 1 for filling)

-5 The result 536870911 after the unsigned three-digit right shift is converted to binary:
0001 1111 1111 1111 1111 1111 1111 1111 // (use 0 for filling)

After the result is converted to binary, we can find that the positive number shifts right, the high value is supplemented with 0, the negative number shifts right, and the high value is supplemented with 1. When the negative number shifts right without a symbol, position with 0 (naturally, it is changed from negative to positive)

Note: Here I am talking about the situation of right shift and high position makeup. Shifts positive or negative values to the left, and uses 0 to fill the low position. (Self-test)

4. bitwise AND (&)

Package com. xcy; public class test {public static void main (string [] ARGs) {system. Out. println (5 & 3); // The result is 1 }}

The old rule is to convert both the two operands and the result into binary for comparison:

5 to binary: 0000 0000 0000 0000 0000 0000 0000 0101

3 to binary: 0000 0000 0000 0000 0000 0000 0000 0011

Bytes -------------------------------------------------------------------------------------

1 to binary: 0000 0000 0000 0000 0000 0000 0000 0001

Bitwise AND: the nth of the first operand is located in the nth bit of the second operand. If both are 1, the nth result is also 1; otherwise, it is 0.

5. Bit or (|)

Package com. xcy; public class test {public static void main (string [] ARGs) {system. Out. println (5 | 3); // The result is 7 }}

5 to binary: 0000 0000 0000 0000 0000 0000 0000 0101

3 to binary: 0000 0000 0000 0000 0000 0000 0000 0011

Bytes -------------------------------------------------------------------------------------

7 to binary: 0000 0000 0000 0000 0000 0000 0000 0111

Bitwise OR operation: the nth bit of the first operand is located in the nth bit of the second operand. As long as one is 1, the nth result is also 1; otherwise, it is 0.

6. bitwise OR (^)

Package com. xcy; public class test {public static void main (string [] ARGs) {system. Out. println (5 ^ 3); // The result is 6 }}

5 to binary: 0000 0000 0000 0000 0000 0000 0000 0101

3 to binary: 0000 0000 0000 0000 0000 0000 0000 0011

Bytes -------------------------------------------------------------------------------------

6 to binary: 0000 0000 0000 0000 0000 0000 0000 0110

Bitwise OR: the nth digit of the first operand is opposite to the nth digit of the second operand. The Nth vertex of the result is also 1; otherwise, it is 0.

7. Non-bits (~ ) Bitwise non-mona1 Operator

Package com. xcy; public class test {public static void main (string [] ARGs) {system. Out. println (~ 5); // The result is-6 }}

5 to binary: 0000 0000 0000 0000 0000 0000 0000 0101

Bytes -------------------------------------------------------------------------------------

-6 to binary: 1111 1111 1111 1111 1111 1111 1111 1010

Non-bits: If the nth bit of the operand is 1, the nth bit of the result is 0, and vice versa.


Derived from bitwise operators:

& = Bitwise AND value assignment

| = By bit or value

^ = Do not assign values by bit

>>= Right shift value

>>>= Unsigned right shift assignment

<= Shifts the value left.

And + =.

For example:

Package COM. xcy; public class test {public static void main (string [] ARGs) {int A = 5A & = 3; system. out. println (a); // The result is 1 }}

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.