If you have the binary number 10110 how can I get it to return 5? e.g a number that tells how many bits are used? There are some likewise examples listed below:
If you are looking for the fastest (and without a table, which is certainly faster), this is probably the one:
public static int bitLength(int i) { int len = 0; while (i != 0) { len += (i & 1); i >>>= 1; } return len; }