bit shift java code example

Example 1: right shift operator in java

int x  = 25;
System.out.println(x);	    //     1 1 0 0 1 : 25
System.out.println(x >> 2); //         1 1 0 : 6
System.out.println(x << 2); // 1 1 0 0 1 0 0 : 100
// Negative numbers are stored as two's complement
// and will trail right shifts with one
// Unsigned operators <<< >>> ignore sign and trail with 0

Example 2: >> vs >>> operators in java

>> is a right shift operator shifts all of the bits in a value to the 
right to a specified number of times.
int a =15;
a= a >> 3;
The above line of code moves 15 three characters right.

>>> is an unsigned shift operator used to shift right. The places which 
were vacated by shift are filled with zeroes

Tags:

Java Example