Java bit operators in-depth understanding

Source: Internet
Author: User
Tags bitwise bitwise operators


In Java, such operators exist for binary operations, they are &, |, ^, ~, >>, <<, >>> Several bitwise operators, regardless of the initial value is based on what the binary, will be converted into binary bits

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

&:

The function is to perform a bitwise AND operation on both sides of the operator in binary representations, and this operation is in the same bit (bit) as the number. The rule of operation is: only if two operands are 1 o'clock, the output is 1, no

is 0, the example 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, and this operation is in the same bit (bit) of the number. The rule of action is: only if two operands are 0 o'clock, the result of the output is 0,

otherwise 1, the example is as follows:

12 Binary is 1100

5 binary is 0101

Then 12 | The binary of 5 is 1101, then the full expression is & 5 = 13;

^ :

The function is to perform a bitwise ' XOR ' operation on both sides of the operator in binary representation, and this operation is in the same bit (bit) of the number. The rules for XOR operations are: only if the two operands are not the same, the corresponding

lose The result is only 1, otherwise 0, an example is as follows:

12 Binary is 1100

5 binary is 0101

Then 12 | The binary of 5 is 1001, then the full expression is & 5 = 9;

~ :

the function of the ' inverse ' operator is to reverse the numbers: all 0 are 1,1 is 0, and the example 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 all the digits of a number to the left by several bits , as shown in the following example:

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 all the digits of a number to the right by several bits , as shown in the following example:

12 Binary is 1100

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

>>> :

unsigned Right shift one , insufficient complement 0, examples are 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 including the original code, anti-code, complement, here to do a small explanation,

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

For the inverse code, the inverse of the positive number is itself, negative number of the inverse code is the binary reserved sign bit, the remaining bit counter, for example-1 of the inverse code 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, for example-1 of the complement is 1111 1111;


In the JDK's original code, there are many initial values are computed by bit operations, bit operations have many characteristics, can play a role in the linear growth of data, and for some operations, bit operation is the most direct and easiest method.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

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.