addition/subtraction for many logical inference problems and priority problems.
x & -x = x & ~(x-1) will return a vector with 1 bit set, and this bit will be the rightmost 1 in x. (or 0 when x = 0)
for example x = 0xF6. (x-1) = 0xF5, ~(x-1) = 0x0A. Notice that the carry of 0xF6 - 1 goes only to the 2nd bit and then stops. As a result, bits 7:2 in the two terms will be opposite -- the carry doesn't propogate this far. Same goes for bit 0 -- the carry forced the lsb's to 0. only bit 1 will be set in both.
Now look at x = 0xF5 in x & ~(x-2) x-2 = 0xF3 and ~(x-2) = 0x0C. noticed that the carry can't even start in the lsb -- x-2 will preserve even/odd. So these bits will be 0 due to "1 and not 1 = 0". At this point, the problem is the same as above, just for bits 7:1 vs 7:0.
For this application, this same trick is applied to the double_req version in order to capture if this carry moves past the normal msb.
if "base" is not a power of two, you could get more than on bit set in the result.
- - - Updated - - -
Possibly better explanation:
Bits to the right of the single bit set in "base" will be unaffected by (double_req - base). (group-a)
The expression (double_req - base) will result in borrowing from this point.
This converts the right bits of double_req past base into 1's if they are 0's. (group-b)
(group-b might be empty, eg if rightmost bit of double_req is the bit set in base)
This borrowing chains until double_req has a 1. This 1 is set to 0. (bit-c)
Bits to the left of this rightmost 1 in double_req are unaffected. (group-d)
The negation means the bits in group-a and group-d (which were not affected by the subtraction) will be different from the values in double_req.
The bits in group-b (if present) will be converted to 0.
The bit in bit-c will be converted to a 1.
The result of the and is thus all 0's except the first in a bit location >= base's only set bit.
(group-a/group-d are zero as: value and not value = 0.)
(bits in group-b are zero as: value and 0 = 0.)