Skip to content
DevMeme
5365 of 7590
LowLevelProgramming Post #5883 · source on Telegram

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

15
Anonymous ★ Top Pick 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
  1. Anonymous ★ Top Pick

    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

  2. Anonymous

    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...

  3. Anonymous

    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

  4. Anonymous

    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

  5. Anonymous

    Bitwise popcount: portable across ancient CPUs, no POPCNT instrinsic needed. True elite status

  6. Anonymous

    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.”

  7. @M4lenov 2y

    nand2tetris course makes everyone the one

  8. @ygerlach 2y

    There is an asm instruction for that and a gcc builtin i think its name is popcount

  9. @dsmagikswsa 2y

    For one moment, I thought it is brain fuck

    1. Deleted Account 2y

      I thought the same. and i still kinda feel like it is brain fuck.

  10. @ismailgaleev 2y

    Я один ничего тут не понял????? Ощущаю себя умственно отсталым

    1. @sylfn 2y

      please use English in this chat

    2. @MDSPro 2y

      source: http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel

    3. @MDSPro 2y

      Magic = Magic Its not smth that we’re supposed to understand 🥤

  11. @ZgGPuo8dZef58K6hxxGVj3Z2 2y

    Just asking those above are the “most efficient way” right? I always thought there must be a better way💀

Use J and K for navigation