this post was submitted on 15 May 2025
183 points (95.5% liked)

Programmer Humor

36747 readers
391 users here now

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

Rules:

founded 5 years ago
MODERATORS
top 27 comments
sorted by: hot top controversial new old
[–] [email protected] 40 points 1 month ago (3 children)

I use bit masks, suck it! (Really though, programming on an embedded CPU might be reasonable to do this, depending on the situation, but on a PC, trying to not waste bits wastes time)

[–] [email protected] 13 points 1 month ago (1 children)

exactly! it is more costly for your pc cpu to check for a bit inside a byte, than just get the byte itself, because adresses only point to bytes

[–] [email protected] 9 points 1 month ago (1 children)

Store 8 bits in the same byte then 👌

[–] [email protected] 3 points 1 month ago (1 children)

Wrong direction!

Store only bits using word-length ints (32 bits in most modern architectures), and program everything to do math using arrays of 32 int-bits to numbers!

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

Oh man! That took me down memory lane!

I once had to reverse engineer a database to make an invoice integration. They had an int named flags. It contained all status booleans in the entire system. Took me a while to figure that one out.

[–] [email protected] 3 points 1 month ago

We've all been there, friend. The bit arrays can't hurt you now.

[–] mindbleach 6 points 1 month ago (1 children)

Even on 6502, the BIT command is useless 99% of the time, and AND ~which_bit is the right answer.

Interestingly the Intel MCS-51 ISA did have several bit-addressable bytes. Like a weird zero page.

[–] [email protected] 3 points 1 month ago (1 children)

Only because 6502 has no BIT immediate -- only BIT zero page and BIT absolute. But the contemporary z80 and gameboy cpu too have dedicated bit instructions, e.g. BIT c,6 (set z flag to bit 6 of register c).

[–] mindbleach 3 points 1 month ago

I think it's intended for checking the same bit in multiple bytes. You load the mask instead of the data.

So much 6502 ASM involves turning your brain inside-out... despite being simple, clever, and friendly. Like how you can't do a strided array sensibly because there's no address register(s). There is no "next byte." Naively, you want separate varied data at the same index is separate arrays. Buuut because each read address is absolute, you can do *(&array+1)[n], for free.

What I really miss on NES versus Game Boy is SWAP.

[–] [email protected] 4 points 1 month ago

Unlikely. Most of the time on modern hardware, you're going to be cache-limited, not cycle-limited. Checking one bit in a register is insanely fast.

[–] [email protected] 26 points 1 month ago (3 children)

I've been working on disassembling some 8-bit code from the 90s. Fuckers returned bits from functions using the overflow bit. Nuts.

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

What era was that device? Some old games on NES had to use all kinds of quirks like this to overcome hardware limitation.

[–] [email protected] 1 points 1 month ago

It's in an AlphaSmart. I'm working through disassembling the ROM to add some new features.

[–] [email protected] 3 points 1 month ago

Where do you go to talk about such things? Could be fun to have a retro reversing community.

[–] [email protected] 1 points 1 month ago
[–] [email protected] 24 points 1 month ago* (last edited 1 month ago) (2 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.

[–] [email protected] 17 points 1 month ago

This is both the right and wrong answer

[–] [email protected] 2 points 1 month ago (3 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?

[–] kora 5 points 1 month ago* (last edited 1 month ago)

Yes, firmware running on bare metal requires good resource management. My current development board processor contains 512KB SRAM. That's equivalent to half of the size of an average PDF.

[–] [email protected] 2 points 1 month ago* (last edited 1 month 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.

[–] [email protected] 1 points 1 month 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

[–] [email protected] 1 points 3 weeks 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.

[–] [email protected] 16 points 1 month ago (1 children)

If you want to optimize to this point, do some embedded development. It's somewhat fun to work at such a low level (testing tends to be annoying though)

[–] [email protected] 18 points 1 month ago

Embedded SW dev here; don't listen to this, fly you fools!

[–] [email protected] 15 points 1 month ago (1 children)

Solution? Store 8 booleans in 1 byte.

[–] [email protected] 7 points 1 month ago* (last edited 1 month ago) (1 children)

If you put them in the right order, you can store 10 bools in a byte.

kompreshun.

[–] [email protected] 7 points 1 month ago

Odd of you to be using base 7.