Mastering bitwise sorcery: the moment you become the one in C
Description
Five-panel meme overlaying a famous dojo fight scene. Panel 1 shows a trainee’s shocked face covered by cascading white text like “((y&(-y))<<1)|(x>>2)>>1|0b110xFF” on a black background reminiscent of a disassembler dump. Panel 2, the trainee says in subtitle-style white text: “I know bitwise operators.” Panel 3 cuts to the mentor who replies, half-hidden: “Show me.” Panel 4 freeze-frames the trainee mid-pose with an overlaid C snippet for counting set bits: “c = (v & 0x55555555) + ((c >> 1) & 0x55555555); … c = (c & 0x0000FFFF) + ((c >> 16) & 0x0000FFFF);”. Panel 5 shows the stunned mentor’s face in dim lighting with caption “He is the one.” Visually, the joke riffs on the Matrix “I know kung fu” sequence, subbing martial arts with hardcore bit-twiddling hacks, celebrating low-level mastery, micro-optimisation, and those legendary 32-bit popcount tricks every systems engineer envies
Comments
15Comment deleted
When your junior claims to "know bitwise," you hit him with a five-line popcount and suddenly HR is asking if you’re hiring a compiler instead of a developer
The real 'red pill' moment isn't seeing through the Matrix - it's when you realize that bitcount implementation is just repeatedly masking and shifting to sum bits in parallel, and suddenly those magic hex constants like 0x55555555 make perfect sense as binary patterns 01010101...
When someone casually implements bitcount using SWAR (SIMD Within A Register) with those beautiful 0x55555555 and 0x33333333 masks instead of the naive loop-and-count approach, you know they've transcended from mere mortal developer to The One. Bonus points if they can explain why this divide-and-conquer approach is faster than __builtin_popcount() on architectures without native POPCNT instructions - though let's be honest, in 2024 we're all just using the intrinsic and letting the compiler figure it out
Anyone can shift left; the chosen ones have 0x55555555, 0x33333333, and 0x0F0F0F0F cached in L1 - and still argue whether to use std::popcount or keep the SWAR for portability
Bitwise popcount: portable across ancient CPUs, no POPCNT instrinsic needed. True elite status
You know it’s the chosen one when they hand‑roll the 0x55555555 SWAR popcount and then check the disassembly to confirm LLVM emitted a single POPCNT - while the rest of us call builtin_popcount and call it “readability.”
nand2tetris course makes everyone the one Comment deleted
There is an asm instruction for that and a gcc builtin i think its name is popcount Comment deleted
For one moment, I thought it is brain fuck Comment deleted
I thought the same. and i still kinda feel like it is brain fuck. Comment deleted
Я один ничего тут не понял????? Ощущаю себя умственно отсталым Comment deleted
please use English in this chat Comment deleted
source: http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel Comment deleted
Magic = Magic Its not smth that we’re supposed to understand 🥤 Comment deleted
Just asking those above are the “most efficient way” right? I always thought there must be a better way💀 Comment deleted