Finished exercises 2-7 and 2-8
This commit is contained in:
@@ -0,0 +1,69 @@
|
|||||||
|
/*
|
||||||
|
*
|
||||||
|
* Exercise 2-7. 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.
|
||||||
|
*
|
||||||
|
* Author: Kun Deng
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
|
||||||
|
// 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.
|
||||||
|
int invert(unsigned int x, int p, int n);
|
||||||
|
|
||||||
|
|
||||||
|
int main(int argc, char **argv)
|
||||||
|
{
|
||||||
|
unsigned int x = 511;
|
||||||
|
int p = 4;
|
||||||
|
int n = 3;
|
||||||
|
|
||||||
|
if (argc == 4)
|
||||||
|
{
|
||||||
|
x = atoi(argv[1]);
|
||||||
|
p = atoi(argv[2]);
|
||||||
|
n = atoi(argv[3]);
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("Calling invert(%d, %d, %d)", x, p, n);
|
||||||
|
printf(" = %d\n", invert(x, p, n));
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int invert(unsigned int x, int p, int n)
|
||||||
|
{
|
||||||
|
int x_inverted = x;
|
||||||
|
printf("%d ^ (~(~0 << %d) << (%d ", x, n, p);
|
||||||
|
|
||||||
|
if (n == 1)
|
||||||
|
{
|
||||||
|
printf(" - 1))\n");
|
||||||
|
printf("%d ^ (~(%d << %d) << (%d - 1))\n", x, ~(0), n, p);
|
||||||
|
printf("%d ^ (~(%d) << (%d - 1))\n", x, (~(0) << n), p);
|
||||||
|
printf("%d ^ (%d << (%d - 1))\n", x, ~(~(0) << n), p);
|
||||||
|
printf("%d ^ (%d << %d)\n", x, ~(~(0) << n), (p - 1));
|
||||||
|
printf("%d ^ %d\n", x, (~(~(0) << n) << (p - 1)));
|
||||||
|
|
||||||
|
// No need to add 1 when the bit amount is 1 in (p + 1 - n)
|
||||||
|
x_inverted = x ^ (~(~0U << n) << (p - 1));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
printf(" + 1 - %d))\n", n);
|
||||||
|
printf("%d ^ (~(%d << %d) << (%d + 1 - %d))\n", x, ~(0), n, p, n);
|
||||||
|
printf("%d ^ (~(%d) << (%d + 1 - %d))\n", x, (~(0) << n), p, n);
|
||||||
|
printf("%d ^ (%d << (%d + 1 - %d))\n", x, ~(~(0) << n), p, n);
|
||||||
|
printf("%d ^ (%d << %d)\n", x, ~(~(0) << n), (p + 1 - n));
|
||||||
|
printf("%d ^ %d\n", x, (~(~(0) << n) << (p + 1 - n)));
|
||||||
|
|
||||||
|
x_inverted = x ^ (~(~0U << n) << (p + 1 - n));
|
||||||
|
}
|
||||||
|
|
||||||
|
return x_inverted;
|
||||||
|
}
|
||||||
@@ -0,0 +1,53 @@
|
|||||||
|
/*
|
||||||
|
*
|
||||||
|
* Exercise 2-8. Write a function rightrot(x,n) that returns the value of the integer x
|
||||||
|
* rotated to the right by n positions.
|
||||||
|
*
|
||||||
|
* Author: Kun Deng
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <limits.h>
|
||||||
|
|
||||||
|
|
||||||
|
// Returns the value of the integer x rotated to the right by n positions
|
||||||
|
unsigned int rightrot(unsigned int x, int n);
|
||||||
|
|
||||||
|
|
||||||
|
int main(int argc, char **argv)
|
||||||
|
{
|
||||||
|
unsigned int x = 511;
|
||||||
|
int n = 3;
|
||||||
|
|
||||||
|
if (argc == 3)
|
||||||
|
{
|
||||||
|
x = atoi(argv[1]);
|
||||||
|
n = atoi(argv[2]);
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned int rotated = rightrot(x, n);
|
||||||
|
|
||||||
|
printf("rightrot(%d, %d)", x, n);
|
||||||
|
printf(" = %u\n", rotated);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
unsigned int rightrot(unsigned int x, int n)
|
||||||
|
{
|
||||||
|
unsigned int some_bits = CHAR_BIT * sizeof(x) - 1;
|
||||||
|
printf("size of x is %ld some_bits = %d\n", sizeof(x), some_bits);
|
||||||
|
|
||||||
|
unsigned int off = n & some_bits;
|
||||||
|
printf("off is %d\n", off);
|
||||||
|
|
||||||
|
printf("(%d >> %d) | (%d << (%d & %d))\n", x, off, x, off, some_bits);
|
||||||
|
printf("(%d >> %d) | (%d << %d)\n", x, off, x, (off && some_bits));
|
||||||
|
printf("%d | %d\n", (x >> off), (x << (off && some_bits)));
|
||||||
|
|
||||||
|
unsigned int rotated = (x >> off) | (x << (off & some_bits));
|
||||||
|
|
||||||
|
return rotated;
|
||||||
|
}
|
||||||
@@ -0,0 +1,41 @@
|
|||||||
|
/*
|
||||||
|
* Simple implementation of getbits() function.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
|
||||||
|
// Gets n bits from position p
|
||||||
|
unsigned getbits(unsigned int x, int p, int n);
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
// 11111110111 = 1015
|
||||||
|
unsigned int x = 1015;
|
||||||
|
int p = 4;
|
||||||
|
int n = 3;
|
||||||
|
|
||||||
|
// Result is 111 = 7
|
||||||
|
unsigned int some_bits = getbits(x, p, n);
|
||||||
|
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
unsigned getbits(unsigned int x, int p, int n)
|
||||||
|
{
|
||||||
|
printf("(%d >> (%d + 1 - %d)) & ~(~0 << %d)\n", x, p, n, n);
|
||||||
|
printf("(%d >> (%d + 1 - %d)) & ~(%d << %d)\n", x, p, n, ~0, n);
|
||||||
|
printf("(%d >> (%d + 1 - %d)) & ~(%d)\n", x, p, n, ((~0) << n));
|
||||||
|
printf("(%d >> (%d + 1 - %d)) & %d\n", x, p, n, ~((~0) << n));
|
||||||
|
printf("(%d >> %d) & %d\n", x, (p + 1 - n), ~((~0) << n));
|
||||||
|
printf("%d & %d\n", (x >> (p + 1 - n)), ~((~0) << n));
|
||||||
|
printf("%d\n", ((x >> (p + 1 - n)) & ~((~0) << n)));
|
||||||
|
// At position 4 and 3 bits from that position of value 1023 is
|
||||||
|
// 11111110111 = 1015
|
||||||
|
// 110 = 6
|
||||||
|
|
||||||
|
return (x >> (p + 1 - n)) & ~(~0 << n);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user