Reputation: 119
int id = ((reply[0] & 0x3));
id = (id<<7) | (reply[1] & 0x7F);
Suppose I have a reply array storing 2 byte reply[0] and reply[1]. I wanna read the last two bits in the first byte and all the bits in the second byte, then adding them together
why the code above is going to work, can anyone explain?
Upvotes: 2
Views: 616
Reputation: 7844
Convert to binary and you will get your answer.
binary of 3 is 11. if you use logical &
with 11 you will end up only with the last two bits. That's the answer to your first part.
For the second part, I don't think it will do what you intended it too. You can use logical AND
(for carry) and logical XOR
to add individual bits.
Try to figure it out ^_^
Upvotes: 1
Reputation: 726649
This code is not doing what you described: instead of taking the second byte in its entirety, it cuts out its last seven bits (the & 0x7F
part; 0x7F
is binary 01111111
), and concatenates them to the two last two bits of the first number (the << 7
part).
Upvotes: 2