web aside data:tneg
This commit is contained in:
parent
2accc057a4
commit
9f63feabbb
21
homework/chapter2/tneg_4.c
Normal file
21
homework/chapter2/tneg_4.c
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
#include <assert.h>
|
||||||
|
#include <limits.h>
|
||||||
|
|
||||||
|
/*
|
||||||
|
* 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user