http://www.catonmat.net/blog/low-level-bit-hacks-you-absolutely-must-know/

 

Bit Hack #6. Turn off the rightmost 1-bit.

y = x & (x-1)

Bit Hack #7. Isolate the rightmost 1-bit.

y = x & (-x)

Bit Hack #8. Right propagate the rightmost 1-bit.

y = x | (x-1)

Bit Hack #9. Isolate the rightmost 0-bit.

y = ~x & (x+1)

Bit Hack #10. Turn on the rightmost 0-bit.

y = x | (x+1)

x y differ only 1 bit
dif = x^y
return dif && !( dif & (dif-1) )



相关文章:

  • 2021-10-31
  • 2021-11-28
  • 2021-05-18
  • 2022-12-23
  • 2021-05-27
  • 2022-12-23
  • 2022-02-01
  • 2022-12-23
猜你喜欢
  • 2021-12-20
  • 2022-12-23
  • 2022-12-23
  • 2021-11-28
  • 2021-07-25
  • 2021-10-18
相关资源
相似解决方案