Answer to Exercise 2-6, page 49
Solution by Richard Heathfield
Write a function setbits(x,p,n,y) that returns x with the n bits that begin at position p set to the rightmost n bits of y, leaving the other bits unchanged.
This one's scary.
#include <stdio.h>
unsigned setbits(unsigned x, int p, int n, unsigned y)
{
return (x & ((~0 << (p + 1)) | (~(~0 << (p + 1 - n))))) | ((y & ~(~0 << n)) << (p + 1 - n));
}
int main(void)
{
unsigned i;
unsigned j;
unsigned k;
int p;
int n;
for(i = 0; i < 30000; i += 511)
{
for(j = 0; j < 1000; j += 37)
{
for(p = 0; p < 16; p++)
{
for(n = 1; n <= p + 1; n++)
{
k = setbits(i, p, n, j);
printf("setbits(%u, %d, %d, %u) = %u\n", i, p, n, j, k);
}
}
}
}
return 0;
}
'The C Programming Language' 카테고리의 다른 글
Chapter 2 - Types, Operators and Expressions 8 (0) | 2009.03.25 |
---|---|
Chapter 2 - Types, Operators and Expressions 7 (0) | 2009.03.25 |
Chapter 2 - Types, Operators and Expressions 5 (0) | 2009.03.25 |
Chapter 2 - Types, Operators and Expressions 4 (0) | 2009.03.25 |
Chapter 2 - Types, Operators and Expressions 3 (0) | 2009.03.25 |