Bitwise less-than
Here and after we denote as [a]B the bitstring representation (i.e. sequence of shared bits correspondings to the binary decomposition of a). Our purpose is, for two bitstrings, to compute the bit [a<Bb].
- [ci]=[ai]⊕[bi]=[ai]+[bi]−2[aibi]
- [di]=s=i∨l−1cs // using prefix OR.
- [ei]=[di]−[di+1] for i<l, [el−1]=[dl−1]
- [a<Bb]=i=0∑l−1[eibi]
It is easy to see by direct inspection: di=0 up to (counting from the most significant bit, l−1) the first different bit, and from it equals 1. Therefore, ei equals 1 exactly in the the most significant different bit, and, hence, i=0∑l−1[eibi] computes 0 if this bit is 0 in b, and 1 if this bit is 1 in b. The only remaining case is if a=b, in which case all the variables in these equations are 0, as it should be.