From 9f63feabbb3f3fa65abae443f477cb0c67712de2 Mon Sep 17 00:00:00 2001 From: "18218461270@163.com" <18218461270@163.com> Date: Fri, 8 Aug 2025 02:38:13 +0800 Subject: [PATCH] web aside data:tneg --- homework/chapter2/tneg_4.c | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 homework/chapter2/tneg_4.c diff --git a/homework/chapter2/tneg_4.c b/homework/chapter2/tneg_4.c new file mode 100644 index 0000000..ead1932 --- /dev/null +++ b/homework/chapter2/tneg_4.c @@ -0,0 +1,21 @@ +#include +#include + +/* +* Generate mask indicating rightmost 1 in x. +* For example 0xFF00 -> 0x0100, and 0x6600 --> 0x0200. +* If x == 0, then return 0. +*/ +int rightmost_one(unsigned x) { + return x & -x; +} + +int main() { + for (unsigned x = 0; ; x++) { + assert((x == 0 && rightmost_one(x) == 0) || (x != 0 && rightmost_one(x) == (1 << __builtin_ctz(x)))); + + if (x == UINT_MAX) { + break; + } + } +} \ No newline at end of file