Posts

Showing posts from March, 2015

Bitwise right shift operators in Java

C/C++ supports only one right shift operator ">>" which is a positive or unsigned integers In java we have two operator 1)Right shift Signed operator(>>)  In Java, the operator ‘>>’ is signed right shift operator. All integers are signed in Java, and it is fine to use >> for negative numbers. The operator ‘>>’ uses the sign bit (left most bit) to fill the trailing positions after shift. If the number is negative, then 1 is used as a filler and if the number is positive, then 0 is used as a filler. For example, if binary representation of number is  1 0….100, then right shifting it by 2 using >> will make it  11 …….1. See following Java programs as example ‘>>’ class Test {      public static void main(String args[])  {         int x = - 4 ;         System.out.println(x>> 1 );           int y = 4 ;         System.out.println(y>> 1 );        }    } Output : -2 2 2)Unsigned ri