Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I get your point. Inevitably someone sees “oh I can swap two values with 3x xor” and puts it in a real codebase. Write readable code. We probably don’t need to save the single word of memory in 2025.

Someone at work learned what a bitmap was and now uses them in dozens of golang structs as part of API bindings to know whether a field is set or not. It’s insane. Cool lower level tricks are neat but let’s not invent things that are difficult to understand or work with.



I agree for most things, not sure about the bitmap though. I'm not a golang user, but from what I know it doesn't have operator overloading, so you can't make a more elegant solution with proper types. Considering this, bitmaps are fairly readable and let you do things like easy comparisons without writing tons of && or ||.

As long as they are declared as enums or similar, I don't see the problem (IIRC the iota keyword let's you do something like FLAG = 1 << iota;).


Most compilers will compile both the three xor version and three variable version to the same machine code anyway, since modern cpu's have swap instructions.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: