Hacker News

Favorites Setup
Comment by cogman10 | original | C for Rust programmers
[−]cogman10 · 2026-10-10 Sat 13:25 UTC · link
The way C handles (prior to C23) booleans is pretty close to how you'd handle booleans in assembly.

CPUs don't have types, everything is integers or floats. You do your work on registers which have fixed sizes. CPUs have built in instructions for "is this register not zero" which leaks into C. 1 is true in C, but so is 2.

Also, single bits are rarely used for booleans because it requires more CPU power to extract a single bit. Everything is byte aligned at a minimum.

When doing something like network code, if you want to store a bunch of booleans you are typically going to either pack them into a byte, or you'll burn the extra bits and send a single byte for the boolean value. Typically this was flags and masks.

[−]xhroot · 2026-10-10 Sat 13:47 UTC · link
> single bits are rarely used for booleans

SQL Server still has no boolean type and groups bits in the same row into a byte if possible.

[−]quikoa · 2026-10-10 Sat 15:32 UTC · link
> Also, single bits are rarely used for booleans because it requires more CPU power to extract a single bit.

Not necessarily, on modern CPUs memory access is often the bottleneck. If you have many bits storing these in a bitarray can be quite beneficial for performance.

[−]streganil · 2026-10-11 Sun 10:41 UTC · link
I feel this comment misses some important context; in assembler, you might return a boolean value from a function in a register, but you also might just set a flag (which is much more similar to the "one bit value"). A flag register is very much a set of booleans, not an integer.

> single bits are rarely used for booleans because it requires more CPU power to extract a single bit

I don't believe this to be true; (on x86) `cmp X, 0` is exactly the same amount of operations as `test X, pow2`. One of them is a `sub` and one of them is an `and`. I believe this is how it works on all important CPU architectures of today back to the early microcomputers (although I don't know how the minicomputers handle this, so it's possible that booleans being integers makes more sense on a PDP-11 or a honeywell).