Removing lowest order bit

2020-06-17 06:50发布

Given a binary number, what is the fastest way of removing the lowest order bit?

01001001010 -> 01001001000

It would be used in code to iterate over the bits of a variable. Pseudo-code follows.

while(bits != 0){
  index = getIndexOfLowestOrderBit(bits);
  doSomething(index);
  removeLowestOrderBit(bits);
}

The possible languages I'm considering using are C and Java.

7条回答
Emotional °昔
2楼-- · 2020-06-17 07:43

In Java use Integer.lowestOneBit().

查看更多
登录 后发表回答