Commit Diff


commit - 1ce4d806ce9d151bca7396796272505d06c710e3
commit + d895e88137a1d39b3da1087c59d6d7b39aeaef60
blob - /dev/null
blob + ea6e10dd4e81b03ec4cc86db6c6845232ff0d097 (mode 644)
--- /dev/null
+++ 2-8-input
@@ -0,0 +1,9 @@
+0x7de65afe 15 0xb5fcfbcc
+0xadd230f1 23 0xa461e35b
+0x0feb3112 31 0x1fd66224
+0xa0b0c118 2 0x282c3046
+0x001992ba 17 0xc95d000c
+0xabcdef01 12 0xf01abcde
+0x12345678 27 0x468acf02
+0x92fab3da 9 0xed497d59
+0x1ba83dea 17 0x1ef50dd4
blob - /dev/null
blob + 37264dfd7b26b72a6ab2bb9e9a1206157c7069f9 (mode 644)
--- /dev/null
+++ 2-8-output
@@ -0,0 +1,11 @@
+Type input line x  n  expected
+               %x %d %x
+result = 0xb5fcfbcc, expect = 0xb5fcfbcc
+result = 0xa461e35b, expect = 0xa461e35b
+result = 0x1fd66224, expect = 0x1fd66224
+result = 0x282c3046, expect = 0x282c3046
+result = 0xc95d000c, expect = 0xc95d000c
+result = 0xf01abcde, expect = 0xf01abcde
+result = 0x468acf02, expect = 0x468acf02
+result = 0xed497d59, expect = 0xed497d59
+result = 0x1ef50dd4, expect = 0x1ef50dd4
blob - /dev/null
blob + 863dd9a1c5ba23ecdd5e97d6347e8c16adc09c4e (mode 644)
--- /dev/null
+++ 2-8.c
@@ -0,0 +1,90 @@
+/* 2-8 Write a function rightrot(x,n) that returns the value of the integer x
+ * rotated to the right by n bit positions.
+ */
+
+#include <stdio.h>
+
+#define MAXLINE 1000		/* maximum input line size */
+
+unsigned getbits(unsigned x, int p, int n);
+unsigned setbits(unsigned x, int p, int n, unsigned y);
+unsigned invert(unsigned x, int p, int n);
+unsigned rightrot(unsigned x, int n);
+
+int main() {
+
+	printf("Type input line x  n  expected\n");
+	printf("               %%x %%d %%x\n");
+	unsigned x = 0;
+	int n = 0;
+	unsigned result = 0;
+	unsigned expect = 0;
+
+	while(scanf("%x %d %x\n", &x, &n, &expect) == 3) {
+		result = rightrot(x, n);
+		printf("result = 0x%x, expect = 0x%x\n", result, expect);
+	}
+	return 0;
+}
+
+/* getbits: get n bits from position p
+ *
+ * From K&R Cv2:
+ * 	getbits(x,p,n) returns the (right adjusted) n-bit field of x that begins at
+ * 	position p. We assume that bit position 0 is at the right end and that n
+ * 	and p are sensible positive values. For example, getbits(x,4,3) returns the
+ * 	three bits in bit positions 4, 3, and 2, right adjusted
+*/
+unsigned getbits(unsigned x, int p, int n) {
+	return (x >> (p+1-n)) & ~(~0 << n);
+}
+
+/* setbits: set n bits beginning at position p in x to rightmost n bits of y */
+unsigned setbits(unsigned x, int p, int n, unsigned y) {
+	return (x & ~(~(~0<<n) << (p-n+1))) | ((y & ~(~0<<n)) << (p-n+1));
+}
+
+/* invert: return x with n bits at position p inverted */
+unsigned invert(unsigned x, int p, int n) {
+	return x ^ (~(~0<<n) << (p-n+1));
+}
+
+/* rightrot(x,n): returns x rotated to the right by n bits
+ */
+unsigned rightrot(unsigned x, int n) {
+	return ((x & ~(~0<<n)) << (sizeof(unsigned)*8-n)) | ((x & (~0<<n)) >> n);
+}
+
+/*
+ * 0x7de65afe 15 0xb5fcfbcc
+ * 0x7de65afe 0111 1101 1110 0110 0101 1010 1111 1110
+ * rightrot(x, 15)
+ * 0xb5fcfbcc 1011 0101 1111 1100 1111 1011 1100 1100
+ *
+ * 0xadd230f1 23 0xa461e35b
+ * 0xadd230f1 1010 1101 1101 0010 0011 0000 1111 0001
+ * rightrot(x, 23)
+ * 0xa461e35b 1010 0100 0110 0001 1110 0011 0101 1011
+ *
+ * 0x0feb3112 31 0x1fd66224
+ * 0x0feb3112 0000 1111 1110 1011 0011 0001 0001 0010
+ * rightrot(x, 31)
+ * 0x1fd66224 0001 1111 1101 0110 0110 0010 0010 0100
+ *
+ * 0xa0b0c118 2 0x282c3046
+ * 0xa0b0c118 1010 0000 1011 0000 1100 0001 0001 1000
+ * rightrot(x, 2)
+ * 0x282c3046 0010 1000 0010 1100 0011 0000 0100 0110
+ *
+ * 0x001992ba 17 0xc95d000c
+ * 0x001992ba 0000 0000 0001 1001 1001 0010 1011 1010
+ * rightrot(x, 17)
+ * 0xc95d000c 1100 1001 0101 1101 0000 0000 0000 1100
+ *
+ * The remainder come from my old KNR test cases
+ *
+ * 0x1ba83dea 17 0x1ef50dd4
+ * 0x1ba83dea 0001 1011 1010 1000 0011 1101 1110 1010
+ * rightrot(x, 17)
+ * 0x1ef50dd4 0001 1110 1111 0101 0000 1101 1101 0100
+ */