From 552bc70b77d4fca54929b46190128721b93d887c Mon Sep 17 00:00:00 2001 From: Adam Carpenter Date: Tue, 23 Jul 2019 12:09:24 -0400 Subject: Cleaned up directory. --- ch2/2-02.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ ch2/2-03.c | 42 ++++++++++++++++++++++++++++++++++++++++++ ch2/2-04.c | 43 +++++++++++++++++++++++++++++++++++++++++++ ch2/2-05.c | 20 ++++++++++++++++++++ ch2/2-06.c | 22 ++++++++++++++++++++++ ch2/2-07.c | 19 +++++++++++++++++++ ch2/2-08.c | 21 +++++++++++++++++++++ ch2/2-09.c | 40 ++++++++++++++++++++++++++++++++++++++++ ch2/2-10.c | 0 ch2/2-2.c | 46 ---------------------------------------------- ch2/2-3.c | 42 ------------------------------------------ ch2/2-4.c | 43 ------------------------------------------- ch2/2-5.c | 20 -------------------- ch2/2-6.c | 22 ---------------------- ch2/2-7.c | 19 ------------------- ch2/2-8.c | 21 --------------------- ch2/2-9.c | 40 ---------------------------------------- 17 files changed, 253 insertions(+), 253 deletions(-) create mode 100644 ch2/2-02.c create mode 100644 ch2/2-03.c create mode 100644 ch2/2-04.c create mode 100644 ch2/2-05.c create mode 100644 ch2/2-06.c create mode 100644 ch2/2-07.c create mode 100644 ch2/2-08.c create mode 100644 ch2/2-09.c create mode 100644 ch2/2-10.c delete mode 100644 ch2/2-2.c delete mode 100644 ch2/2-3.c delete mode 100644 ch2/2-4.c delete mode 100644 ch2/2-5.c delete mode 100644 ch2/2-6.c delete mode 100644 ch2/2-7.c delete mode 100644 ch2/2-8.c delete mode 100644 ch2/2-9.c diff --git a/ch2/2-02.c b/ch2/2-02.c new file mode 100644 index 0000000..52b0a2e --- /dev/null +++ b/ch2/2-02.c @@ -0,0 +1,46 @@ +#include + + +#define MAX_LEN 10 + + +/* + * Reads STDIN into string s. Is allowed to use && and ||. + */ +void getline_with_ops(char s[]) { + char c; + int i; + int lim = MAX_LEN; + + for (i = 0; i < lim - 1 && (c = getchar()) != '\n' && c != EOF; ++i) + s[i] = c; + + s[i] = '\0'; +} + + +/* + * Reads STDIN into string s. Is not allowed to use && or ||. + */ +void getline_without_ops(char s[]) { + char c; + int i; + int lim = MAX_LEN; + + for (i = 0; i < lim - 1; ++i) + if ((c = getchar()) != '\n') + if (c != EOF) + s[i] = c; + + s[i] = '\0'; +} + + +int main() { + char s[MAX_LEN]; + + //getline_with_ops(s); + getline_without_ops(s); + + printf("%s\n", s); +} diff --git a/ch2/2-03.c b/ch2/2-03.c new file mode 100644 index 0000000..54ce89f --- /dev/null +++ b/ch2/2-03.c @@ -0,0 +1,42 @@ +#include + + +unsigned int htoi(char hex[]) { + char c; + int i; + int length; + int power; + unsigned int result; + + for (length = 0; hex[length] != '\0'; ++length) + + power = 1; + result = 0; + + for (i = length - 1; i >= 0; --i) { + c = hex[i]; + printf("%c\n", c); + + if (c >= 'a' && c <= 'f') + c = c - 'a' + 10; + else if (c >= 'A' && c <= 'F') + c = c - 'A' + 10; + else if (c >= '0' && c <= '9') + c = c - '0'; + else if (c == 'x' || c == 'X') + break; + else + return 0; + + result += c * power; + power *= 16; + } + + return result; +} + + +int main() { + char hex[] = "0x589"; + printf("%d\n", htoi(hex)); +} diff --git a/ch2/2-04.c b/ch2/2-04.c new file mode 100644 index 0000000..3f122bb --- /dev/null +++ b/ch2/2-04.c @@ -0,0 +1,43 @@ +#include + + +void squeeze_char(char s[], char c) { + int i; + int j; + + for (i = j = 0; s[i] != '\0'; i++) + if (s[i] != c) + s[j++] = s[i]; + + s[j] = '\0'; +} + + +void squeeze_str(char s1[], char s2[]) { + int i; + + for (i = 0; s2[i] != '\0'; ++i) + squeeze_char(s1, s2[i]); +} + + +void squeeze_all(char s1[], char s2[]) { + int i; + int j; + int k; + + for (k = 0; s2[k] != '\0'; ++k) { + for (i = j = 0; s1[i] != '\0'; ++i) + if (s1[i] != s2[k]) + s1[j++] = s1[i]; + + s1[j] = '\0'; + } +} + + +int main() { + char squeezed[] = "abracadabra"; + squeeze_all(squeezed,"abc"); + printf("%s\n", squeezed); +} diff --git a/ch2/2-05.c b/ch2/2-05.c new file mode 100644 index 0000000..495473c --- /dev/null +++ b/ch2/2-05.c @@ -0,0 +1,20 @@ +#include + +int any(char s1[], char s2[]) { + int i; + int j; + + for (i = 0; s2[i] != '\0'; ++i) + for (j = 0; s1[j] != '\0'; ++j) + if (s2[i] == s1[j]) + return j; + + return -1; +} + +int main() { + char test_str[] = "blargh"; + char test_chars[] = "cab"; + int result = any(test_str, test_chars); + printf("%d\n", result); +} diff --git a/ch2/2-06.c b/ch2/2-06.c new file mode 100644 index 0000000..918f583 --- /dev/null +++ b/ch2/2-06.c @@ -0,0 +1,22 @@ +#include + +/* + * Set n bits in x starting at position p to rightmost n bits of y. + */ + +unsigned setbits(unsigned x, unsigned p, unsigned n, unsigned y) { + // Create a basa from x that zeroes out all of the bits to be set. + unsigned a = x & ~(~(~0 << n) << (p + 1 - n)); + + // Create a mask from y that zeroes out all of the bits except those to set. + unsigned b = (y & ~(~0 << n)) << (p + 1 - n); + + // Apply the mask to the base. + return a | b; +} + +int main() { + printf("%x\n", setbits(0xaa, 3, 3, 0x33)); + printf("%x\n", setbits(0x00, 3, 3, 0xff)); + return 0; +} diff --git a/ch2/2-07.c b/ch2/2-07.c new file mode 100644 index 0000000..bf9fed1 --- /dev/null +++ b/ch2/2-07.c @@ -0,0 +1,19 @@ +#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. + */ + +unsigned invert(unsigned x, unsigned p, unsigned n) { + // Create mask where bits should be flipped. + unsigned mask = ~(~0 << n) << (p + 1 - n); + + // Flip bits with mask. + return x ^ mask; +} + +int main() { + printf("%x\n", invert(0xaa, 3, 3)); + printf("%x\n", invert(0xff, 3, 3)); +} diff --git a/ch2/2-08.c b/ch2/2-08.c new file mode 100644 index 0000000..9f7adbd --- /dev/null +++ b/ch2/2-08.c @@ -0,0 +1,21 @@ +#include + +/* + * Returns the value of the integer x rotated to the right by n bit positions. + */ + +unsigned rightrot(unsigned x, int n) { + unsigned mask = ~(~0 >> 1); + + while (n-- > 0) + if (x & 1) + x = (x >> 1) | mask; + else + x >>= 1; + + return x; +} + +int main() { + printf("%x\n", rightrot(0xaa, 3)); +} diff --git a/ch2/2-09.c b/ch2/2-09.c new file mode 100644 index 0000000..5ab3241 --- /dev/null +++ b/ch2/2-09.c @@ -0,0 +1,40 @@ +#include + +int bitcount_original(unsigned x); +int bitcount(unsigned x); + +int main() { + printf("%d\n", bitcount_original(0xaa)); + printf("%d\n", bitcount(0xaa)); + return 0; +} + +/* + * A binary subtraction operation will toggle the least-significant bits + * required for the subtraction. The `&` operation between two opposing bits + * will always result in 0. This is why using `x &= (x - 1)` will always delete + * the rightmost 1-bit in x; the least-significant 1-bit and all 1-bits + * generated from the subtraction operation will be zeroed out. + * + * This observation makes this implementation more efficient than + * `bitcount_original` simply because it requires fewer operations. + */ +int bitcount(unsigned x) { + int b; + + for (b = 0; x > 0; b++) + x &= (x - 1); + + return b; +} + +int bitcount_original(unsigned x) { + int b; + + for (b = 0; x != 0; x >>= 1) + if (x & 01) + b++; + + return b; +} + diff --git a/ch2/2-10.c b/ch2/2-10.c new file mode 100644 index 0000000..e69de29 diff --git a/ch2/2-2.c b/ch2/2-2.c deleted file mode 100644 index 52b0a2e..0000000 --- a/ch2/2-2.c +++ /dev/null @@ -1,46 +0,0 @@ -#include - - -#define MAX_LEN 10 - - -/* - * Reads STDIN into string s. Is allowed to use && and ||. - */ -void getline_with_ops(char s[]) { - char c; - int i; - int lim = MAX_LEN; - - for (i = 0; i < lim - 1 && (c = getchar()) != '\n' && c != EOF; ++i) - s[i] = c; - - s[i] = '\0'; -} - - -/* - * Reads STDIN into string s. Is not allowed to use && or ||. - */ -void getline_without_ops(char s[]) { - char c; - int i; - int lim = MAX_LEN; - - for (i = 0; i < lim - 1; ++i) - if ((c = getchar()) != '\n') - if (c != EOF) - s[i] = c; - - s[i] = '\0'; -} - - -int main() { - char s[MAX_LEN]; - - //getline_with_ops(s); - getline_without_ops(s); - - printf("%s\n", s); -} diff --git a/ch2/2-3.c b/ch2/2-3.c deleted file mode 100644 index 54ce89f..0000000 --- a/ch2/2-3.c +++ /dev/null @@ -1,42 +0,0 @@ -#include - - -unsigned int htoi(char hex[]) { - char c; - int i; - int length; - int power; - unsigned int result; - - for (length = 0; hex[length] != '\0'; ++length) - - power = 1; - result = 0; - - for (i = length - 1; i >= 0; --i) { - c = hex[i]; - printf("%c\n", c); - - if (c >= 'a' && c <= 'f') - c = c - 'a' + 10; - else if (c >= 'A' && c <= 'F') - c = c - 'A' + 10; - else if (c >= '0' && c <= '9') - c = c - '0'; - else if (c == 'x' || c == 'X') - break; - else - return 0; - - result += c * power; - power *= 16; - } - - return result; -} - - -int main() { - char hex[] = "0x589"; - printf("%d\n", htoi(hex)); -} diff --git a/ch2/2-4.c b/ch2/2-4.c deleted file mode 100644 index 3f122bb..0000000 --- a/ch2/2-4.c +++ /dev/null @@ -1,43 +0,0 @@ -#include - - -void squeeze_char(char s[], char c) { - int i; - int j; - - for (i = j = 0; s[i] != '\0'; i++) - if (s[i] != c) - s[j++] = s[i]; - - s[j] = '\0'; -} - - -void squeeze_str(char s1[], char s2[]) { - int i; - - for (i = 0; s2[i] != '\0'; ++i) - squeeze_char(s1, s2[i]); -} - - -void squeeze_all(char s1[], char s2[]) { - int i; - int j; - int k; - - for (k = 0; s2[k] != '\0'; ++k) { - for (i = j = 0; s1[i] != '\0'; ++i) - if (s1[i] != s2[k]) - s1[j++] = s1[i]; - - s1[j] = '\0'; - } -} - - -int main() { - char squeezed[] = "abracadabra"; - squeeze_all(squeezed,"abc"); - printf("%s\n", squeezed); -} diff --git a/ch2/2-5.c b/ch2/2-5.c deleted file mode 100644 index 495473c..0000000 --- a/ch2/2-5.c +++ /dev/null @@ -1,20 +0,0 @@ -#include - -int any(char s1[], char s2[]) { - int i; - int j; - - for (i = 0; s2[i] != '\0'; ++i) - for (j = 0; s1[j] != '\0'; ++j) - if (s2[i] == s1[j]) - return j; - - return -1; -} - -int main() { - char test_str[] = "blargh"; - char test_chars[] = "cab"; - int result = any(test_str, test_chars); - printf("%d\n", result); -} diff --git a/ch2/2-6.c b/ch2/2-6.c deleted file mode 100644 index 918f583..0000000 --- a/ch2/2-6.c +++ /dev/null @@ -1,22 +0,0 @@ -#include - -/* - * Set n bits in x starting at position p to rightmost n bits of y. - */ - -unsigned setbits(unsigned x, unsigned p, unsigned n, unsigned y) { - // Create a basa from x that zeroes out all of the bits to be set. - unsigned a = x & ~(~(~0 << n) << (p + 1 - n)); - - // Create a mask from y that zeroes out all of the bits except those to set. - unsigned b = (y & ~(~0 << n)) << (p + 1 - n); - - // Apply the mask to the base. - return a | b; -} - -int main() { - printf("%x\n", setbits(0xaa, 3, 3, 0x33)); - printf("%x\n", setbits(0x00, 3, 3, 0xff)); - return 0; -} diff --git a/ch2/2-7.c b/ch2/2-7.c deleted file mode 100644 index bf9fed1..0000000 --- a/ch2/2-7.c +++ /dev/null @@ -1,19 +0,0 @@ -#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. - */ - -unsigned invert(unsigned x, unsigned p, unsigned n) { - // Create mask where bits should be flipped. - unsigned mask = ~(~0 << n) << (p + 1 - n); - - // Flip bits with mask. - return x ^ mask; -} - -int main() { - printf("%x\n", invert(0xaa, 3, 3)); - printf("%x\n", invert(0xff, 3, 3)); -} diff --git a/ch2/2-8.c b/ch2/2-8.c deleted file mode 100644 index 9f7adbd..0000000 --- a/ch2/2-8.c +++ /dev/null @@ -1,21 +0,0 @@ -#include - -/* - * Returns the value of the integer x rotated to the right by n bit positions. - */ - -unsigned rightrot(unsigned x, int n) { - unsigned mask = ~(~0 >> 1); - - while (n-- > 0) - if (x & 1) - x = (x >> 1) | mask; - else - x >>= 1; - - return x; -} - -int main() { - printf("%x\n", rightrot(0xaa, 3)); -} diff --git a/ch2/2-9.c b/ch2/2-9.c deleted file mode 100644 index 5ab3241..0000000 --- a/ch2/2-9.c +++ /dev/null @@ -1,40 +0,0 @@ -#include - -int bitcount_original(unsigned x); -int bitcount(unsigned x); - -int main() { - printf("%d\n", bitcount_original(0xaa)); - printf("%d\n", bitcount(0xaa)); - return 0; -} - -/* - * A binary subtraction operation will toggle the least-significant bits - * required for the subtraction. The `&` operation between two opposing bits - * will always result in 0. This is why using `x &= (x - 1)` will always delete - * the rightmost 1-bit in x; the least-significant 1-bit and all 1-bits - * generated from the subtraction operation will be zeroed out. - * - * This observation makes this implementation more efficient than - * `bitcount_original` simply because it requires fewer operations. - */ -int bitcount(unsigned x) { - int b; - - for (b = 0; x > 0; b++) - x &= (x - 1); - - return b; -} - -int bitcount_original(unsigned x) { - int b; - - for (b = 0; x != 0; x >>= 1) - if (x & 01) - b++; - - return b; -} - -- cgit v1.2.3