this post was submitted on 15 May 2025
185 points (95.6% liked)

Programmer Humor

37140 readers
254 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 1 month ago* (last edited 1 month 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 1 month 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?

[–] homura1650@lemm.ee 1 points 1 month ago

I've used it a fair amount for memory mapped IO where the hardware defined bitfields. It is also useful when you have a data format with bitfields. I'd say it is also useful when your data does not respect byte boundaries, but the only time I've run into that involved the bit order being "backwards", which means that I still had to bittwidle things back together.

From a performance perspective, a cache line is only 64 bytes. Space in registers, low level memory caches, and memory throughout are all limited as well.

load more comments (3 replies)
load more comments (4 replies)