Java – changing bit values with byte

I have some data in the field type byte (I save eight inputs in the byte, and each bit is an input)

Solution

Set the seventh bit to 1:

b = (byte) (b | (1 << 6));

To set the sixth bit to zero:

b = (byte) (b & ~(1 << 5));

(the bit position is actually based on 0, so that's why the "seventh bit" maps to 1 < 6 instead of 1 < 7

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>