5

Let's say I have the following bit masks:

1 << 1, // 2
1 << 2, // 4 
1 << 3, // 8 
1 << 4, // 16 
1 << 5, // 32 
1 << 6  // 64

I would like to get the 'inverse'.

This does the job:

void foo(int n) {
  int c = 1;
  while (n/2 >= 2) {
    n /= 2;  
    c++;;
  }
  println(c);
}

For example, 1 << 4 resulted in 16. If I run foo(16) it prints 4. However, I feel like it could be done a lot simpler, but I can't figure out how.

Is it possible?

clankill3r
  • 9,146
  • 20
  • 70
  • 126
  • 3
    Logarithms my friend! – Adam Jul 23 '15 at 16:05
  • 1
    [Integer.numberOfLeadingZeros](http://docs.oracle.com/javase/8/docs/api/java/lang/Integer.html#numberOfLeadingZeros-int-) can be used to get the position of the 1 bit, and its javadoc even has a couple of useful formulae with logarithms. – Oleg Estekhin Jul 23 '15 at 16:08
  • possible duplicate of [Position of least significant bit that is set](http://stackoverflow.com/questions/757059/position-of-least-significant-bit-that-is-set) – talex Jul 23 '15 at 16:13
  • @talex thanks, I would never have found that topic without knowing the terminology. – clankill3r Jul 23 '15 at 16:19

3 Answers3

2

BigInteger has many useful methods - including getLowestSetBit(). It probably does it as fast as it can possibly be done.

public void test() {
    System.out.println(BigInteger.valueOf(16).getLowestSetBit());
}
OldCurmudgeon
  • 64,482
  • 16
  • 119
  • 213
1
void foo(int n)
{
    println( (Math.log(n) / Math.log(2))); 
    //cast to int with (int) to take off decimal place
}

Returns the "inverse" bitshift as you are calling it, the base 2 logarithm.

Adam
  • 2,422
  • 18
  • 29
1

Slightly faster and not depend on value itself.

UPDATED version

private static int foo2(int value) {
    int result = 0;

    int mask = 0xFFFF;
    int shift = 1<<4;
    for (int j = 0; j < 5; j++) {
        result <<= 1;
        if ((value & mask) == 0) {
            result |= 1;
            value >>>= shift;
        }
        shift >>>= 1;
        mask >>= shift;
    }
    return result;
}
talex
  • 17,973
  • 3
  • 29
  • 66