Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
We’ve talked about the fact that there are 8 bits in a byte, 16 bits in a short, and so on. You might have occasion to turn individual bits on or off. For instance you might find yourself writing code for your new Java enabled toaster, and realize that due to severe memory limitations, certain toaster settings are controlled at the bit level. For easier reading, we’re showing only the last 8 bits in the comments rather than the full 32 for an int).
Bitwise NOT Operator: ~
This operator ‘flips all the bits’ of a primitive.
int x = 10; // bits are 00001010 x = ~x; // bits are now 11110101
The next three operators compare two primitives on a bit by bit basis, and return a result based on comparing these bits. We’ll use the following example for the next three operators: