boolean bloat
I first thought you wrote boolean float, not sure if that’s even worse.
Are you telling me that no compiler optimizes this? Why?
Consider what the disassembly would look like. There’s no fast way to do it.
It’s also unnecessary since 8 bytes is a negligible amount in most cases. Serialization is the only real scenario where it matters. (Edit: and embedded)
It would be slower to read the value if you had to also do bitwise operations to get the value.
But you can also define your own bitfield types to store booleans packed together if you really need to. I would much rather that than have the compiler do it automatically for me.
Well there are containers that store booleans in single bits (e.g.
std::vector<bool>
- which was famously a big mistake).But in the general case you don’t want that because it would be slower.
Why is this a big mistake? I’m not a c++ person
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
Good 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
pragma(pack) {
int a:1, b:1, … h:1;
}
IIRC.
Back in the day when it mattered, we did it like
#define BV00 (1 << 0) #define BV01 (1 << 1) #define BV02 (1 << 2) #define BV03 (1 << 3) ...etc #define IS_SET(flag, bit) ((flag) & (bit)) #define SET_BIT(var, bit) ((var) |= (bit)) #define REMOVE_BIT(var, bit) ((var) &= ~(bit)) #define TOGGLE_BIT(var, bit) ((var) ^= (bit)) ....then... #define MY_FIRST_BOOLEAN BV00 SET_BIT(myFlags, MY_FIRST_BOOLEAN)
Okay. Gen z programmer here. Can you explain this black magic? I see it all the time in kernel code but I have no idea what it means.
With embedded stuff its still done like that. And if you go from the arduino functionss to writing the registers directly its a hell of a lot faster.
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.
True.
Well storing that would only take half a bit.
It’s far more often stored in a word, so 32-64 bytes, depending on the target architecture. At least in most languages.
No it isn’t. All statically typed languages I know of use a byte. Which languages store it in an entire 32 bits? That would be unnecessarily wasteful.
Wait till you find out about alignment and padding
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.
The 8-bit Intel 8051 family provides a dedicated bit-addressable memory space (addresses 20h-2Fh in internal RAM), giving 128 directly addressable bits. Used them for years. I’d imagine many microcontrollers have bit-width variables.
bit myFlag = 0;
Or even return from a function:
bit isValidInput(unsigned char input) { // Returns true (1) if input is valid, false (0) otherwise return (input >= '0' && input <= '9'); }
Nothing like that in ARM. Even microcontrollers have enough RAM that nobody cares, I guess.
Tell this to the LPC1114 I’m working with. Did you ever run a multilingual GUI from 2kbytes RAM on a 256x32 pixel display?
ARM has bit-banding specifically for this. I think it’s limited to M-profile CPUs (e.g. v7-M) but I’ve definitely used this before. It basically creates a 4-byte virtual address for every bit in a region. So the CPU itself can’t “address” a bit but it can access an address backed by only 1 bit of SRAM or registers (this is also useful to atomically access certain bits in registers without needing to use SW atomics).
We could go the other way as well: TI’s C2000 microcontroller architecture has no way to access a single byte, let alone a bit. A Boolean is stored in 16-bits on that one.
And, you can have pointers to bits!
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.
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
You beat me to it!
I set all 8 bits to 1 because I want it to be really true.
01111111 = true
11111111 = negative true = false
00001111 = maybe
10101010 = I don’t know
00000001 00000000 00001111 10101010
100001111 = maybe not
Schrödingers Boolean
What if it’s an unsigned boolean?
Cthulhu shows up.
Common misconception… Unsigned booleans (ubool) are always 16-bits.
Super true.
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 .
Why do alternative facts always gotta show up uninvited to the party? 🥳
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
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
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.
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.
Pl/1 did it right:
Dcl 1 mybools, 3 bool1 bit(1) unaligned, 3 bool2 bit(1) unaligned, … 3 bool8 bit(1) unaligned;
All eight bools are in the same byte.
string boolEnable = "True";
Violence