highestOneBit function

int highestOneBit(
  1. int n
)

Reurn the highest one bit of an int n : input number References

.. 1 "understanding logic behind integer highestonebit method implementation". https://stackoverflow.com/questions/53369498/understanding-logic-behind-integer-highestonebit-method-implementation. Retrieved 2019-07-24. Examples

Implementation

int highestOneBit(int n) {
  // HD, Figure 3-1
  n |= (n >> 1);
  n |= (n >> 2);
  n |= (n >> 4);
  n |= (n >> 8);
  n |= (n >> 16);
  return n - (n >> 1);
}