So I'm having trouble trying to isolate a certain amount of bits through masking. Essentially I have a certain amount of bits I want to mask (let's call it offset) from a single byte (so 8 bits). There are 2 kinds of masks I need help with:
First one: say the offset = 4 and I have a byte with binary 1110 0001. I want to mask the last bits of size offset such that I can have a final byte of 1110 0000 (so mask the last 4 bits).
Second one: say the offset is 3 and I have a byte with binary 1011 0010. I want to now mask the first few bits of size offset so that I have a final byte of 0001 0010.
I've pasted the code I have so far. Currently it does not work for the first mask I want to create as it masks the first few bits not the last. I'm not sure if I'm creating the mask correctly.
uint8_t mask = (1 << offset) - 1;
byte = (byte & mask);
uint8_t mask = ((1 << offset) - 1) << pos;and use the complement of mask (~mask) to clear the masked bits from the bytebyte = (byte & ~mask);. - isrnickoffsetand and mask you expect from it? Like:offset = 3givesmask = 00011110(setting the left 3 bits to 0, keep the next 4 bits, set the rightmost bit to 0). - the busybee