Hacker News new | past | comments | ask | show | jobs | submit login

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.





Sorry 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


A) 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>


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


Should I use 5 NANDs instead? Everything we do is built on abstractions, and unlike most this one doesn't break down!

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.

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



Join us for AI Startup School this June 16-17 in San Francisco!

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

Search: