From 8a8707cc3538ef89421fc1266091d1a6dc9aec6f Mon Sep 17 00:00:00 2001 From: kdeng00 Date: Sun, 18 Oct 2020 17:15:40 -0400 Subject: [PATCH] Finished exercises 2-7 and 2-8 --- ch_02/exercise_02-07/main.c | 69 +++++++++++++++++++++++++++++++++++++ ch_02/exercise_02-08/main.c | 53 ++++++++++++++++++++++++++++ ch_02/getbits/main.c | 41 ++++++++++++++++++++++ 3 files changed, 163 insertions(+) create mode 100644 ch_02/exercise_02-07/main.c create mode 100644 ch_02/exercise_02-08/main.c create mode 100644 ch_02/getbits/main.c diff --git a/ch_02/exercise_02-07/main.c b/ch_02/exercise_02-07/main.c new file mode 100644 index 0000000..e5c1f0b --- /dev/null +++ b/ch_02/exercise_02-07/main.c @@ -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 +#include + + +// 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; +} diff --git a/ch_02/exercise_02-08/main.c b/ch_02/exercise_02-08/main.c new file mode 100644 index 0000000..6bc3d5e --- /dev/null +++ b/ch_02/exercise_02-08/main.c @@ -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 +#include +#include + + +// 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; +} diff --git a/ch_02/getbits/main.c b/ch_02/getbits/main.c new file mode 100644 index 0000000..2436bfe --- /dev/null +++ b/ch_02/getbits/main.c @@ -0,0 +1,41 @@ +/* + * Simple implementation of getbits() function. + * + */ + +#include + + +// 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); +} \ No newline at end of file