#include #include /** * bits - fun printing the bits of a number. */ /** * Procedure: * show_int_bits * Parameters: * i, an integer * Purpose: * Show all the bits in i * Produces: * [Nothing, called for the side effect.] * Preconditions: * [No additional] * Postconditions: * 32 bits (character 0 or character 1) appear on output. * (Explain output.) */ void show_int_bits (int i) { int j; int current; // For each bit position for (j = 31; j >= 0; j--) { // Check what the bit at position j is // Need the number corresponding to 00000100000000000, that is, // 1 followed by j 0's // Can do it with expontentiation, but that's wasteful current = 0x1 << j; if ((i & current) == 0) printf ("0"); else printf ("1"); } } // show_int_bits /** * main */ int main (int argc, char *argv[]) { // Grab element 1 of argv as an integer int i = atoi (argv[1]); printf ("%d is ", i); show_int_bits (i); printf ("\n"); return EXIT_SUCCESS; } // main