Difference between >>> and >>

前端 未结 7 1071
情歌与酒
情歌与酒 2020-11-22 07:13

What is the difference between >>> and >> operators in Java?

相关标签:
7条回答
  • 2020-11-22 07:48

    The logical right shift (v >>> n) returns a value in which the bits in v have been shifted to the right by n bit positions, and 0's are shifted in from the left side. Consider shifting 8-bit values, written in binary:

    01111111 >>> 2 = 00011111
    10000000 >>> 2 = 00100000
    

    If we interpret the bits as an unsigned nonnegative integer, the logical right shift has the effect of dividing the number by the corresponding power of 2. However, if the number is in two's-complement representation, logical right shift does not correctly divide negative numbers. For example, the second right shift above shifts 128 to 32 when the bits are interpreted as unsigned numbers. But it shifts -128 to 32 when, as is typical in Java, the bits are interpreted in two's complement.

    Therefore, if you are shifting in order to divide by a power of two, you want the arithmetic right shift (v >> n). It returns a value in which the bits in v have been shifted to the right by n bit positions, and copies of the leftmost bit of v are shifted in from the left side:

    01111111 >> 2 = 00011111
    10000000 >> 2 = 11100000
    

    When the bits are a number in two's-complement representation, arithmetic right shift has the effect of dividing by a power of two. This works because the leftmost bit is the sign bit. Dividing by a power of two must keep the sign the same.

    0 讨论(0)
  • 2020-11-22 07:50

    The right shift logical operator (>>> N) shifts bits to the right by N positions, discarding the sign bit and padding the N left-most bits with 0's. For example:

    -1 (in 32-bit): 11111111111111111111111111111111
    

    after a >>> 1 operation becomes:

    2147483647: 01111111111111111111111111111111
    

    The right shift arithmetic operator (>> N) also shifts bits to the right by N positions, but preserves the sign bit and pads the N left-most bits with 1's. For example:

    -2 (in 32-bit): 11111111111111111111111111111110
    

    after a >> 1 operation becomes:

    -1: 11111111111111111111111111111111
    
    0 讨论(0)
  • 2020-11-22 07:52

    Read more about Bitwise and Bit Shift Operators

    >>      Signed right shift
    >>>     Unsigned right shift
    

    The bit pattern is given by the left-hand operand, and the number of positions to shift by the right-hand operand. The unsigned right shift operator >>> shifts a zero into the leftmost position,

    while the leftmost position after >> depends on sign extension.

    In simple words >>> always shifts a zero into the leftmost position whereas >> shifts based on sign of the number i.e. 1 for negative number and 0 for positive number.


    For example try with negative as well as positive numbers.

    int c = -153;
    System.out.printf("%32s%n",Integer.toBinaryString(c >>= 2));
    System.out.printf("%32s%n",Integer.toBinaryString(c <<= 2));
    System.out.printf("%32s%n",Integer.toBinaryString(c >>>= 2));
    System.out.println(Integer.toBinaryString(c <<= 2));
    
    System.out.println();
    
    c = 153;
    System.out.printf("%32s%n",Integer.toBinaryString(c >>= 2));
    System.out.printf("%32s%n",Integer.toBinaryString(c <<= 2));
    System.out.printf("%32s%n",Integer.toBinaryString(c >>>= 2));
    System.out.printf("%32s%n",Integer.toBinaryString(c <<= 2));
    

    output:

    11111111111111111111111111011001
    11111111111111111111111101100100
      111111111111111111111111011001
    11111111111111111111111101100100
    
                              100110
                            10011000
                              100110
                            10011000
    
    0 讨论(0)
  • 2020-11-22 07:54

    They are both right-shift, but >>> is unsigned

    From the documentation:

    The unsigned right shift operator ">>>" shifts a zero into the leftmost position, while the leftmost position after ">>" depends on sign extension.

    0 讨论(0)
  • 2020-11-22 07:56

    >> is arithmetic shift right, >>> is logical shift right.

    In an arithmetic shift, the sign bit is extended to preserve the signedness of the number.

    For example: -2 represented in 8 bits would be 11111110 (because the most significant bit has negative weight). Shifting it right one bit using arithmetic shift would give you 11111111, or -1. Logical right shift, however, does not care that the value could possibly represent a signed number; it simply moves everything to the right and fills in from the left with 0s. Shifting our -2 right one bit using logical shift would give 01111111.

    0 讨论(0)
  • 2020-11-22 08:00

    >>> is unsigned-shift; it'll insert 0. >> is signed, and will extend the sign bit.

    JLS 15.19 Shift Operators

    The shift operators include left shift <<, signed right shift >>, and unsigned right shift >>>.

    The value of n>>s is n right-shifted s bit positions with sign-extension.

    The value of n>>>s is n right-shifted s bit positions with zero-extension.

        System.out.println(Integer.toBinaryString(-1));
        // prints "11111111111111111111111111111111"
        System.out.println(Integer.toBinaryString(-1 >> 16));
        // prints "11111111111111111111111111111111"
        System.out.println(Integer.toBinaryString(-1 >>> 16));
        // prints "1111111111111111"
    

    To make things more clear adding positive counterpart

    System.out.println(Integer.toBinaryString(121));
    // prints "1111001"
    System.out.println(Integer.toBinaryString(121 >> 1));
    // prints "111100"
    System.out.println(Integer.toBinaryString(121 >>> 1));
    // prints "111100"
    

    Since it is positive both signed and unsigned shifts will add 0 to left most bit.

    Related questions

    • Right Shift to Perform Divide by 2 On -1
    • Is shifting bits faster than multiplying and dividing in Java? .NET?
    • what is c/c++ equivalent way of doing ‘>>>’ as in java (unsigned right shift)
    • Negative logical shift
    • Java’s >> versus >>> Operator?
    • What is the difference between the Java operators >> and >>>?
    • Difference between >>> and >> operators
    • What’s the reason high-level languages like C#/Java mask the bit shift count operand?
      • 1 >>> 32 == 1
    0 讨论(0)
提交回复
热议问题