Yes, of course it is. But generally we just use bit operations directly on the bits (called bitops) using bitwise operators defined for all discrete integer types.
For instance, if you need to test the 5th least significant bit you can use x &= 1 << 4
to have x
just to have the 5th bit set, and all others set to zero. Then you can use if (x)
to test if it has been set; C doesn't use a boolean
type but assumes that zero is false and any other value means true. If you store 1 << 4
into a constant then you have created a "(bit) mask" for that particular bit.
If you need a value 0
or 1
then you can use a shift the other way and use x = (x >> 4) & 1
. This is all covered in most C books, so I'd implore you to read about these bit operations there.
There are many Q/A's here how to split integers into bytes, see e.g. here. In principle you can store those in a char
, but if you may require integer operations then you can also split the int into multiple values. One problem with that is that an int
is just defined to at least store values from -32768 to 32767. That means that the number of bytes in an int can be 2 bytes or more.
In principle it is also possible to use bit fields but I'd be hesitant to use those. With an int
you will at least know that the bits will be stored in the least significant bits.