Compare byte values?

前端 未结 2 689
情话喂你
情话喂你 2021-01-07 18:03

I\'m curious to know why, when I compare a byte array with a value...

boolean match = ((data[0] & 0xFF) == 0xFE);

...retur

2条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-07 18:41

    I believe that it has to do with promoting 0xFF to an int with sign extension. In the first expression, 0xFE is also promoted to an int and therefore the result of data[0] & 0xFF is also an int and an int comparison is done.

    But in the second code sample, there is no operation being done and so there is no promotion to int. That is to say, that data[0] does not get promoted to int but 0xFE is an int.

提交回复
热议问题