changing specific bits of data in a single byte of data

76 views Asked by At

I want to be able to change specific bits in a byte from a 1 to a 0

for example

uint8_t _data = 0b11111111; (Change bit 1 and 7 to 0)
_data = 0b10111110;

I was planning on building a input system with a shift register and being able to find a way to change specific bits in a byte of data would basically complete this mini project for me. I've been stuck trouble shooting for days over this so any help would be very appreciated. Thanks in advance.

1

There are 1 answers

0
Dharmik On

It is simple bit manipulation. For example, if you want to set bit 3 you can do it like this.

uint8_t _data = 0b00000000; (Change bit 1 and 7 to 0)
_data |= 0b00000001;    // use |= to set bit it will not change other bits

// after this the _data will be 00000001
//Now if you want to reset 3 bits without changing other bits use &= ~

_data &= ~(0b00000001);
// after this the _data will be 00000000