2021年4月30日星期五

Write expressions that use bitwise operators, equality checks, and Boolean operators to do the following in O(1) time

• Right propagate the rightmost set bit in x, e.g., turns (01010000) to (01011111).

I am stuck on this one. I can mask out the right-most bit with x &= ~(x-1) how can I set the bits after it as the problem states?

My attempt:

int problemOne(int n)  {      int k = n;      k &= ~(n-1);      n |= k;      return n;  }  
https://stackoverflow.com/questions/67342133/write-expressions-that-use-bitwise-operators-equality-checks-and-boolean-opera May 01, 2021 at 09:27AM

没有评论:

发表评论