diff options
| author | Richard Qin <richard.qin.x@gmail.com> | 2026-09-21 00:48:43 +0800 |
|---|---|---|
| committer | Richard Qin <richard.qin.x@gmail.com> | 2026-09-21 00:48:43 +0800 |
| commit | 18a40637ac8c2b74885790a699e4afdce4253efb (patch) | |
| tree | b9e199bd1b8aa4387a2839b36b51c7ed492544de | |
| parent | 4bd411139935c8147e20f82d4154ec25c85ab3bf (diff) | |
| download | datalab2026-18a40637ac8c2b74885790a699e4afdce4253efb.tar.gz datalab2026-18a40637ac8c2b74885790a699e4afdce4253efb.tar.xz | |
optimize leftBitCount
| -rw-r--r-- | bits.c | 22 |
1 files changed, 8 insertions, 14 deletions
@@ -155,29 +155,23 @@ int logicalShift(int x, int n) { * Difficulty: 4 */ int leftBitCount(int x) { - int shift = 16; - int t = !((((x >> shift) & 0xFFFF) ^ 0xFFFF)); - int count = t << 4; + int y = ~x; + int t; + int count = (!(y >> 16)) << 4; - shift = 24 + ~count + 1; - t = !((((x >> shift) & 0xFF) ^ 0xFF)); + t = !(y >> (25 + ~count)); count = count + (t << 3); - shift = 28 + ~count + 1; - t = !((((x >> shift) & 0xF) ^ 0xF)); + t = !(y >> (29 + ~count)); count = count + (t << 2); - shift = 30 + ~count + 1; - t = !((((x >> shift) & 0x3) ^ 0x3)); + t = !(y >> (31 + ~count)); count = count + (t << 1); - shift = 31 + ~count + 1; - t = !((((x >> shift) & 0x1) ^ 0x1)); + t = !(y >> (32 + ~count)); count = count + t; - count = count + !(~x); - - return count; + return count + !y; } /* |
