Originally Posted by
Aerie
I was looking at the solution posted to problem 2-7 in K&R:
Write a function invert(x,p,n) that returns x with the n bits that begin at position p inverted (i.e., 1 changed into 0 and vice versa), leaving the others unchanged.
The page I browed had this solution:
<snip>
I noticed a couple bugs in the code, so I modified it a bit
The solution posted there correctly answers the question, yours does not. Note the differences between this
Code:
0, 1, 1: 00000000000000000000000000000010
0, 1, 2: 00000000000000000000000000000110
0, 1, 3: 00000000000000000000000000001110
0, 1, 4: 00000000000000000000000000011110
0, 1, 5: 00000000000000000000000000111110
0, 1, 6: 00000000000000000000000001111110
0, 1, 7: 00000000000000000000000011111110
0, 2, 1: 00000000000000000000000000000100
0, 2, 2: 00000000000000000000000000001100
0, 2, 3: 00000000000000000000000000011100
0, 2, 4: 00000000000000000000000000111100
0, 2, 5: 00000000000000000000000001111100
0, 2, 6: 00000000000000000000000011111100
and this
Code:
0, 1, 1: 00000000000000000000000000000001
0, 1, 2: 00000000000000000000000000000010
0, 1, 3: 00000000000000000000000000000100
0, 1, 4: 00000000000000000000000000001000
0, 1, 5: 00000000000000000000000000010000
0, 1, 6: 00000000000000000000000000100000
0, 1, 7: 00000000000000000000000001000000
0, 1, 8: 00000000000000000000000010000000
0, 2, 2: 00000000000000000000000000000011
0, 2, 3: 00000000000000000000000000000110
0, 2, 4: 00000000000000000000000000001100
0, 2, 5: 00000000000000000000000000011000
0, 2, 6: 00000000000000000000000000110000
0, 2, 7: 00000000000000000000000001100000
0, 2, 8: 00000000000000000000000011000000
in regard to the original question. For example, the highlighted line doesn't look like the value 0 with the 6 bits beginning at bit 2 inverted.
1. Generate a single bit 0 with 0U, then reverse it to single bit 1U
This means all value bits of an unsigned int set to 1; which is UINT_MAX.
[edit]Here's what's going on:
Code:
#include <stdio.h>
void showbits(unsigned int byte)
{
unsigned int bit;
for ( bit = (~0U >> 1) + 1; bit; bit >>= 1 )
{
putchar(byte & bit ? '1' : '0');
}
putchar('\n');
}
unsigned invert(unsigned x, int p, int n)
{
unsigned temp;
printf("x = %u, p = %d, n = %d\n", x, p, n);
temp = 0U; fputs(" 0U: ", stdout); showbits(temp);
temp = ~temp; fputs(" ~0U: ", stdout); showbits(temp);
temp = temp << n; fputs(" ~0U << n: ", stdout); showbits(temp);
temp = ~temp; fputs(" ~(~0U << n): ", stdout); showbits(temp);
temp = temp << p; fputs(" ~(~0U << n) << p: ", stdout); showbits(temp);
fputs("x : ", stdout); showbits(x);
temp = temp ^ x; fputs("x ^ (~(~0U << n) << p): ", stdout); showbits(temp);
return x ^ (~(~0U << n) << p);
}
int main(void)
{
invert(100, 6, 2);
return 0;
}
/* my output
x = 100, p = 6, n = 2
0U: 00000000000000000000000000000000
~0U: 11111111111111111111111111111111
~0U << n: 11111111111111111111111111111100
~(~0U << n): 00000000000000000000000000000011
~(~0U << n) << p: 00000000000000000000000011000000
x : 00000000000000000000000001100100
x ^ (~(~0U << n) << p): 00000000000000000000000010100100
*/