How does the formula x & (x – 1) work?
The formula x & (x – 1) is used in bitwise operations to clear the lowest set bit (the rightmost ‘1’) in an integer x. When you subtract 1 from x, you flip all the bits to the right of the least significant set bit.
The bitwise AND operation between x and x – 1 will reset the rightmost set bit to 0, leaving the rest of the bits unchanged.
The formula `x & (x – 1)` is a bitwise operation that clears the least significant bit (LSB) of `x`.
Here’s how it works:
Example:
Suppose `x` is 12 (1100 in binary).
`x – 1` would be 11 (1011 in binary).
`x & (x – 1)` would be 1100 & 1011 = 1000 (8 in decimal).
As you can see, the LSB of `x` (12) is cleared, resulting in 8.How does the formula x & (x – 1) work?no