5 ms·
Every now and again one of these come up in a PR and I hard reject it every time. Its clever. Its neat. Its elegant. And it takes a giant ass white paper to exp
by localghost3000 2y ago
Every now and again one of these come up in a PR and I hard reject it every time. Its clever. Its neat. Its elegant. And it takes a giant ass white paper to explain it to people. Please please please don't use stuff like this when other people have to read your code.
- hermannj314 2y ago[flagged]
- do_not_redeem 2y agoSorry but in what universe is xor too advanced for a programmer (someone who works with computers and numbers for a living) to understand? 99% of the article is just fun trivia anyway. If a deep dive like this scares you, then the + operator should leave you absolutely terrified, just look at the length of this article: https://en.wikipedia.org/wiki/Addition https://en.wikipedia.org/wiki/Addition
- localghost3000 2y agoA) don’t be rude please. B) yes. I get how it works. It’s not too advanced for me. It’s too clever. There’s a difference. In my experience something more readable and verbose would do in nearly every case I’ve ever seen someone try to use it. It’s an opinion though so.. <shrug>
- Dylan16807 2y agoYou said "it takes a giant ass white paper to explain" and that's not an opinion, that's just wrong. And not only is + more complicated, + can cause undefined behavior very easily in C.
- skeeter2020 2y agoShould I use 5 NANDs instead? Everything we do is built on abstractions, and unlike most this one doesn't break down!
- kali_00 2y agoUnderstanding basic boolean logic isn't just fundamental to programming, it's a particularly easy component that's usually taught very early, for both reasons. It takes one table to explain XOR and it's the first one on the page. In ASCII: a | b | a XOR b --+---+--------- 0 | 0 | 0 0 | 1 | 1 1 | 0 | 1 1 | 1 | 0 If a "programmer" seeing that still can't read code using a XOR, I'd fire them.
- stbenjam 2y agoI 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.
- hananova 2y agoMost 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.
- PhilipRoman 2y agoI 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;).