diff options
| -rw-r--r-- | bits.c | 24 |
1 files changed, 23 insertions, 1 deletions
@@ -155,7 +155,29 @@ int logicalShift(int x, int n) { * Difficulty: 4 */ int leftBitCount(int x) { - return 2; + int shift = 16; + int t = !((((x >> shift) & 0xFFFF) ^ 0xFFFF)); + int count = t << 4; + + shift = 24 + ~count + 1; + t = !((((x >> shift) & 0xFF) ^ 0xFF)); + count = count + (t << 3); + + shift = 28 + ~count + 1; + t = !((((x >> shift) & 0xF) ^ 0xF)); + count = count + (t << 2); + + shift = 30 + ~count + 1; + t = !((((x >> shift) & 0x3) ^ 0x3)); + count = count + (t << 1); + + shift = 31 + ~count + 1; + t = !((((x >> shift) & 0x1) ^ 0x1)); + count = count + t; + + count = count + !(~x); + + return count; } /* |
