[Turn]java bit arithmetic (1)

Source: Internet
Author: User

http://blog.csdn.net/xiaochunyong/article/details/7748713

The bitwise operators provided by Java are: Left (<<), right Shift (>>), unsigned Right shift (>>>), Bit and (&), bit or (|), bit non (~), bitwise XOR (^), except for bits (~), which are unary operators. All the others are two-dollar operators.

1. Shift Left (<<)

Test1, move 5 to the left 2 bits:

[Java]View PlainCopy
    1. Package com.xcy;
    2. Public class Test {
    3. public static void Main (string[] args) {
    4. System.out.println (5<<2); The result of the operation is
    5. }
    6. }

The result is 20, but how does the program work?

First, the 5 is converted to a 2-binary representation (in Java, integers are by default the int type, or 32-bit):

0000 0000 0000 0000 0000 0000 0000 0101 Then shift left 2 bit, low 0:

0000 0000 0000 0000 0000 0000 0001 0100 Converted to 10 binary 20

2, right Shift (>>), right shift the same, but the direction is not the same (feel and not say the same)

[Java]View PlainCopy
    1. System.out.println (5>>2); Operation result is 1

or first turn 5 into a 2 binary representation:

0000 0000 0000 0000 0000 0000 0000 0101 Then right-shift 2-bit, high-fill 0:

0000 0000 0000 0000 0000 0000 0000 0001

3, unsigned Right shift (>>>)

We know that in Java, the int type occupies 32 bits, can represent a positive number, or it can represent a negative number. A positive number is converted to binary with a maximum of 0, and the binary of negative numbers is up to 1

For example, 5 is converted into binary after:

1111 1111 1111 1111 1111 1111 1111 1011 (at the beginning of contact with the binary, I do not know that the highest bit is used to denote the positive and negative points, it was not always possible to understand. What you get is a positive number-_-).

We move the 5 right 3 bits, 5 to the right 3 bits and the unsigned right shift 3 bits respectively:

[Java]View PlainCopy
  1. Package com.xcy;
  2. Public class Test {
  3. public static void Main (string[] args) {
  4. System.out.println (5>>3); The result is 0 .
  5. System.out.println (-5>>3); The result is -1 .
  6. System.out.println (-5>>>3); The result is 536870911 .
  7. }
  8. }



Let's take a look at the shift process (which can be compared by converting the results into binary):

5 conversion into binary: 0000 0000 0000 0000 0000 0000 0000 0101

5 The binary with the result of 0,0 3-bit right shift is: 0000 0000 0000 0000 0000 0000 0000 0000//(complement with 0)

-5 conversion into binary: 1111 1111 1111 1111 1111 1111 1111 1011

-5 Right-shift 3-bit results in binary for -1,-1:1111 1111 1111 1111 1111 1111 1111 1111//(with 1 for complement)

-5 Unsigned right-shifted 3-bit result 536870911 converted to binary: 0001 1111 1111 1111 1111 1111 1111 1111//(with 0 for complement)

After the result is converted into binary, we can find that positive shift, high with 0 complement, negative right shift, high with 1 complement, when negative use unsigned right shift, with 0 of the position (naturally, the negative number becomes a positive number)

Note: The author is here to say the right shift, high-level complement situation. Positive or negative shifts left, and low is 0. (Self-test)

4, Bit and (&)

[Java]View PlainCopy
    1. Package com.xcy;
    2. Public class Test {
    3. public static void Main (string[] args) {
    4. System.out.println (5 & 3); Result is 1
    5. }
    6. }

or old routine, convert 2 operands and results to binary for comparison:

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

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

-------------------------------------------------------------------------------------

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

Bits and: The nth bit of the first operand in the second operand if all is 1, then the result of Nth is also 1, otherwise 0

5, bit or (|)

[Java]View PlainCopy
    1. Package com.xcy;
    2. Public class Test {
    3. public static void Main (string[] args) {
    4. System.out.println (5 | 3); //result is 7
    5. }
    6. }

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

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

-------------------------------------------------------------------------------------


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

Bit or operation: the nth bit of the first operand is at the nth digit of the second operand as long as there is a 1, then the result of Nth is also 1, otherwise 0

6, Bit XOR or (^)

[Java]View PlainCopy
    1. Package com.xcy;
    2. Public class Test {
    3. public static void Main (string[] args) {
    4. System.out.println (5 ^ 3); Result is 6
    5. }
    6. }

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

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

-------------------------------------------------------------------------------------


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

Bitwise XOR: The nth position of the first operand is the opposite of the nth bit of the second operand, then the nth of the result is also 1, otherwise 0

7, bit non (~) bit is not a unary operator

[Java]View PlainCopy
    1. Package com.xcy;
    2. Public class Test {
    3. public static void Main (string[] args) {
    4. SYSTEM.OUT.PRINTLN (~5); Result is-6
    5. }
    6. }

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

-------------------------------------------------------------------------------------

6 conversion to binary: 1111 1111 1111 1111 1111 1111 1111 1010

Bit non: the nth bit of the operand is 1, then the nth bit of the result is 0, and vice versa.

Derived from bitwise operator operators are:

&= Bitwise AND Assignment

|= Bitwise OR Assignment

^= bitwise non-assigned value

>>= Right Shift Assignment

>>>= Unsigned Right Shift assignment

<<= Assignment Shift Left

and + = a concept only.

As an example:

[Java]View PlainCopy
      1. PACKAGE&NBSP;COM.XCY;&NBSP;&NBSP;
      2.   
      3. public class test {  
      4.     public static  void main (string[] args)  {  
      5.         int a = 5&NBSP;&NBSP;
      6.         a  &= 3;&NBSP;&NBSP;
      7.          system.out.println (a); //result is 1  
      8.     }  
      9. }&NBSP;

[Turn]java bit Operations (1)

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.