Skip to content

Instantly share code, notes, and snippets.

@drguildo
Last active September 16, 2020 21:39
Show Gist options
  • Save drguildo/4273bef094e163b26163e6106967b929 to your computer and use it in GitHub Desktop.
Save drguildo/4273bef094e163b26163e6106967b929 to your computer and use it in GitHub Desktop.
Solution to Practice Problem 2.13 in Computer Systems: A Programmer's Perspective.
#include <stdio.h>
void dump(unsigned int i) {
printf("%#08x = %d\n", i, i);
}
/* Declarations of functions implementing operations bis and bic */
int bis(int x, int m) {
return x | m;
}
int bic(int x, int m) {
return x & ~m;
}
/* Compute x|y using only calls to functions bis and bic */
int bool_or(int x, int y) {
int result = bis(x, y);
return result;
}
/* Compute x^y using only calls to functions bis and bic */
int bool_xor(int x, int y) {
int result = bis(bic(x, y), bic(y, x));
return result;
}
int main() {
dump(bool_or(0xFF, 0xFF00));
dump(bool_or(0xFF, 0xFF));
dump(bool_or(0xFE, 0xFF));
dump(bool_xor(0xFF, 0xFF00));
dump(bool_xor(0xFF, 0xFF));
dump(bool_xor(0xFF, 0x01));
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment