/* bitwise.c Jeff Ondich, 9 Jan 2022 Modified by Tanya Amert for Fall 2024 Explore some bitwise logical operations (|, &, >>). Compile as usual: gcc -Wall -Werror -g -o bitwise bitwise.c Run with no command-line arguments: ./bitwise */ #include void print_binary(int n, FILE *stream); void experiment1(); void experiment2(); void experiment3(); void experiment4(); void experiment5(); void experiment6(); // Different experiments to play around with bitwise operations. int main(int argc, char *argv[]) { experiment1(); experiment2(); experiment3(); experiment4(); experiment5(); experiment6(); return 0; } void experiment1() { // Experiment 1 printf("===== 1. Bitwise OR to turn a bit on =====\n"); int a = 0x00000023; int bit = 0x00000080; int b = a | bit; printf("a=%d, bit=%d, b=%d\n", a, bit, b); printf("a = "); print_binary(a, stdout); printf("\nbit = "); print_binary(bit, stdout); printf("\nb = "); print_binary(b, stdout); printf("\n\n"); } void experiment2() { // Experiment 2 printf("===== 2. Bitwise AND to turn a bit off =====\n"); int a = 0x00000023; int mask = 0xFFFFFFFD; int b = a & mask; printf("a=%d, mask=%d, b=%d\n", a, mask, b); printf("a = "); print_binary(a, stdout); printf("\nmask = "); print_binary(mask, stdout); printf("\nb = "); print_binary(b, stdout); printf("\n\n"); } void experiment3() { // Experiment 3 printf("===== 3. Bitwise AND to mask out all but lower-order byte =====\n"); int a = 0x1234ABCD; int mask = 0x000000FF; int b = a & mask; printf("a=%d, mask=%d, b=%d\n", a, mask, b); printf("a = "); print_binary(a, stdout); printf("\nmask = "); print_binary(mask, stdout); printf("\nb = "); print_binary(b, stdout); printf("\n\n"); } void experiment4() { // Experiment 4 printf("===== 4. Bit shift to move middle bits to low-order bits =====\n"); int a = 0x1234ABCD; int shift = 8; int b = a >> shift; printf("a=0x%X, shift=0x%X, b=0x%X\n", a, shift, b); printf("a = "); print_binary(a, stdout); printf("\nshift = "); print_binary(shift, stdout); printf("\nb = "); print_binary(b, stdout); printf("\n\n"); } void experiment5() { // Experiment 5 printf("===== 5. What if a is negative? =====\n"); int a = 0x8765ABCD; int shift = 8; int b = a >> shift; printf("a=0x%X, shift=0x%X, b=0x%X\n", a, shift, b); printf("a = "); print_binary(a, stdout); printf("\nshift = "); print_binary(shift, stdout); printf("\nb = "); print_binary(b, stdout); printf("\n\n"); } void experiment6() { // Experiment 6 printf("===== 6. Building a mask =====\n"); int a = 0x1234ABCD; // I want to zero out bits 8-11 (i.e. the B in that hexadecimal) int inverted_mask = 0x00000F00; // the bits I want to squash are the four bits (8-11) covered by the F int mask = ~inverted_mask; // this bitwise NOT converts 0x0000F000 to 0xFFFF0FFF printf("a=0x%X, inverted_mask=0x%X, mask=0x%X\n", a, inverted_mask, mask); printf("a & mask: 0x%X\n", a & mask); printf("\n\n"); } // Print out the binary version of a given integer. // // Parameters: // - n: integer value to print as binary // - stream: where to print void print_binary(int n, FILE *stream) { int bits_printed = 0; for (unsigned int k = 0x80000000; k != 0; k = (k >> 1)) { char bit = (k & n) != 0 ? '1' : '0'; fputc(bit, stream); bits_printed++; if ((bits_printed % 4) == 0) { fputc(' ', stream); } } }