Java bit operators in-depth understanding

Source: Internet
Author: User


There is a class of operators in Java that operate on binary. They are &, |, ^, ~, >>, <<, >>> several bit operators. Whether the initial value is based on what kind of binary, it will be converted into binary bits

Operation. The following is a brief explanation of these operators.

&:

The function is to perform the bitwise AND operation of the operator on both sides of a binary expression. This operation is in the same number of bits (bit) as the unit. The rules for the operation are: only two operands are 1 o'clock. The output is only 1. Whether

is 0, the scale is as follows:

12 binary is 1100

5 binary is 0101

Then the binary of the & 5 is 0100, then the complete arithmetic is 5 = 4;

|

The function is to perform a bitwise ' or ' operation on both sides of the operator in binary representations. This operation is in the same number of bits (bit) as the unit.

The rule of action is that only if two operands are 0 o'clock, the result of the output is 0.

otherwise 1, the scale is as follows:

12 Binary is 1100

5 binary is 0101

Then 12 | The binary of 5 is 1101. Then the complete expression is & 5 = 13;

^ :

The function is to perform an ' XOR ' operation on two sides of the operator with binary representation of the operands, and this operation is in the same bit (bit) of the number.

The rules for an XOR operation are: only if the two operands are not the same. corresponding to the

lose The result is only 1, otherwise 0, the ratio is as follows:

12 Binary is 1100

5 binary is 0101

Then 12 | The binary of 5 is 1001. Then the complete expression is & 5 = 9;

~ :

the function of the ' inverse ' operator is to reverse the numbers: all 0 is 1,1 is 0, and the ratio is as follows:

12 Binary is 1100

The counter operation is 10000000 00000000 00000000 00001101

The complete expression is ~12 = 13

<< :

The left shift is to move the entire number of digits to the left by a number of bits , as shown in the following scale:

12 Binary is 1100

Then the binary of the << 1 is 11000, then the complete arithmetic is 1 = 24;

>> :

The right shift is to move the entire number of digits to the right by a number of bits , as shown in the following scale:

12 Binary is 1100

then the binary of >> 1 is 0110. Then the complete expression is >> 1 =6;

>>> :

unsigned Right move one . insufficient complement of 0. The scale is as follows:

12 Binary is 1100

then the binary of >> >1 is 0110, then the complete arithmetic is 1 = 6;


Off-topic: bit operation is a binary-based operation, involving the knowledge contains the original code, anti-code, complement, here to do a small explanation,

For the original code. is the binary representation of the current number, such as 1 of the original code is 1000 0001.

For the inverse code, the inverse of the positive number is itself. The inverse of a negative number is a binary reserved symbol bit. The inverse of the remaining bits, for example 1, is 1111 1110;

For the complement, positive number of anti-code, complement, the original code is the same, negative complement is in its anti-code based on +1, such as 1 of the complement is 1111 1111.


In the original code of the JDK. Many of the initial values are computed by bit operations, and bit operations have many characteristics that can play a role in linearly growing data. And for some operations, bit operation is the most direct and simplest method.

Java bit operators in-depth understanding

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.