top | item 40484248

(no title)

rokicki | 1 year ago

To take this to the next level: what does [(a^b) & (-(a^b)) & a] compute? (Assume unsigned arithmetic.)

And then after that: what use can this be put to?

discuss

order

rokicki|1 year ago

This expression is nonzero iff reverse(a) > reverse(b) (where reverse is the bitreversal of an unsigned number).

It (using the address of the nodes as arguments) can serve as a tiebreaker in a Cartesian tree (such as one implementing a first-fit memory allocator) or even to replace the random priority value in a treap (meaning you need neither storage nor computation for the priority node of the treap).

wruza|1 year ago

Iow, we flip some bits in `a`, then do subj, then mask it back to `a`. It’s unclear what it computes in general, but if `b` disturbs the 2-powerness of `a` then I guess we learn that fact by seeing zero. Not sure where to use it.

rokicki|1 year ago

There's a nice elegant description of what it does, mathematically, and a significant use in Computer Science.