I'm trying to convert an int to a 4 byte unsigned ints. I was reading a post and I'm not sure what comparison does. I know that its a sub mask, but I'm not sure when to use the & and when to use the |. Lets use the number 6 as an example, if the LSB is the number 6. Why would we do
6 & 0xFF// I know that we are comparing it to 11111111
when do we use the OR operator? I'm still not sure how to use the & nor |
x & 0xFF
will set all bits of x
to zero except for the last byte (which stays the same). If you had used a bitwise or (|
), it would leave the bits of x
set, and set all the bits of the last byte to 1.
Typically, the comparison will be something like (x & 0xFF) == x
. This is to make sure that the first three bytes of x
are all 0.