位运算中可以使用的三个技巧

  1. x & (-x) to keep the rightmost 1-bit and to set all the others bits to zero.
    application:
  • x&(-x)==x check if x is power of 2, pay attention to potentially overflow of -x
  1. x & (x - 1) set the rightmost 1 bit as 0.
    application:
  • x&(x - 1)==0 check if x is power of 2
  1. x % 2 or x & 1 to retrive the right most bit
posted @ 2020-12-17 05:59  EvanMeetTheWorld  阅读(21)  评论(0)    收藏  举报