Intro to C #1: bases and bits
Starter code: bases-and-bits-package.tar
Upload via Moodle only your completed copy of bases-and-bits.c
Goals
- Get started with C
- Start thinking about integers, bases, and bits
Rubric
Programming assignment #1: bases-and-bits
For this assignment, you will implement two C functions involving
the C int type. One of these functions, void print_int_in_binary(int n),
once implemented, will do just as its name says. The other,
int count_one_bits(int n) will count the number of 1 bits found in
the binary representation of n.
Neither function requires a lot of code, but both will require you to think clearly about the binary representation of integers, and how that representation can be accessed directly in C.
A few concepts and C constructs you might find useful for this assignment:
printf, including the format specifiers%d,%o, and%xifandelse- loops (
whileandfor) - the bitwise AND operator:
& - (not necessary, but possibly handy) the mod operator:
%
Homework packages for this class
Several of the assignments for this class will include a package of starter code plus other infrastructure. I will generally deliver each package to you as a tar file, which is an old-school but very common way of sharing a collection of documents as a single file.
Part of the goal of this assignment is to get you familiar with the code packages we will use.
What you should do
- Grab a copy of bases-and-bits-package.tar and extract its contents.
- Read the
readme.txtfile. - Try running the starter code as-is:
make test - Read the
Makefileand see if you can make any sense of it, and how it causedmake testto do what it did. - Read the code in
bases-and-bits.handbases-and-bits.c. - Based on the instructions in
bases-and-bits.h, write your implementations of the functions inbases-and-bits.c. You can runmake testany time to see whether your program is compiling, running, and getting one simple test case right. - Devise your own tests (even if you ignore
makeand just runbases-and-bitsmanually). Don't assume that getting the "27" test case right means your code is correct for all the cases. - Done? Submit your copy of
bases-and-bits.cand only that file to Moodle.
Have fun!
Don't by shy about experimenting and asking lots of questions.