this post was submitted on 22 Jul 2025
407 points (98.1% liked)

Programmer Humor

25282 readers
652 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
 

I don't think that casting a range of bits as some other arbitrary type "is a bug nobody sees coming".

C++ compilers also warn you that this is likely an issue and will fail to compile if configured to do so. But it will let you do it if you really want to.

That's why I love C++

you are viewing a single comment's thread
view the rest of the comments
[–] Treczoks@lemmy.world 1 points 3 days ago (1 children)
[–] Valmond@lemmy.world 1 points 3 days ago (1 children)

You use it to "pack" bitfields, bytes etc together in structs/classes (wo functions), otherwise the computer usually align every variable on a 32bit boundary for speed.

[–] Treczoks@lemmy.world 1 points 3 days ago (1 children)

You don't need that pragma to pack bitfields.

[–] Valmond@lemmy.world 2 points 3 days ago* (last edited 3 days ago) (1 children)

With say a 3bit int, then a 2bit int and various char, int etc and so on you did have to use the pragma with gcc & visual around 2012 at least

[–] Treczoks@lemmy.world 2 points 2 days ago (1 children)

OK, I use the Keil ARM compiler, and never needed to push anything.

[–] Valmond@lemmy.world 1 points 2 days ago

Then I'd make a unit test, there is no requirement to do so by the compiler (not even the order).