On the Operation of Moving Bits in java: greater thangreater thangreater than

  • 2021-07-01 07:37:31
  • OfStack

1. Concepts

< < Left shift operator, left shift is followed by 0, num < < 1, equivalent to num times 2
> > Right shift operator, right shift is preceded by 1 or 0, num > > 1, equivalent to num divided by 2
> > > Unsigned right shift is to fill 0 in front, ignoring sign bits, and all vacancies are filled with 0
In addition, whether you move 32 bits left or right, it is equivalent to not moving, or the original value.

In fact, when the java virtual machine executes this code, it is executed as follows:
5 > > (n%32)--- > Results
You have n=32; So 5 > > 32 is 5 > > (32%32)-- > 5 > > 0;

2. Test the code


public class Test{
 public Test(){
 System.out.println("============= Arithmetic right shift  >> ===========");
 int i=0xC0000000;
 System.out.println(" Before displacement: i= "+i+" = "+Integer.toBinaryString(i)+"(B)");
 i=i>>28;
 System.out.println(" After displacement: i= "+i+" = "+Integer.toBinaryString(i)+"(B)");
 
 System.out.println("---------------------------------");
 
 int j=0x0C000000;
 System.out.println(" Before displacement: j= "+j+" = "+Integer.toBinaryString(j)+"(B)");
 j=j>>24;
 System.out.println(" After displacement: j= "+j+" = "+Integer.toBinaryString(j)+"(B)");
 
 System.out.println("\n");
 System.out.println("============== Logical right shift  >>> =============");
 int m=0xC0000000;
 System.out.println(" Before displacement: m= "+m+" = "+Integer.toBinaryString(m)+"(B)");
 m=m >>> 28;
 System.out.println(" After displacement: m= "+m+" = "+Integer.toBinaryString(m)+"(B)");
 
 System.out.println("---------------------------------");
 
 int n=0x0C000000;
 System.out.println(" Before displacement: n= "+n+" = "+Integer.toBinaryString(n)+"(B)");
 n=n>>24;
 System.out.println(" After displacement: n= "+n+" = "+Integer.toBinaryString(n)+"(B)");
 
 System.out.println("\n");
 System.out.println("============== Modulus taking of shift symbols ===============");
 int a=0xCC000000;
 System.out.println(" Before displacement: a= "+a+" = "+Integer.toBinaryString(a)+"(B)");
 System.out.println(" Arithmetic right shift 32 : a="+(a>>32)+" = "+Integer.toBinaryString(a>>32)+"(B)");
 System.out.println(" Logical right shift 32 : a="+(a>>>32)+" = "+Integer.toBinaryString(a>>>32)+"(B)");
 
 System.out.println(" Arithmetic right shift 64 : a="+(a>>64)+" = "+Integer.toBinaryString(a>>64)+"(B)");
 System.out.println(" Logical right shift 64 : a="+(a>>>64)+" = "+Integer.toBinaryString(a>>>64)+"(B)");
 
 }
 
 public static void main(String[] args){
 new Test();
 }
 
}
 

Run results:

= = = = = = = = = = = = = = = arithmetic right shift > > ===========
Before Shift: i =-1073741824 =
After shift: i =-4 = 11111111111111111111111100 (B)

Before Shift: j = 201326592 =
After shift: j = 12 = 1100 (B)

= = = = = = = = = = = = = = = = = = logical right shift > > > =============
Before displacement: m =-1073741824 = 11000000000000000000000000000000000000 (B)
After shift: m = 12 = 1100 (B)

Before Shift: n = 201326592 =
After shift: n = 12 = 1100 (B)

= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
Before Shift: a =-872415232 =
Arithmetic Right Shift 32: a=-872415232 = 11001100000000000000000000000000000000 (B)
Logic Right Shift 32: a=-872415232 =
Arithmetic Right Shift 64: a=-872415232 = 110011000000000000000000000000000000 (B)
Logic Right Shift 64: a=-872415232 = 110011000000000000000000000000000000000000 (B)

3. Why is there no unsigned left shift

You can think about this problem for a while, and you should be able to come up with it. (Hint: No means no existence)


Related articles: