r/C_Programming • u/Yha_Boiii • 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
2
u/ByMeno Jul 07 '26
I assume in 8 bits you only want to use the 6 afaik most used ones are shift left or right to trumcate unused bits or use bit mask for last 6 bits then compare the reason probably it didn't work is you ignored the the two bits which are still important for comparing there should be zero if you want to compare and 011001 is not same as 0b011001 you should but 0b in front to tell the compiler its in bits
10
u/Daveinatx Jul 08 '26
There's a few things that's missing, so excuse my assumptions. It sounds similar to my FPGA work, reading an arbitrary number of bits, that are packed together over PCI/PCIe or DMA transfers.
If this is correct, then what you show above is not going to work in any normal means. What you need to do is to learn bit manipulation. So you'll need to learn to use bitwise operators to mask and shift over six bits at a time.
It may seem weird at first, but once you think it out logically, it'll make sense. Just think of a huge one bit array. And in doing so, see how you will be traversing through your 8-bit array six bits at a time. Takes a little bit of thought, but I know you can do it.
1
u/dkopgerpgdolfg Jul 07 '26
uint8_t has sufficient size for your example. There are larger int types too. Only if you expect to need more than the largest available type, then you need an alternative.
011001 is not understood as binary literal. Try 0b11001 (which probably is supported). (But in any case it doesn't make sense to compare such a number to a bool array).
With the bool array you're thinking of, you need to compare each digit separately, with loops etc. . For small bool arrays there are some different ways, but if it's small enough for that, see the first paragraph.
And if arrays are needed, making it an array of eg. uint8_t (where each index holds 8bit) is worth considering too.
2
u/Vollink Jul 08 '26
Consider what everyone above is saying about a byte being a minimum. Let's do it the hard way...
My approach to what you are asking (not what you are claiming to do), is to use one character like this...
1
u/Wertbon1789 Jul 08 '26
There can be a lot of theory involved, but at the end of the day, a computer is not a magical device, and most of the time you're better off going with practical design choices, and see what sticks.
In this case, don't try to crunch your problem down into the bit range. Literally, just use a uint32_t, define some bits in there to be your states, and that's it. That's really how I've seen it being done, from internal APIs to the state machine built into USB, it's literally just an integer type, and you check some bits in there. It's really unlikely you'll ever exceed 32 bits of states and not have a horrible design anyway, so that might be something to consider. Also don't try to squeeze everything into it, e.g. if you want to encode state information that is by nature not binary, but can really have n different states, but are also mutually exclusive, like encoding in what state a system overall is (is it starting up, running, stopping, sleeping, whatever), encode that somewhere else than your bit field.
In the case you described if you want 6 bits of information, either just use a uint8_t, and waste 2 bits of it, or go with a bit field declaration of 6 bits, and maybe use the other 2 bits for something else. Do note though, this is unlikely to make your stuff really more space efficient or faster, something something alignment.
1
u/sciencekm Jul 08 '26
I could be wrong, but in your situation, you might just want to avoid bit operations until you become more familiar with it. For now, let the compiler do the work for you. You can do something like:
struct state {unsigned s0:1;unsigned s1:1;unsigned s2:1;unsigned s3:1;unsigned s4:1;unsigned s5:1;unsigned s6:1;} state_t;Now, your 'if' statements become:
if (a.s0 && !a.s1) ...and assignments become:
a.s3 = 1;a.s5 = 0;Increment/decrement would be:
a.s1++;a.s4--;The compiler will perform the increment/decrement without affecting ("overflowing" to) neighboring bits (which seems to be one of your concerns).
1
u/Traveling-Techie Jul 08 '26
I’m confused by your details, but some big picture stuff: memory has become unbelievably cheap and plentiful since C was invented, so be ok with wasting it. Use the operators and libraries available and write new helper functions if necessary.
1
u/TiredEngineer-_- Jul 08 '26 edited Jul 08 '26
It sounds like youre just wanting to check the bottom 6 bits, or X bits of an arbitrary number. As others said, youll need to learn bitmasking:
If you're trying to make something space efficient, most systems arent letting you read 6 bits of a byte, usually the smallest any architechture is getting you nowadays is a byte. (Saving 2 bits anyways wont do much unless youre declaring thousands of arrays :P)
1000 'types' of 1 byte length with 2 bits per byte saved = 8kB used, 2kb (kilobit) wasted, which is 250 B (bytes) wasted. If youre trying to save 250 B, youre either too constrained or something somewhere else is oversized imo.
If youre looking for 'simplicity', or somewhat avoiding bit masking, you can do this (if your system supports / likes byte-size page reads)
So the bitfield_B would be a 1 byte struct on a 1 byte boundary.
Would be the memory layout of
And you would need to do
To access and modify
is UB. accessing pointers to these bitfields is UB altogether. The machine cannot deduce bit- offsets to memory, since it all lives in bytes.
Initializing with a number would be like:
Which is a pain to write, so maybe you expose a macro to cover for you (no error check here):
...
gdb : x/b &x should show something like:
To the console, since youre inspecting the memory.
However, this is all really tedious to setup (and I may not even done so correctly), just to avoid some of the bitmasking youd need to know anyways to begin doing the macro and stuff I setup above...
You could simply write:
No structs, no packing, no forced alignment, no macros, just masking and moving on. I did 7<<2 here to bump the 'command key' to the upper 6 bits. 7 is 111 in binary. So you move it to be 0b00011100, which is still in the upper 6 bits.
1
u/WittyStick Jul 08 '26 edited Jul 08 '26
C23 has
_BitInt(N)types which could be used for this if your bit vector sizes are statically known. The compiler will do most of the work for you. You can compare them with==, and can initialize from literals up toULL(unsigned long long). Larger bitvectors might require more complex initialization.The maximum
Nis implementation defined and is specified byBITINT_MAXWIDTHfrom<limits.h>. The standard mandates a 64-bit minimum, but implementations support much larger widths - GCC supports64kiband Clang supports8Mib.https://godbolt.org/z/99Gd6GGd9
1
u/Paul_Pedant Jul 08 '26
Doing an assignment in the test (with =) instead of a comparison (with ==) is not going to be helping.
Assigning decimal 4609 to a bool variable is not so good either. 011001 is an octal numeric initialiser (evaluated as an integer), not a bit map, although it truncates to 0b00000001 when assigned to a bool (because the 001 part represents nine bits).
You probably should not rely on a bool value for a numeric anyway. A bool is false if zero, true if non-zero. A compiler could restrict a bool to 0 or 1, or 0 and 255 if it felt like it.
An array of 6 bools is 48 bits. You cannot subdivide a bool variable.
1
u/Educational-Paper-75 Jul 08 '26
Code first, optimize later. Don't worry about memory use initially. Sure you may use each bit of an uint8_t separately and have functions to set and unset any of the bits, but it will require shifting and masking. And if you have 256 or less different states you don't even need to set/unset individual bits and each state id fits nicely in an uint8_t. As long as you know what you're doing the implementation is straightforward.