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

7

u/Irverter Jul 08 '26 edited Jul 08 '26

Sounds like your reciting theory without understanding actual usage.

There's no overflow or underflow because you're not incrementing nor decrementing the value. You will explicitly assign the value of the state you want it to go to. It's the same as using a bit range for state, except it's easier to handle.

Edit: If you're scared of accidentally over/underflowing then just use a wrapper function. Something like:

uint8_t setState(uint8_t nextState) {
    state = nextState;
    return state;
}

uint8_t getState() {
    return state;
}

With state being a global variable (or withing a struct, however you organized it) that is only set/read within those functions. Plus whatever check you feel like doing for safety, like masking nextState so only the 6-7 bits your' obessed with are used.

5

u/F1nnyF6 Jul 08 '26

Exactly what I was thinking. There are a lot of buzzwords and references to technical topics, but a core lack of understanding