What is the difference between >>= and >>>= in Java?

Asked

Viewed 326 times

14

Reading a book about the Java language, I came across some operators I had never noticed before. They are: >>, <<, >>=, <<=, >>>, <<<, >>>= and <<<=. I suppose knowing the difference between only two of these operations being >>= and >>>=, it is possible to clarify on all other.

What difference between >>= and >>>=? In which situation they can be applied?

1 answer

13


There’s nothing oop about that. These are bit operators, in which case are the compound assignment operators, then it will take this variable apply the operator and the result will be saved in the variable itself. You can say he does the operation inplace.

The >>= moves the bits to the right and fills with 0 the ones on the left that become "empty". Already the >>>= does the same but disregarding the signal.

class HelloWorld {
    public static void main (String[] args) {
        int x = -100;
        x >>= 2;
        System.out.println(x);
        x = 100;
        x >>= 2;
        System.out.println(x);
        x = -100;
        x >>>= 2;
        System.out.println(x);
        x = 100;
        x >>>= 2;
        System.out.println(x);
    }
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

  • Cool, with this edition clarified better! Thank you.

  • @Guilhermenascimento is, made running, thank you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.