c++ - Understanding bitwise operations - shifting and AND -
uint8_t payload[] = { 0, 0 }; pin5 = analogread(a0); payload[0] = pin5 >> 8 & 0xff; payload[1] = pin5 & 0xff; this code xbee library published andrewrapp on github. wondering how bitwise operation worked. suppose pin 5 gets analog value of 256 using particle photon board comes in 12bit format text 000100000000. payload[0] last 8 bits ie 00000000, or value after shifting ie, 00000001? becomes value in payload[1]?
i want add 4-bit code of on using bitmask first 4 bits in array followed data bits. can & payload[1] 0x1 payload[1] this?
the code in example reverser content of pin5's 2 bytes payload array: significant byte placed payload[0] , least significant byte placed payload[1].
if, example, pin5 0x0a63, payload contain 0x63, 0x0a.
if pin5 has 12-bit value, can use 4 significant bits store four-bit value of own. make sure upper bits zeroed out, use 0x0f mask instead of 0xff:
payload[0] = pin5 >> 8 & 0x0f; // ^ now can move data upper 4 bits | operator:
payload[0] |= myfourbits << 4;
Comments
Post a Comment