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

Programmer Humor

23376 readers
1255 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
top 50 comments
sorted by: hot top controversial new old
[–] KindaABigDyl@programming.dev 183 points 6 days 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;
[–] xthexder@l.sw0.com 41 points 6 days ago* (last edited 6 days 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)
[–] kiri@ani.social 44 points 6 days ago

You beat me to it!

load more comments (2 replies)
[–] ricecake@sh.itjust.works 135 points 6 days ago (5 children)

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

[–] laranis@lemmy.zip 94 points 6 days ago* (last edited 6 days ago) (8 children)

01111111 = true

11111111 = negative true = false

[–] StellarSt0rm@lemmy.world 48 points 6 days ago (3 children)
[–] ThatGuy46475@lemmy.world 28 points 6 days ago (3 children)
[–] foofiepie@lemmy.world 22 points 6 days ago

100001111 = maybe not

load more comments (2 replies)
load more comments (2 replies)
[–] VonReposti@feddit.dk 34 points 6 days ago (4 children)

What if it's an unsigned boolean?

[–] ivanovsky@lemm.ee 25 points 6 days ago

Cthulhu shows up.

load more comments (3 replies)
load more comments (6 replies)
load more comments (4 replies)
[–] Lucien@mander.xyz 140 points 6 days ago (1 children)
[–] mmddmm@lemm.ee 154 points 6 days ago (10 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 (10 replies)
[–] WanderingThoughts 95 points 6 days ago (2 children)

string boolEnable = "True";

[–] jenesaisquoi@feddit.org 14 points 5 days ago (1 children)

Wait until you hear about alignment

[–] bastion@feddit.nl 3 points 4 days ago

The alignment of the language and the alignment of the coder must be similar on at least one metric, or the coder suffers a penalty to develop for each degree of difference from the language's alignment. This is penalty stacks for each phase of the project.

So, let's say that the developer is a lawful good Rust ~~zealot~~ Paladin, but she's developing in Python, a language she's moderately familiar with. Since Python is neutral/good, she suffers a -1 penalty for the first phase, -2 for the second, -3 for the third, etc. This is because Rust (the Paladin's native language) is lawful, and Python is neutral (one degree of difference from lawful), so she operates at a slight disadvantage. However, they are both "good", so there's no further penalty.

The same penalty would occur if using C, which is lawful neutral - but the axis of order and chaos matches, and there is one degree of difference on the axis of good and evil.

However, if that same developer were to code in Javascript (chaotic neutral), it would be at a -3 (-6, -9...) disadvantage, due to 2 and 1 degree of difference in alignment, respectively.

Malbolge (chaotic evil), however, would be a -4 (-8, -12) plus an inherent -2 for poor toolchain availability.

..hope this helps. have fun out there!

[–] skisnow@lemmy.ca 53 points 6 days 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)

load more comments (6 replies)
[–] 30p87@feddit.org 91 points 6 days ago (10 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?

[–] ricecake@sh.itjust.works 38 points 6 days ago

Sounds like a compiler problem to me. :p

load more comments (9 replies)
[–] acockworkorange@mander.xyz 39 points 6 days ago (1 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.

[–] ZILtoid1991@lemmy.world 20 points 5 days ago (1 children)

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).

[–] acockworkorange@mander.xyz 30 points 5 days ago (6 children)

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

load more comments (6 replies)
[–] catnip@lemmy.zip 56 points 6 days ago (1 children)

Wait till you find out about alignment and padding

load more comments (1 replies)
[–] midori_matcha@lemmy.world 21 points 5 days ago (3 children)
load more comments (3 replies)
[–] mavu@discuss.tchncs.de 12 points 5 days ago

This reminds me that I actually once made a class to store bools packed in uint8 array to save bytes.

Had forgotten that. I think i have to update the list of top 10 dumbest things i ever did.

[–] houseofleft@slrpnk.net 10 points 5 days ago (7 children)

Wait till you here about every ascii letter. . .

load more comments (7 replies)
[–] Subverb@lemmy.world 39 points 6 days ago* (last edited 6 days 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)
[–] JakenVeina@lemm.ee 27 points 6 days ago (6 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 (6 replies)
[–] borokov@lemmy.world 34 points 6 days ago (1 children)
load more comments (1 replies)
[–] glitchdx@lemmy.world 24 points 6 days ago (1 children)

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

[–] MystikIncarnate@lemmy.ca 28 points 6 days ago (1 children)

It's 7 bits....

Pay attention. 🤪

[–] JasonDJ@lemmy.zip 31 points 6 days ago (1 children)

7 bytes! Look at Mr. Moneybags here!

load more comments (1 replies)
[–] pelya@lemmy.world 33 points 6 days ago (2 children)

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

load more comments (2 replies)
[–] savedbythezsh@sh.itjust.works 14 points 5 days ago (6 children)

Are you telling me that no compiler optimizes this? Why?

[–] Anders429@programming.dev 35 points 5 days ago

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.

[–] timhh@programming.dev 24 points 5 days ago (4 children)

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.

load more comments (4 replies)
load more comments (4 replies)
load more comments
view more: next ›