bitwise most significant set bit

后端 未结 10 1974
走了就别回头了
走了就别回头了 2020-12-20 19:50

I want to find the most significant bit that is set to 1. I have tried every possible way from & to ORing all of the bits from 1 t

10条回答
  •  春和景丽
    2020-12-20 20:23

    Not the most efficient, perhaps, but this should work::

    public int firstBit(int i) {
        return i < 0 ? 31 : i == 0 ? 0 : Integer.toString(i, 2).length();
    }
    

提交回复
热议问题