Java Learning Notes-The difference between ">>>" and ">>"

Today,I have met a problem when I wrireJava program,that is the difference between bitwise operator ">>>" and ">>".In C++,there is only ">>"  bitwise operator,which performs a bitwise shift on their operands.But the right-hand operand must not be negative and must be a value that is strictly less than the number of bits in the result.Otherwise,the operation is undefine.

However,in Java,it is premited that the right-hand operator is a negative,and there are two different types bitwise shift  operator.">>>" is called logical shift,unlike an arithmetic shift ">>",it does not perserve a number's sign bit,every bits in operand simply moved a given numbers of bit positions,and the vacant bit positions are filled,usually with zeros and possibly one. In a word,">>>" will fill the vacant positions with zeros,and ">>" or called arithmetic shift will fill the vacant position with sign position.we use a negative number -10 as an example.

-10 True code :                             10000000 00000000 00000000 00001010

Inverse code :                               11111111 11111111 11111111 11110101

Complement code:                      11111111 11111111 11111111 11110110

arithmetic shift ">>" :                   11111111 11111111 11111111 11111011           

-5's Inverse code :                       11111111 11111111 11111111 11111010

-5's True code :                            10000000 00000000 00000000 00000101            ----->          -5

logical shift ">>>" :                      01111111 11111111 11111111 11111011

Inverse code :                              01111111 11111111 11111111 11111011

True code :                                   01111111 11111111 11111111 11111011           ----->     2147483643

As we can see from the upper's display of the left shift operations,logical shift operation(">>>") shifts all the bits of the operand 10,and fill the vacant position with zeros.Unlike the logical shift , arithmetic shift operation shifts all the bits the operand as well,but it fills the vacant position with sign position---->1.And,there is another things we should pay attention to,that is both logical shift operation and arithmetic shift operation shift the Complement Code rather than True code.The reasons why it shift Complement Code is very simple that the values are stored in memoy as Complement Code.There is also a program to illustrate what I showed in the front of this bolgs.

public class FirstSample
{
    public static void main(String[] args) {
        int a=(-10>>>1);
        int b=(-10>>1);
        System.out.println("-10"+"   "+Integer.toBinaryString(-10));
        System.out.println("a:"+"   "+a+"    "+Integer.toBinaryString(a));
        System.out.println("b"+"    "+b+"   "+Integer.toBinaryString(b));
    }
}

Result:
-10:   -10    11111111111111111111111111110110
a:   2147483643    1111111111111111111111111111011
b:    -5   11111111111111111111111111111011

Actually,there are lots of differences about bitwise operation between programing language C++  and Java,which may cause some problem's when we use it in Java.The programing language C++ only have logical shift operator ">>",it fills the vacant position with 0 and operand must not be a negative.But the programing language Java have both logical operand and arithmetic operand,and allow right hand operand could be a nagetive.In many situations,logical shift operation is an efficient ways to perform multiplication or division of unsigned intergers by powers of two.

猜你喜欢

转载自blog.csdn.net/qq_23557345/article/details/88243623