diff options
| author | Adam Carpenter <gitlab@53hor.net> | 2019-07-13 11:57:09 -0400 | 
|---|---|---|
| committer | Adam Carpenter <gitlab@53hor.net> | 2019-07-13 11:57:09 -0400 | 
| commit | c838e15b7f7124e82ba59ae92029957622fdc475 (patch) | |
| tree | 0af1c13f3d054b753b975b2739e79e637e19f528 /ch2 | |
| parent | 4a24a8288478231537ec89db40b93591d7fa2b2c (diff) | |
| download | learning-c-c838e15b7f7124e82ba59ae92029957622fdc475.tar.xz learning-c-c838e15b7f7124e82ba59ae92029957622fdc475.zip | |
Added 2-6
Diffstat (limited to 'ch2')
| -rw-r--r-- | ch2/2-6.c | 15 | 
1 files changed, 15 insertions, 0 deletions
| diff --git a/ch2/2-6.c b/ch2/2-6.c new file mode 100644 index 0000000..ee6770d --- /dev/null +++ b/ch2/2-6.c @@ -0,0 +1,15 @@ +#include<stdio.h> + +unsigned setbits(unsigned x, unsigned p, unsigned n, unsigned y) { +    // set n bits in x starting at p to rightmost n bits of y +    unsigned offset = (p + 1 - n); +    unsigned y_mask = y & ~(~0 << n); +    unsigned bot = x & ~(~0 << offset); +    unsigned top = (x >> offset << offset) & y_mask; +    unsigned result = top & bot; +    return result; +} + +int main() { +    printf("%x\n", setbits(0xaa, 3, 3, 0x33)); +} |