Why does (x-1) toggle all the bits from the rightmost set bit of x?

后端 未结 2 1531
攒了一身酷
攒了一身酷 2021-01-27 15:18

Why does this property hold true?

say the kth bit from right side is the first set bit in number \'x\'. (x-1) will toggle every bit upto kth bit from r

2条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-27 15:37

    It's the same reason if you do (x-1) where x is a base 10 integers all the zeros (if any) on the right will become nines.

    9324930000000 - 1 = 9324929999999
    

提交回复
热议问题