this post was submitted on 15 May 2025
193 points (96.2% liked)

Programmer Humor

36208 readers
340 users here now

Post funny things about programming here! (Or just rant about your favourite programming language.)

Rules:

founded 5 years ago
MODERATORS
you are viewing a single comment's thread
view the rest of the comments
[–] jsomae@lemmy.ml 26 points 3 weeks ago* (last edited 3 weeks ago) (6 children)

Use bit-fields:

struct {
  bool a : 1;
  bool b : 1;
  bool c : 1;
  //...
};

Edit: careful not to use a 1-bit signed int, since the only values are 0 and -1, not 0 and 1. This tripped me up once.

[–] Strawberry@lemmy.blahaj.zone 2 points 3 weeks ago (4 children)

In a world where a bigger memory chip is more expensive by only a few cents where this would be most useful, is this feature still relevant?

[–] jsomae@lemmy.ml 2 points 3 weeks ago* (last edited 3 weeks ago) (1 children)

Yes, because cache optimization is still important. Also useful to keep the size of packets down, to reduce the size of file formats, and anywhere that you use hundreds of thousands of instances of the struct.

[–] Strawberry@lemmy.blahaj.zone 1 points 3 weeks ago

For the packet size and fils format issues, it seems like this language feature would be less reliable than bit shifting or masking, given that different implementations may store the bits in a different order or not compactly

load more comments (2 replies)
load more comments (3 replies)