diff options
| author | Richard Qin <richard.qin.x@gmail.com> | 2026-09-21 00:45:05 +0800 |
|---|---|---|
| committer | Richard Qin <richard.qin.x@gmail.com> | 2026-09-21 00:45:05 +0800 |
| commit | 4bd411139935c8147e20f82d4154ec25c85ab3bf (patch) | |
| tree | 0fdad7579e2faf947200eac96150d5dad1eee4a7 | |
| parent | a861dd065647f7ec434a7d4a4c43c30a1577e03a (diff) | |
| download | datalab2026-4bd411139935c8147e20f82d4154ec25c85ab3bf.tar.gz datalab2026-4bd411139935c8147e20f82d4154ec25c85ab3bf.tar.xz | |
leftBitCount
| -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; } /* |
