need help with binary

Sep 18, 2011 at 12:51am
ilog2 - return floor(log base 2 of x), where x > 0
Example: ilog2(16) = 4
Legal ops: ! ~ & ^ | + << >>


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
int ilog2(int x) {
        int hold;

        hold = (x >> 1);
        hold = x|hold;
        hold = (x >> 2);
        hold = x|hold;
        hold = (x >> 4);
        hold = x|hold;
        hold = (x >> 8);
        hold = x|hold;
        hold = (x >> 16);
        hold = x|hold;
        hold = (x >> 32);
        hold = x|hold;

        x = hold;

        return x +(~0x2 + 2);
}
Sep 18, 2011 at 1:24am
closed account (zb0S216C)
After consulting with my psychic abilities, I can conclude that I have no psychic abilities, therefore, I cannot predict where your problem resides (if there is one).

Sorry for the sarcasm, but you need to specify your problems before we can help you.

Wazzak
Topic archived. No new replies allowed.