this post was submitted on 15 May 2025
1157 points (98.6% liked)

Programmer Humor

23402 readers
1586 users here now

Welcome to Programmer Humor!

This is a place where you can post jokes, memes, humor, etc. related to programming!

For sharing awful code theres also Programming Horror.

Rules

founded 2 years ago
MODERATORS
 
(page 2) 50 comments
sorted by: hot top controversial new old
[–] [email protected] 182 points 1 week ago (4 children)
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;
[–] [email protected] 44 points 1 week ago

You beat me to it!

[–] [email protected] 41 points 1 week ago* (last edited 1 week ago) (1 children)

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 booleans

load more comments (1 replies)
load more comments (2 replies)
[–] [email protected] 140 points 1 week ago (1 children)
[–] [email protected] 154 points 1 week ago (15 children)

And compiler. And hardware architecture. And optimization flags.

As usual, it's some developer that knows little enough to think the walls they see around enclose the entire world.

load more comments (15 replies)
[–] [email protected] 135 points 1 week ago (5 children)

I set all 8 bits to 1 because I want it to be really true.

[–] [email protected] 94 points 1 week ago* (last edited 1 week ago) (8 children)

01111111 = true

11111111 = negative true = false

[–] [email protected] 48 points 1 week ago (3 children)
[–] [email protected] 1 points 5 days ago

Is this quantum computing? 😜

[–] [email protected] 28 points 1 week ago (3 children)
[–] [email protected] 22 points 1 week ago

100001111 = maybe not

load more comments (2 replies)
load more comments (1 replies)
[–] [email protected] 34 points 1 week ago (4 children)

What if it's an unsigned boolean?

[–] [email protected] 25 points 1 week ago

Cthulhu shows up.

load more comments (3 replies)
load more comments (6 replies)
load more comments (4 replies)
[–] [email protected] 95 points 1 week ago (1 children)

string boolEnable = "True";

[–] [email protected] 40 points 1 week ago (1 children)
load more comments (1 replies)
[–] [email protected] 91 points 1 week ago (6 children)

Then you need to ask yourself: Performance or memory efficiency? Is it worth the extra cycles and instructions to put 8 bools in one byte and & 0x bitmask the relevant one?

[–] [email protected] 38 points 1 week ago

Sounds like a compiler problem to me. :p

[–] [email protected] 23 points 1 week ago (4 children)

A lot of times using less memory is actually better for performance because the main bottleneck is memory bandwidth or latency.

load more comments (4 replies)
load more comments (4 replies)
[–] [email protected] 56 points 1 week ago (1 children)

Wait till you find out about alignment and padding

load more comments (1 replies)
[–] [email protected] 53 points 1 week ago (6 children)

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)

[–] [email protected] 1 points 6 days ago* (last edited 6 days ago)

Edit - oops, responded to wrong comment...

load more comments (5 replies)
[–] [email protected] 39 points 1 week ago* (last edited 1 week ago) (7 children)

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'); }

load more comments (7 replies)
[–] [email protected] 39 points 1 week ago (8 children)

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.

load more comments (8 replies)
[–] [email protected] 34 points 1 week ago (1 children)
load more comments (1 replies)
[–] [email protected] 33 points 1 week ago (2 children)

std::vector<bool> fits eight booleans into one byte.

[–] [email protected] 2 points 6 days ago
auto v = std::vector<bool>(8);
bool* vPtr = v.data;
vPtr[2] = true;
// KABOOM !!!

I've spent days tracking this bug... That's how I learned about bool specialisation of std::vector.

load more comments (1 replies)
[–] [email protected] 27 points 1 week ago (11 children)

It's far more often stored in a word, so 32-64 bytes, depending on the target architecture. At least in most languages.

load more comments (11 replies)
[–] [email protected] 24 points 1 week ago (1 children)

if wasting a byte or seven matters to you, then then you need to be working in a lower level language.

[–] [email protected] 28 points 1 week ago (1 children)

It's 7 bits....

Pay attention. 🤪

[–] [email protected] 31 points 1 week ago (1 children)

7 bytes! Look at Mr. Moneybags here!

load more comments (1 replies)
[–] [email protected] 22 points 1 week ago* (last edited 1 week ago) (1 children)

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;
};
load more comments (1 replies)
[–] [email protected] 22 points 1 week ago (1 children)

Joke’s on you, I always use 64 bit wide unsigned integers to store a 1 and compare to check for value.

load more comments (1 replies)
[–] [email protected] 21 points 1 week ago (3 children)
load more comments (3 replies)
load more comments
view more: ‹ prev next ›