From 18a40637ac8c2b74885790a699e4afdce4253efb Mon Sep 17 00:00:00 2001 From: Richard Qin Date: Mon, 21 Sep 2026 00:48:43 +0800 Subject: optimize leftBitCount --- bits.c | 22 ++++++++-------------- 1 file changed, 8 insertions(+), 14 deletions(-) diff --git a/bits.c b/bits.c index c5c08fa..6450c1c 100644 --- a/bits.c +++ b/bits.c @@ -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; } /* -- cgit v1.2.3