17.2.09

Java's Bitwise Operators

Some notes on Java's bitwise operators, answering a question from one of my friend:

1. There are only three bitwise shift operators, <<, >>, and >>>. Remember there is no <<<
2. << and >> move only the bits other than the sign bit.
3. >>> works the way as >> does, except that the former one uses the sign bit as the top bit. Please try -1 >>> 1 to see what will happen.
4. When applying bitwise shift operators on negative integers, make sure you have translated the BITs into binary expression. Don't simply shift the bits as if you work on a positive value.


No comments: