I have a solution with a bit fields. Now your bool is 1 byte :
struct Flags { bool flag0 : 1; bool flag1 : 1; bool flag2 : 1; bool flag3 : 1; bool flag4 : 1; bool flag5 : 1; bool flag6 : 1; bool flag7 : 1; };
Or for example:
struct Flags { bool flag0 : 1; bool flag1 : 1: int x_cord : 3; int y_cord : 3; };
I watched a YouTube video where a dev was optimizing unity code to match the size of data that is sent to the cpu using structs just like this.
Now store the numbers (array):
0 0 0 1 0 1 1 2
think 8 bytes???
This guy never coded in KEIL C on an 8051 architecture. They actually use bit addressable RAM for booleans. And if you set the compiler to pass function parameters in registers, it uses the carry flag for the first bit or bool type parameter.
typedef struct { bool a: 1; bool b: 1; bool c: 1; bool d: 1; bool e: 1; bool f: 1; bool g: 1; bool h: 1; } __attribute__((__packed__)) not_if_you_have_enough_booleans_t;
This was gonna be my response to OP so I’ll offer an alternative approach instead:
typedef enum flags_e : unsigned char { F_1 = (1 << 0), F_2 = (1 << 1), F_3 = (1 << 2), F_4 = (1 << 3), F_5 = (1 << 4), F_6 = (1 << 5), F_7 = (1 << 6), F_8 = (1 << 7), } Flags; int main(void) { Flags f = F_1 | F_3 | F_5; if (f & F_1 && f & F_3) { // do F_1 and F_3 stuff } }
Why not
if (f & (F_1 | F_3)) {
? I use this all the time in embedded code.edit: never mind; you’re checking for both flags. I’d probably use
(f & (F_1 | F_3)) == (F_1 | F_3)
but that’s not much different than what you wrote.
You beat me to it!
Or just
std::bitset<8>
for C++. Bit fields are neat though, it can store weird stuff like a 3 bit integer, packed next to booleansThat’s only for C++, as far as I can tell that struct is valid C
string boolEnable = "True";
Violence
Maybe json is named after Jason Voorhees
Wait till you here about every ascii letter. . .
what about them?
Ascii needs seven bits, but is almost always encoded as bytes, so every ascii letter has a throwaway bit.
Some old software does use 8-Bit ASCII for special/locale specific characters. Also there is this Unicode hack where the last bit is used to determine if the byte is part of a multi-byte sequence.
Let’s store the boolean there then!!
That boolean can indicate if it’s a fancy character, that way all ASCII characters are themselves but if the boolean is set it’s something else. We could take the other symbol from a page of codes to fit the users language.
Or we could let true mean that the character is larger, allowing us to transform all of unicode to a format consisting of 8 bits parts.
ASCII was originally a 7-bit standard. If you type in ASCII on an 8-bit system, every leading bit is always
0
.(Edited to specify context)
At least ASCII is forward compatible with UTF-8
Is ascii base-7 fandom’s strongest argument…
In the industrial automation world and most of the IT industry, data is aligned to the nearest word. Depending on architecture, that’s usually either 16, 32, or 64 bits. And that’s the space a single Boolean takes.
That’s why I primarily use booleans in return parameters, beyond that I’ll try to use bitfields. My game engine’s tilemap format uses a 32 bit struct, with 16 bit selecting the tile, 12 bit selecting the palette, and 4 bit used for various bitflags (horizontal and vertical mirroring, X-Y axis invert, and priority bit).
Bit fields are a necessity in low level networking too.
They’re incredibly useful, I wish more people made use of them.
I remember I interned at a startup programming microcontrollers once and created a few bitfields to deal with something. Then the lead engineer went ahead and changed them to masked ints. Because. The most aggravating thing is that an int size isn’t consistent across platforms, so if they were ever to change platforms to a different word length, they’d be fucked as their code was full of platform specific shenanigans like that.
/rant
Yeah. I once had to do stuff to code that had bit-fields like that and after a while, realised (by means of StackOverflow) that that part is UB and I had to go with bitwise operations instead.
Undefined Behavior…?
Ok, I recalled wrong, it was unspecified
I always use stdint.h so that my types are compatible across any mcu. And it makes the data type easily known instead of guessing an i t size
Or you could just use Rust
Good rant.
I set all 8 bits to 1 because I want it to be really true.
You jest, but on some older computers, all ones was the official truth value. Other values may also have been true in certain contexts, but that was the guaranteed one.
TIL, 255 is the new 1.
Aka -1 >> 1 : TRUE
But only if you really mean it. If not, it’s a syntax error and the compiler will know.
I was programming in assembly for ARM (some cortex chip) and I kid you not the C program we were integrating with required 255, with just 1 it read it as false
01111111 = true
11111111 = negative true = false
So all this time true was actually false and false was actually true ?
Depends on if you are on a big endian or little endian architecture.
Come on man, I’m not gonna talk about my endian publicly
negative true = negative non-zero = non-zero = true.
What if it’s an unsigned boolean?
Cthulhu shows up.
Common misconception… Unsigned booleans (ubool) are always 16-bits.
Super true.
Why do alternative facts always gotta show up uninvited to the party? 🥳
Could also store our bools as floats.
00111111100000000000000000000000
is true and10111111100000000000000000000000
is negative true.Has the fun twist that true & false is true and true | false is false .
00001111 = maybe
Is this quantum computing? 😜
10101010 = I don’t know
0011 1111 = could you repeat the question
100001111 = maybe not
00000001 00000000 00001111 10101010
Schrödingers Boolean
Wait till you realise the size of SSD sectors
boolean bloat
I first thought you wrote boolean float, not sure if that’s even worse.
boolean root beer float
deleted by creator
Does anybody ever figure in parity when comparing bit sizes and all that jazz or are we only ever concerned with storage space?
You can’t store data in parity bits… so it’s irrelevant.
…or you can be coding assembler - it’s all just bits to me
3GPP has an interesting way of serialising bools on the wire with ASN.1
NULL OPTIONAL
meaning only the type would be stored if true, otherwise it won’t be set at all
That requires some form of self describing format and will probably look like a sparse matrix in the end.
if wasting a byte or seven matters to you, then then you need to be working in a lower level language.
It’s 7 bits…
Pay attention. 🤪
7 bytes! Look at Mr. Moneybags here!
Well when it comes to bytes, you could say I’m a bit of a millionaire myself.
Redundancy is nice in the event of bitflip errors
Is the redundancy used for bools? I mean in actual practice.
C/C++ considers an nonzero number, as your true value but false is only zero. This would allow you to guard against going from true to false via bit flip but not false to true.
Other languages like rust define 0 to be false and 1 to be true and any other bit pattern to be invalid for bools.iunno ¯_(ツ)_/¯