0

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.

unkow
  • 1
  • Does this answer your question? [How do I set, clear, and toggle a single bit?](https://stackoverflow.com/questions/47981/how-do-i-set-clear-and-toggle-a-single-bit) – HTF Aug 05 '22 at 07:29

1 Answers1

0

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
Dharmik
  • 44
  • 4