Wednesday, 29 October 2014

Checking the bits in Java

To check a certain bit in the position n is enabled or not, the follow sentence could be used:

boolean isBitNEnabled = (data & (1 << n) != 0)

For instance, the follow code could be used to check if the second bit is enabled or not.

boolean isBit2Enabled = (data & (1 << 2) != 0)

Source

No comments:

Post a Comment