Java: Displacement operator

Source: Internet
Author: User

There are three displacement operators in Java for the int typeBinary representation of integersTo do the following:

1. "<<": Left shift operator, at the end of binary representationAdd "0", before the other bits are equivalent to moving left one, can be considered as"Multiply two"Operation.

For example, a = 10,a binary is represented as "0000 0000 0000 0000 0000 0000 0000 1010", "a << 1" means that the binary representation of a is shifted left one bit, resulting in "0000 0000 0000 0000 0000 0000 0001 0100 ", corresponding to the decimal" 20 ".

b =-10, the binary of B is represented as "1111 1111 1111 1111 1111 1111 1111 0110", "b<<1" represents the binary representation of B as a left shift, resulting in "1111 1111 1111 1111 1111 1111 111 0 1100 ", corresponding to the decimal"-20 ".

2. ">>": Right-shift operator, minus a binary representation of the last few, and add at the frontsign Bit,positive Add "0", negative number Add "1"。 ">>1" Remove the last one, ">>2" remove the last two digits.cannot simply be considered as "divided by two" operation.

For example, a = 9, "A>>1" represents the binary representation of a "0000 0000 0000 0000 0000 0000 0000 1001" Move right One bit, resulting in "0000 0000 0000 0000 0000 0000 0000 0100", Corresponds to the decimal "4"(not the "divided by two" operation).

B =-9, "b>>1" represents the binary representation of B "1111 1111 1111 1111 1111 1111 1111 0111" move right one bit, resulting in "1111 1111 1111 1111 1111 1111 1111 1011", corresponding to 10 The "-5" in the system.

3. "<<<": Unsigned Right shift operator, no longer considers symbol bit,Add "0" to the front. (the "0" sign in front of the positive number can be omitted)

For example, B =-9, "B >>> 1" represents the binary representation of B "1111 1111 1111 1111 1111 1111 1111 0111" move right one bit, resulting in "0111 1111 1111 1111 1111 1111 1111 1011 ", corresponding to the decimal" 2147483643 ".

public class Test {public    static void Main (string[] args) {        int a =-9;        System.out.println ("a =" + a);        System.out.println ("A binary representation:       " + integer.tobinarystring (a));        System.out.println ("");        "<<" left shift operator        int a1 = a << 1;        System.out.println ("a<<1 =" + A1);        SYSTEM.OUT.PRINTLN ("Binary representation after a<<1:  " + integer.tobinarystring (A1));        System.out.println ("");        ">>" right-shift operator        int a2 = a >> 1;        System.out.println ("a>>1 =" + A2);        SYSTEM.OUT.PRINTLN ("Binary representation after a>>1:  " + integer.tobinarystring (A2));        System.out.println ("");        Unsigned right-shift operator        int a3 = a >>> 1;        System.out.println ("a >>> 1 =" + A3);        SYSTEM.OUT.PRINTLN ("Binary representation after a>>>1:" + integer.tobinarystring (a >>> 1));}    }

  

Operation Result:

2018-01-02 18:51:21

Java: Displacement operator

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.