Using one bit in byte

Good day!
Since both boolean and byte (variable type) are 1 byte (data volume), it's logical in a way of memory saving to use byte for 8 bits (1 byte) instead of 8 booleans (8 bytes).
So, for example a=158 and it stands for next array of bits - 10011110. If I want to change for example bit #5 (start from 0), any way to do it with inbuilt C++ functions?

byte a=158; //10011110
SomeMagic to set bit #5 to 1: 10011110->10111110
print(a); //190

if you are talking of doing that in the arduino IDE environment, you can use:
bitset() and bitclear()

hope that helps...

2 Likes

byte a=158; //10011110
//        76543210
a = a | 0b00100000;
print(a); //190

1 Like
a |= (1<<5);  // set bit - of integer max  (larger variables will need more measures)
a &= ~(1<<5);  // clear bit

edit:
a quick google search will bring you here:

1 Like

Thanks everyone, you are great help)

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.