What does AND 0xFF do?

后端 未结 7 1233
我在风中等你
我在风中等你 2020-12-23 02:12

In the following code:

short = ((byte2 << 8) | (byte1 & 0xFF))

What is the purpose of &0xFF? Because other somes

相关标签:
7条回答
  • 2020-12-23 02:50

    Assuming your byte1 is a byte(8bits), When you do a bitwise AND of a byte with 0xFF, you are getting the same byte.

    So byte1 is the same as byte1 & 0xFF

    Say byte1 is 01001101 , then byte1 & 0xFF = 01001101 & 11111111 = 01001101 = byte1

    If byte1 is of some other type say integer of 4 bytes, bitwise AND with 0xFF leaves you with least significant byte(8 bits) of the byte1.

    0 讨论(0)
提交回复
热议问题