aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRichard Qin <richard.qin.x@gmail.com>2026-09-21 00:45:05 +0800
committerRichard Qin <richard.qin.x@gmail.com>2026-09-21 00:45:05 +0800
commit4bd411139935c8147e20f82d4154ec25c85ab3bf (patch)
tree0fdad7579e2faf947200eac96150d5dad1eee4a7
parenta861dd065647f7ec434a7d4a4c43c30a1577e03a (diff)
downloaddatalab2026-4bd411139935c8147e20f82d4154ec25c85ab3bf.tar.gz
datalab2026-4bd411139935c8147e20f82d4154ec25c85ab3bf.tar.xz
leftBitCount
-rw-r--r--bits.c24
1 files changed, 23 insertions, 1 deletions
diff --git a/bits.c b/bits.c
index 8297011..c5c08fa 100644
--- a/bits.c
+++ b/bits.c
@@ -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;
}
/*