Java Left Shift Operator Example

This Java example demonstrates how to use the left shift operator (<<) in Java with an example.

Java Left Shift 

The left shift operator, <<, shifts all of the bits in value to the left a specified number of times. It has this general form:
value << num
Here, num specifies the number of positions to left-shift the value in value. That is, the << moves all of the bits in the specified value to the left by the number of bit positions specified by num.

Java Left Shift Example

The following program demonstrates this concept:
package net.javaguides.corejava.operators.bitwise;

public class ByteShift {
    public static void main(String args[]) {
        byte a = 64, b;
        int i;
        i = a << 2;
        b = (byte)(a << 2);
        System.out.println("Original value of a: " + a);
        System.out.println("i and b: " + i + " " + b);
    }
}
Output:
Original value of a: 64
i and b: 256 0

Comments