Exercise 2-6 (setbits)
Chapter_2 Exercise_2-5 bitstrings | Exercise_2-7 |
Exercise 2-6 K&R, p. 49
Exercise 2-6. Write a function setbits(x,p,n,y) that returns x with the last n bits that begin at position p set to the rightmost n bits of y, leaving the other bits unchanged.
setbits.c download
#include <stdio.h> // for printf(), scanf(), putchar()
#define LENGTH 100 // bits
// set the last n bits of x starting at position p to the rightmost n bits of y:
unsigned setbits(unsigned x, int p, int n, unsigned y);
// reverse s[], knowing its length:
void reverse(char s[], int len);
// get the bits of x into bits[], return length of bits[] (no of bits):
int bitstring (char bits[], unsigned x);
int main()
{
char bits1[LENGTH], bits2[LENGTH];
unsigned x, y;
int len1, len2, min, p, n, diff;
printf("Give a positive integer: ");
scanf("%u", &x);
printf("Give another positive integer: ");
scanf("%u", &y);
len1 = bitstring(bits1, x);
len2 = bitstring(bits2, y);
printf("x: %s\n", bits1);
printf("y: %s\n", bits2);
if (len1 < len2)
{min = len1;}
else {min = len2;}
printf("Set last 0 <= n <= %d bits of x ", min);
printf("starting at position max(0, n-1) <= p <=%d\n", min-1);
printf("to the rightmost n bits of y:\n");
printf ("n = ");
scanf("%d", &n);
printf ("p = ");
scanf("%d", &p);
x = setbits(x,p,n,y);
len1 = bitstring(bits1, x);
diff = n - len1;
while (diff > 0)
{
putchar('0');
diff--;
}
printf("%s\n", bits1);
return 0;
}
// set the last n bits of x starting at position p to the rightmost n bits of y:
unsigned setbits(unsigned x, int p, int n, unsigned y)
{
unsigned mask1 = (~0 << n); // ends in n bits of 0s
mask1 = mask1 << (p+1-n); // 0s are in the proper position in x
mask1 = mask1 | ~(~0 << (p+1-n)); // last (p+1-n) bits set (to 1)
mask1 = mask1 | (y << (p+1-n)); // 1..1(rightmost n bits of y)1..1
unsigned mask2 = ~(~0 << n); // ends in n bits of 1s
mask2 = mask2 << (p+1-n); // 0..0(n bits of 1)0..0
x = mask2 | x; // set the required bits of x to 1
return mask1 & x; // set the required bits of x to the values of y
}
// reverse s[], knowing its length:
void reverse(char s[], int len)
{
int i = 0, j = len-1;
char temp;
while (i < j)
{
temp = s[i];
s[i] = s[j];
s[j] = temp;
i++;
j--;
}
}
// get the bits of x into bits[], return length of bits[] (no of bits):
int bitstring (char bits[], unsigned x)
{
int i = 0;
if (x == 0)
{
bits[i++] = '0';
bits[i] = '\0';
return i;
}
while (x > 0)
{ // last bit gives the parity:
bits[i++] = '0' + x % 2; // 0 for even, 1 for odd
x /= 2; // x = x / 2; // lose last bit
}
bits[i] = '\0'; // end the string
reverse(bits, i);
return i;
}
/*
gcc setbits.c -o setbits
./setbits
Give a positive integer: 74
Give another positive integer: 83
x: 1001010
y: 1010011
Set last 0 <= n <= 7 bits of x starting at position max(0, n-1) <= p <=6
to the rightmost n bits of y:
n = 7
p = 6
1010011
./setbits
Give a positive integer: 850
Give another positive integer: 12356
x: 1101010010
y: 11000001000100
Set last 0 <= n <= 10 bits of x starting at position max(0, n-1) <= p <=9
to the rightmost n bits of y:
n = 7
p = 7
1110001000
./setbits
Give a positive integer: 85012356
Give another positive integer: 20125456
x: 101000100010010111110000100
y: 1001100110001011100010000
Set last 0 <= n <= 25 bits of x starting at position max(0, n-1) <= p <=24
to the rightmost n bits of y:
n = 11
p = 24
101110001000010111110000100
*/
Chapter_2 Exercise_2-5 bitstrings | BACK_TO_TOP | Exercise_2-7 |
Comments
Post a Comment