Created
April 15, 2018 16:04
-
-
Save knuu/cb28d6b4e627387f5ffcc15ea9b41245 to your computer and use it in GitHub Desktop.
k&r exercise
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <limits.h> | |
unsigned int getbits(unsigned int x, int p, int n); | |
unsigned int setbits(unsigned int x, int p, int n, unsigned int y); | |
void print_bits(unsigned int x); | |
int main() { | |
print_bits(setbits((1 << 5) - 1, 3, 3, 10)); // x = 11111, y = 1010 => 10101 | |
return 0; | |
} | |
unsigned int getbits(unsigned int x, int p, int n) { | |
return (x >> (p + 1 - n)) & ~(~0 << n); | |
} | |
unsigned int setbits(unsigned int x, int p, int n, unsigned int y) { | |
return (x & (~0 << (p + 1))) | (getbits(y, n - 1, n) << (p + 1 - n)) | getbits(x, p - n, p - n + 1); | |
} | |
void print_bits(unsigned int x) { | |
for (int i = CHAR_BIT * (int)sizeof(int) - 1; i >= 0; i--) { | |
printf("%d", x >> i & 1); | |
} | |
printf("\n"); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <limits.h> | |
unsigned int getbits(unsigned int x, int p, int n); | |
unsigned int setbits(unsigned int x, int p, int n, unsigned int y); | |
unsigned int invert(unsigned int x, int p, int n); | |
void print_bits(unsigned int x); | |
int main() { | |
print_bits(invert((1 << 5) - 1, 3, 3)); // x = 11111 -> 10001 | |
print_bits(invert(~0, 10, 5)); // x = 1..1 -> 1..100000111111 | |
return 0; | |
} | |
unsigned int getbits(unsigned int x, int p, int n) { | |
return (x >> (p + 1 - n)) & ~(~0 << n); | |
} | |
unsigned int setbits(unsigned int x, int p, int n, unsigned int y) { | |
return (x & (~0 << (p + 1))) | (getbits(y, n - 1, n) << (p + 1 - n)) | getbits(x, p - n, p - n + 1); | |
} | |
unsigned int invert(unsigned int x, int p, int n) { | |
return setbits(x, p, n, ~getbits(x, p, n)); | |
} | |
void print_bits(unsigned int x) { | |
for (int i = CHAR_BIT * (int)sizeof(int) - 1; i >= 0; i--) { | |
printf("%d", x >> i & 1); | |
} | |
printf("\n"); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <limits.h> | |
unsigned int rightrot(unsigned int x, int n); | |
void print_bits(unsigned int x); | |
int main() { | |
print_bits(rightrot(10U, 4)); // 1010....0 | |
print_bits(rightrot(10U | (~0U << 4), 4)); // 10101....1 | |
return 0; | |
} | |
unsigned int rightrot(unsigned int x, int n) { | |
for (int i = 0; i < n; i++) { | |
x = (x >> 1) | ((x & 1) << (CHAR_BIT * (int)sizeof(int) - 1)); | |
} | |
return x; | |
} | |
void print_bits(unsigned int x) { | |
for (int i = CHAR_BIT * (int)sizeof(int) - 1; i >= 0; i--) { | |
printf("%d", x >> i & 1); | |
} | |
printf("\n"); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <limits.h> | |
int bitcount(unsigned int x); | |
int bitcount_faster(unsigned int x); | |
int main() { | |
int N = 329482135U; | |
printf("%d, %d\n", bitcount(N), bitcount_faster(N)); | |
return 0; | |
} | |
int bitcount(unsigned int x) { | |
int b; | |
for (b = 0; x != 0; x >>= 1) { | |
if (x & 1) b++; | |
} | |
return b; | |
} | |
int bitcount_faster(unsigned int x) { | |
int b; | |
for (b = 0; x != 0; x &= x - 1) { | |
b++; | |
} | |
return b; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment