正确理解 lowbit

公式:lowbit(x) = x & (-x)

前置知识:

-x = ~x + 1(补码 = 反码 + 1)

ps: u 和 v 互为反码,为任意 0 或 -1

x		uuuuuuuuuuuuuuuu10000
~x		vvvvvvvvvvvvvvvv01111
~x+1	vvvvvvvvvvvvvvvv10000
-x		vvvvvvvvvvvvvvvv10000

x&(-x)	000000000000000010000

你可能感兴趣的:(算法)