1
for (int i = 32; i <= 127; i++) {
}

I convert the number int 32 into binary number 00100000 and the number int 127 into binary 01111111. I need the first position of one read from right (Bit numbering - find first set (ffs) or find first one (ffo)), 00100000 -> 6 and at 01111111 -> 1

Thank you!

user3235117
  • 45
  • 2
  • 7

3 Answers3

5

The API is your friend too :

static int position(int a){
    int pos = Integer.numberOfTrailingZeros(a);
    return pos == 32 ? -1 : pos;
}
Alexis C.
  • 91,686
  • 21
  • 171
  • 177
  • @user3235117 You can do `int pos = Integer.numberOfTrailingZeros(a)+1; return pos == 33 ? -1 : pos;`. This is just a matter of taste (bit at position 0th or not). Usually, the convention is to say that the first bit is at position 0. – Alexis C. Jan 25 '14 at 22:15
  • @user3235117 if you only have a byte instead of an int I suggest you have a look at the answer I gave when you asked this question before http://stackoverflow.com/a/21351222/57695 – Peter Lawrey Jan 25 '14 at 23:42
  • @ZouZou can you briefly explain why `pos == 32 ? -1 : pos` and how to get to the result? I want to understand. Thank you! – user3235117 Jan 26 '14 at 11:20
  • @user3235117 What don't you understand? The syntax? – Alexis C. Jan 26 '14 at 11:30
  • @user3235117 This is the same as `if(pos == 32) return -1; else return pos;` This is called conditional operator : http://docs.oracle.com/javase/tutorial/java/nutsandbolts/op2.html – Alexis C. Jan 26 '14 at 12:13
  • Thank you! I'm studying computer of science only in the first semester – user3235117 Jan 26 '14 at 12:21
2

A simple way of finding this number is as follows:

int findLowestSetBit(int n) {
    for (int i = 0 ; i != 32 ; i++) {
        if ((n & (1 << i)) != 0) {
            return i;
        }
    }
    return -1;
}

However, it is not the fastest one, because it searches for a set bit "linearly". You can do it in parallel with the following piece of code copied from the bit hack page:

int v;      // 32-bit word input to count zero bits on right
int c = 32; // c will be the number of zero bits on the right
v &= -v;
if (v != 0) c--;
if ((v & 0x0000FFFF) != 0) c -= 16;
if ((v & 0x00FF00FF) != 0) c -= 8;
if ((v & 0x0F0F0F0F) != 0) c -= 4;
if ((v & 0x33333333) != 0) c -= 2;
if ((v & 0x55555555) != 0) c -= 1;
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • To turn this code into Java, you will need to say `if ((v & ....) != 0)` instead of just `if (v & ....)`, as Java requires `if` conditions to be `boolean` rather than `int`. – Ian Roberts Jan 25 '14 at 21:43
  • Multiple markers at this line - Syntax error on token ")", ; expected - Syntax error on token "(", ; expected – user3235117 Jan 25 '14 at 22:18
0

to find a leading zeros you can use recursion & bit wise shift

i post answer to similar question here:

count leading zeros (clz) or number of leading zeros (nlz) in Java

or using a Divine & Conquer Algorithm which some examples are here :

How to count the number of set bits in a 32-bit integer?

Community
  • 1
  • 1
ceph3us
  • 7,326
  • 3
  • 36
  • 43