r/C_Programming • • Jul 07 '26

If statement checking a bool array?

Hi,

I need a custom sized bit vector so uint8_t won't suffice so the idea was to just initiate a bool array with size so then say i want to compare it. Ex**. the bool array is 6 bits and tried with `if (bool_var = 011001) { // do something}`, i suspect it comes just compares to an int given it compiles and wont run. Any idea on how to make it work?**

0 Upvotes

53 comments sorted by

View all comments

Show parent comment

10

u/F1nnyF6 Jul 08 '26

2 bits is an unimaginably minor amount of "waste". Like literally so negligible there are 0 scenarios where you would ever care about it. Additionally, there is physically no possible way to waste LESS than that if you are using only 6 bits for something. The smallest amount of data your cpu can work with is a byte. So any other way of representing 6 bits will always waste even more.

On the topic of your actual problem, I am not sure why you would choose to use a bitfield for a state machine. Typically the point of a state machine is that the states are mutually exclusive, so I don't know why you would need a variety of different boolean values instead of a single number representing each state. The idiomatic way to represent a state machine is with an enum, which under the hood is just a number I.e. 0 1 2 3. Are you sure a state machine is what you are after?