P11: Binary count
Goals
- Get started writing C
Objective
You have been watching me write C code for a couple of weeks – now it is time for you to try it out.
For this problem, you will write a program that counts in binary. The user will enter a number and you will count from 0 to that number in binary.
Here is an example run:
$ ./problem11 6
000
001
010
011
100
101
110
Here is another
$ ./problem11 16
00000
00001
00010
00011
00100
00101
00110
00111
01000
01001
01010
01011
01100
01101
01110
01111
10000
And one more
./problem11
Usage: ./problem11 <number>
Requirements
- The user can enter in any integer as a command line argument as the end point of the count
- The program will print out every number from 0 up to and including the entered number in binary
- Each number will be on its own line
- All numbers should be displayed with the same number of bits, which should be just enough to represent the largest number
- If no number is entered on the command line, the program should print out the usage message shown above. Note that the
./problem11is the name of the executable and may vary. Your usage message should show the actual executable’s name. - At the top of your C file, there should be a multiline comment that includes your name, the date, instructions for compiling the code and instructions for running it
- Your solution should involve bitwise operations
- On a valid run, your program should return 0 (this is usually how we indicate “completed with no errors”)
Implementation details and tips
Since this is your first C program, I’ll give you some tips to get you started.
Working over SSH
I have shown you two ways to work over SSH. You can open a terminal program and connect directly using ssh (e.g., `ssh username@basin.cs.middlebury.edu, where username is your Middlebury user name). If you go this route, you need to use one of the editors installed on basin like vim or emacs.
Alternatively, you can connect directly from VSCode and do everything from within your editor window.
- Directions for connecting to basin (including using ssh)
- Directions for setting up VSCode to connect with basin
Argument parsing
As I said in class, the arguments to main give us access to the command line arguments.
argc- the number of argumentsargv- an array of strings
The first argument (argv[0]) is always the string used to invoke the program.
All of the arguments are strings – even if they were typed as numbers, and there is no automagic conversions.
To convert numeric strings to numbers, I recommend the string to long function strtol. For the second argument (endptr), just pass NULL.
The strol function belongs to stdlib.h, so you will need a #include <stdlib.h> at the top of the file.
Math
You may want access to math functions like log or floor or ceil (that was absolutely a hint). Those functions (and others) can be found in math.h. To use them you need add #include <math.h> at the top of the file.
The math library is a little different from stdio and stdlib. You will need to add -lm as a command line argument when you compile the code in order for the math functions to be included in your code.
Approach
There are a couple of different ways that you could do this. One approach could be to make an array of 1s and 0s and just flip them in regular patterns without ever really thinking about their values.
There is an easier way to do this which I would like you to use. Integers are already being stored in binary, so if you can read the individual bits you could then print them out.
To read the bits, you will need to make use of bitwise operators
|- bitwise OR&- bitwise AND^- bitwise XOR~- bitwise NOT>>- shift right<<- shift left
You certainly won’t need all of those, but it would be good to start getting familiar with them.
Compiling
To create runnable programs, you will want to use gcc on basin.
If your program file is called program11.c then you might use
$ gcc program11.c -o program11 -lm
The -o provides the name of the executable, and since we didn’t specify a different option like -c or -S, this will do the whole sequence, compile, assemble and link. As a reminder, -lm links in the math library code.
Submission
Upload your .c file to Gradescope.