Im writing an Arduino program to control 12 leds connected to Shift registers.
I want to process bytes in certain way, and im not sure how to do it.
lets say i have two bytes:
byte 1 = 01110100 (the 7th and 8th bit will always be 0)
byte 2 = 01110100
now i want the 1st and 2nd bit of byte 2, to be combined with byte 1, ie
byte 1 =01110101
lastly i want byte 2 to be shifted up, without the first 2 bits, ie
byte 2=11010000
You need the bitwise AND & and the bitwise OR | (note that is a single & | )
byte a = b01110100;
byte b = b01110100;
byte c = b & b11000000; // split of 2 highest bits c = 0100000
c = c >> 6; // move them to position 0,1 c = 00000001;
a = a | c ; // a = 01110101;
b = b & b11111100; // force bit 0, 1 to zero b = 01110100;
b = b << 2; // shift 2 positions, b = 11010000