Bitwise operations in Java

Source: Internet
Author: User

Java bit operator

Bitwise operators are used to operate binary bits. Java provides bitwise operators as shown below:

Bitwise operator (>>,<, >>>,&, |, ^ ,~ ), In the bitwise operator, ~ The others are binary operators. The operation data can only be integral and character data.

 

Basic knowledge

Complement
All Integer types (except char) are signed integers. This means they can both represent positive numbers and negative numbers.

Java uses complement code to show the binary number. In the complement code table, the highest bit is the symbolic bit, the positive number is 0, and the negative number is 1. The Code population rules are as follows:

For the positive number, the maximum bit is 0, and the number of other data replicas is indicated in binary notation. For example, the complement code of + 42 is 00101010.
For a negative number, the bitwise side of the complement code of this number is reversed, and then 1 is added to the entire number, that is, the complement code of this number is obtained. For example, if the value of-42 is 11010110 (00101010, the bitwise result is 11010101 + 1 = 11010110)

The complement code is used to show the number. The complement code of 0 is the only one, and all are 00000000. (In the original code and reverse code table, the tables of + 0 and-0 are not unique. You can refer to the corresponding books ). And can be expressed as-1 in 111111 (this is also the difference between the complement code and the original code and the anti-code ).

Java bit operators:

~
Bitwise non (not) (mona1 Operation)

&
Bitwise AND (and)

|
Bitwise OR (OR)

^
Bitwise exclusive or (XOR)

>
Right Shift

>>>
Right Shift. The left blank places are filled with 0; unsigned right shift

<
Move left

& =
Bitwise AND value assignment

| =
Bitwise OR value assignment

^ =
Bitwise variance or value assignment

>>=
Shift value to the right

>>>=
Shifts the value to the right. The blank places on the left are filled with 0; the unsigned value is shifted to the left.

<=
Left shift assignment

Non-(not) by bit)

Bitwise is also called complement. The unary operator not "~" Returns the inverse of each bit of the number of operations. For example, if the number is 42, its binary code is:
0010 1010 42
After bitwise non-calculation
1101 0101

 

 

Bitwise AND (and)

Bitwise AND operator "&". If the number of both operations is 1, the result is 1. In other cases, the result is zero. See the following example:

0010 1010 42

& Amp; 0000, 1111

0000 1010 10

Bitwise OR (OR)

Bitwise OR operator "|". If any operation is 1, the result is 1. The following is an example:

0010 1010 42

| 0000 1111 15

0010 1111 47

Bitwise XOR or operator "^ ",

The result is 1 only when two comparison bits are different. Otherwise, the result is zero. When the difference is 1, the same result is 0. 1*0 = 1 0*1 = 1;

 

The following example shows the effect of the "^" operator. This example also shows a useful property of the XOR operator. Note that the second operation number has a digit of 1, and 42 corresponds to the corresponding bit of binary code. The second operation has a digit of 0, and the number of digits corresponding to the first operation remains unchanged. When bitwise operations are performed on some types, you will see the usefulness of this attribute.

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.