How do I apply the bitwise shift operator in Java?

Question:

Java is said to have the bitwise shift operator << .
He, in theory, can shift a binary number to the left ( >> – to the right). For example, convert 1010 to 10100.
This is what I want to do: I am trying to write a simple program that will shift a number by a bit (left or right):

public class Binary {

    public static void main(String[] args) {
        int x = 0B1010; //запись числа в двоичной системе исчисления
        x << 1;
        System.out.println(x);
    }
}

But a compilation error is generated, no matter how I rewrite the code.

Answer:

Write the code correctly:

public class Binary {

    public static void main(String[] args) {
        int x = 0B1010; //запись числа в двоичной системе исчисления
        x = x << 1;
        System.out.println(x);
    }
}

And if you want to see it in binary, then output like this:

System.out.println(Integer.toBinaryString(x));
Scroll to Top