EECS 61C, Spring 2007
Due Wednesday, Jan 24, 2007 @ 11:59pm
This assignment will give you practice compiling and executing C programs; it checks your understanding of the material in P&H Chapter 1, and number representation.
Submit your solution by creating a directory named hw1 that contains files named cod.txt, base2.print.c, bitcount.c, and cod.txt. (Note that capitalization matters in file names; the submission program will not accept your submission if your file names differ at all from those specified.) From within that directory, type "submit hw1". This is not a partnership assignment; hand in your own work.
$ mkdir ~/hw $ cp -r ~cs61c/hw/01/ ~/hwAll the files referred in this homework will be in the folder you copied.
/* Name: Lab Login: */ #include <stdio.h> int main ( ) { unsigned int numToPrintInBase2 = 1431655765; /* alternating 1's and 0's */ unsigned int exp = 1; int k; /* can't declare variable in a loop header */ /* Compute the highest storable power of 2 (2 to the 31th). */ for (k=0; k<31; k++) { exp = exp * 2; } /* For each power of 2 from the highest to the lowest, print 1 if it occurs in the number, 0 otherwise. */ for (k=31; !(k=0); k--) { if (numToPrintInBase2 >= exp) { printf ("%d", '1'); numToPrintInBase2 = numToPrintInBase2 - exp; } else { printf ("%d", '0'); } exp = exp / 2; } printf ("\n\n"); return 0; }You should take advantage of this opportunity to learn more about the gdb debugger if you're not already familiar with it.
bitCount()
in bitcount.c
that returns the number of 1-bits in the binary representation of its unsigned integer argument. Remember to fill in the identification information and run the completed program to verify correctness.
/* Name: Lab section time: */ #include <stdio.h> int bitCount (unsigned int n); int main ( ) { printf ("# 1-bits in base 2 representation of %u = %d, should be 0\n", 0, bitCount (0)); printf ("# 1-bits in base 2 representation of %u = %d, should be 1\n", 1, bitCount (1)); printf ("# 1-bits in base 2 representation of %u = %d, should be 16\n", 2863311530u, bitCount (2863311530u)); printf ("# 1-bits in base 2 representation of %u = %d, should be 1\n", 536870912, bitCount (536870912)); printf ("# 1-bits in base 2 representation of %u = %d, should be 32\n", 4294967295u, bitCount (4294967295u)); return 0; } int bitCount (unsigned int n) { /* your code here */ }