frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

XOR'ing a register with itself is the idiom for zeroing it out. Why not sub?

https://devblogs.microsoft.com/oldnewthing/20260421-00/?p=112247
37•ingve•1h ago

Comments

nopurpose•34m ago
It amazes me how entertaining Raymond's writing on most mundane aspects of computing often is.
grebc•25m ago
If you’re not first, you’re last.
NewCzech•16m ago
The obvious answer is that XOR is faster. To do a subtract, you have to propagate the carry bit from the least-significant bit to the most-significant bit. In XOR you don't have to do that because the output of every bit is independent of the other adjacent bits.

Probably, there are ALU pipeline designs where you don't pay an explicit penalty. But not all, and so XOR is faster.

Surely, someone as awesome as Raymond Chen knows that. The answer is so obvious and basic I must be missing something myself?

mikequinlan•10m ago
As TFA says, on x86 `sub eax, eax` encodes to the same number of bytes and executes in the same number of cycles.
arka2147483647•9m ago
> The answer is so obvious

A tangent, but what is Obvious depends on what you know.

Often experts don't explain the things they think are Obvious, but those things are only Obvious to them, because they are the expert.

We should all kind, and explain also the Obvious things those who do not know.

themafia•7m ago
XOR and SUB have had identical cycle counts and latencies since the 8088. That's because you can "look ahead" when doing carries in binary. It's just a matter of how much floorspace on the chip you want to use.

https://en.wikipedia.org/wiki/Carry-lookahead_adder

The only minor difference between the two on x86, really, is SUB sets OF and CF according to the result while XOR always clears them.

svnt•7m ago
His point is that in x86 there is no performance difference but everyone except his colleague/friend uses xor, while sub actually leaves cleaner flags behind. So he suspects its some kind of social convention selected at random and then propagated via spurious arguments in support (or that it “looks cooler” as a bit of a term of art).

It could also be as a result of most people working in assembly being aware of the properties of logic gates, so they carry the understanding that under the hood it might somehow be better.

3form•2m ago
I think an even more likely explanation would be that x86 assembly programmers often were, or learned from other-architecture assembly programmers. Maybe there's a place where it makes more sense and it can be so attributed. 6502 and 68k being first places I would look at.
phire•6m ago
I'm not actually aware of any CPUs that preform a XOR faster than a SUB. And more importantly, they have identical timings on the 8086, which is where this pattern comes from.
feverzsj•6m ago
It's like 0.5 cycles vs 0.9 cycles. So both are 1 cycle, considering synchronization.
Tepix•5m ago
From TFA:

> It encodes to the same number of bytes, executes in the same number of cycles.

jojobas•4m ago
The non-obvious bit is why there isn't an even faster and shorter "mov <register>,0" instructions - the processors started short-circuiting xor <register>,<register> much later.
virexene•3m ago
The operation is slightly more complex yes, but has there ever been an x86 CPU where SUB or XOR takes more than a single CPU cycle?
anematode•11m ago
My favorite (admittedly not super useful) trick in this domain is that sbb eax, eax breaks the dependency on the previous value of eax (just like xor and sub) and only depends on the carry flag. arm64 is less obtuse and just gives you csetm (special case of csinv) for this purpose.
defrost•7m ago

  Once an instruction has an edge, even if only extremely slight, that’s enough to tip the scales and rally everyone to that side.
And this, interestingly, is why life on earth uses left-handed amino acids and right-handed sugars .. and why left handed sugar is perfect for diet sodas.
Sweepi•4m ago
"Bonus bonus chatter: The xor trick doesn’t work for Itanium because mathematical operations don’t reset the NaT bit. Fortunately, Itanium also has a dedicated zero register, so you don’t need this trick. You can just move zero into your desired destination."

Will remember for the next time I write asm for Itanium!

tliltocatl•3m ago
It might be because XOR is rarely (in terms of static count, dynamically it surely appears a lot in some hot loops) used for everything else, so it is easier to spot and identify as "special" if you are writing manual assembly.
stingraycharles•2m ago
And helps with SMT
tliltocatl•1m ago
What's SMT in this context?
rasz•3m ago
Looking at some random 1989 Zenith 386SX bios written in assembly so purely programmer preferences:

8 'sub al, al', 14 'sub ah, ah', 3 'sub ax, ax'

26 'xor al, al', 43 'xor ah, ah', 3 'xor ax, ax'

Colorado Age Attestation bill gets amendments to have open source excluded

https://www.gamingonlinux.com/2026/04/colorado-age-attestation-bill-gets-amendments-to-have-open-...
2•jurgemaister•5m ago•0 comments

SpaceX says unproven AI space data centers may not be commercially viable

https://www.reuters.com/world/spacex-says-unproven-ai-space-data-centers-may-not-be-commercially-...
2•1vuio0pswjnm7•7m ago•0 comments

Went to bed with a $10 budget alert. Woke up to $25,672 in debt to Google Cloud

https://old.reddit.com/r/googlecloud/comments/1ssagtw/went_to_bed_with_a_10_budget_alert_woke_up_to/
2•ratg13•7m ago•0 comments

I built a simple flowchart tool to organize ideas

https://www.processon.io/
1•shujie_li•9m ago•0 comments

Florida Attorney General launches criminal probe into ChatGPT over FSU shooting

https://apnews.com/article/florida-chatgpt-fsu-gunman-b32a7276426f621193f61a0f904f924c
2•1vuio0pswjnm7•9m ago•0 comments

Show HN: Content-mill: Index any static content into Meilisearch via YAML config

https://github.com/blueinit/content-mill
1•centrali•10m ago•1 comments

Let's Measure Gravity

https://owl.billpg.com/lets-measure-gravity/
1•billpg•11m ago•0 comments

Show HN: We benchmarked 18 LLMs on OCR (7K+ calls) – cheaper models win

https://www.arbitrhq.ai/leaderboards/
2•TimoKerr•12m ago•1 comments

Top Law Firm Apologizes to Bankruptcy Judge for AI Hallucination

https://www.bloomberg.com/news/articles/2026-04-21/top-law-firm-apologizes-to-bankruptcy-judge-fo...
2•1vuio0pswjnm7•12m ago•0 comments

Show HN: Notation – an iOS chess coach (Stockfish and optional BYOK Claude)

https://notationchesscoach.app
1•darrenc81•13m ago•1 comments

Orson Scott Card on feedback from editors

https://twitter.com/i/status/2046702294406680751
1•Michelangelo11•15m ago•0 comments

You can make something good

https://sfalexandria.com/posts/farzas-creations/
1•aadillpickle•20m ago•0 comments

Git for web services – everything as a file for coding agents

https://github.com/KrzysztofBogdan/gitfs
1•kpbogdan•20m ago•1 comments

American Supply Chain

https://twitter.com/bihanmahadewa/status/2046732759339552772
1•bihan•20m ago•1 comments

In Search of (Claude's) Lost Time - Globalizing Claude's project memories

https://www.gsarigiannidis.gr/claude-global-memory/
2•gsarig•23m ago•0 comments

Deep Research Max

https://blog.google/innovation-and-ai/models-and-research/gemini-models/next-generation-gemini-de...
2•markerbrod•24m ago•0 comments

Aube: A fast Node.js package manager

https://github.com/endevco/aube
1•icar•24m ago•0 comments

Spain's greatest matador gored by bull in comeback from retirement

https://www.thetimes.com/world/europe/article/morante-bullfighter-injured-bull-goring-tsj0bt7ks
2•petethomas•26m ago•2 comments

C++ Scripting with Libriscv

https://libriscv.no/blog/expert-example/
1•fwsgonzo•27m ago•0 comments

Anthropic CVP – Run 2

https://sunglasses.dev/reports/anthropic-cvp-opus-4-7-evaluation-run-2
1•azrollin•30m ago•0 comments

Shared Agent Harness

https://github.com/goncalossilva/.agents
1•ankitg12•35m ago•0 comments

Rspack 2.0

https://rspack.rs/blog/announcing-2-0
2•0x1997•39m ago•0 comments

The Free Universal Construction Kit

https://fffff.at/free-universal-construction-kit/
1•robinhouston•40m ago•0 comments

Force all app traffic into the tunnel in the iOS app

https://mullvad.net/en/blog/force-all-app-traffic-into-the-tunnel
1•eptcyka•41m ago•0 comments

Run Commands on File Event

https://evilcookie.de/on-run-commands-on-file-event.html
1•Tch1b0•41m ago•0 comments

Viewing One's Live Self Interrupts Mindless Short-Form Video Scrolling

https://arxiv.org/abs/2604.19424
2•50kIters•42m ago•0 comments

When the pronoun "they" breaks your RAG pipeline

https://old.reddit.com/r/Rag/comments/1spro5f/when_the_pronoun_they_breaks_your_rag_fixing/
1•HarinezumIgel•46m ago•0 comments

What Makes Docs Beautiful?

https://passo.uno/what-makes-docs-beautiful/
2•eigenBasis•46m ago•0 comments

CrabTrap

https://github.com/brexhq/CrabTrap/
1•handfuloflight•50m ago•0 comments

Pixi: One Package Manager for Python and C/C++ Libraries

https://codecut.ai/uv-pixi-comparison/
1•lululpac•50m ago•0 comments