Assignment 2: C what you can do
Due Friday, September 19th, before midnight
The goals for this assignment are:
-
Work with binary numbers
-
Work with C structs, strings, and arrays
Update your repository
We will use the same repository as Assignment 1
First, you must accept the pull requst on your repository on Github (screenshot).
$ cd cs240-f25-classwork
$ git pull
Your repository should now contain a new folder named A02
.
1. Compute
Write a program, compute.c
, that simulates bitwise addition and subtraction between 4 bit numbers.
This program should implement a function add
with the following function signature.
#define TYPE_SIZE 4
int add(int a[TYPE_SIZE], int b[TYPE_SIZE], int c[TYPE_SIZE], int carryin)
The function add
computes c = a + b
. The carryin stores an additional bit
to be added to the least significant bits of a and b. The function returns
the carry out bit.
$ make compute
gcc -g -Wno-unused-variable -Wno-unused-but-set-variable -Wl,-rpath=. compute.c -o compute
$ ./compute 2 - -2
a: 2 = 0010
b: -2 = 1110
result: 0100
Value as an unsigned integer (carryout = 0): 4
Value as a signed integer (carryout = 0): 4
$ ./compute 2 + -2
a: 2 = 0010
b: -2 = 1110
result: 0000
Value as an unsigned integer (carryout = 1): 0
Value as a signed integer (carryout = 1): 0
$ ./compute 2 + 2
a: 2 = 0010
b: 2 = 0010
result: 0100
Value as an unsigned integer (carryout = 0): 4
Value as a signed integer (carryout = 0): 4
$ ./compute 2 + 9
a: 2 = 0010
b: 9 = 1001
result: 1011
Value as an unsigned integer (carryout = 0): 11
Value as a signed integer (carryout = 0): -5
$ ./compute 2
usage: ./compute
$ ./compute 2 apl 9
Invalid operator
$ ./compute 2 9
usage: ./compute
Requirements:
-
Your program must use command line arguments with form <num1> <op> <num2>
-
Your program should support both + and - operations
-
Your program should produce similar output:
-
Show the binary representations of the inputs
-
Should the output in binary and as unsigned and signed integers
-
2. Subtext
Write a program, decode.c
, that reads a binary file containing a sequence of characters
as a series of bytes as follows.
For each byte:
-
The highest order bit of each bytes combines into a secret message, terminated with
\0
. -
The lower order 7 bits correspond to standard ASCII characters.
For example, if we visualize the contents of the file, simple-secret.txt
using hexedit,
we see the following.
00000000 41 42 C3 44 45 46 47 C8 49 4A 4B 4C 4D 4E 4F 50 0A AB.DEFG.IJKLMNOP.
The leftmost column should the offset of the row from the beginning of the file. The middle
columns show the file contents as hexadecimal numbers. And the rightmost file shows the contents of
the file in ASCII. From this output, we can see that the highest order bit of each character
in the file is 0010 0001 0000 0000 0
. The final character, 0A, is the newline character \n
.
0010 0001
, or 0x21
, corresponds to the character !
and 0x0
corresponds to the terminating
character \0
. Thus, the secret text is !\0
. To regain
the main text, we must reset the highest order bit of each byte to 0. This gives us the following
hexadecimal values 41 42 83 44 45 46 47 48 49 4A 4B 4C 4D 4E 4F 50
which correspond to the ASCII
values "ABCDEFGHIJKLMNOP"
$ make decode
gcc -g -Wno-unused-variable -Wno-unused-but-set-variable -Wl,-rpath=. decode.c -o decode
$ ./decode simple-secret.txt
The secret message:
!
The main message:
ABCDEFGHIJKLMNOP
2.1. Extra credit (0.3)
In the file, encode.c
, implement a utility that creates a text file that contains hidden subtext.
Include in your submission files that you generated with your utility.
Submit your Work
Push you work to Github to submit your work.
$ cd A02
$ git add *.c
$ git commit -m "A02 complete"
$ git push
Grading Rubric
Assignment rubrics
Grades are out of 4 points.
-
(2 point) compute
-
(0.1 points) style
-
(0.9 points) correct behavior: asks the user for input and creates the new string
-
(1.0 points) no memory errors
-
-
(2 points) decode
-
(0.1 points) style
-
(0.9 points) correct behavior
-
(1.0 points) no memory errors
-
Code rubrics
For full credit, your C programs must be feature-complete, robust (e.g. run without memory errors or crashing) and have good style.
-
Some credit lost for missing features or bugs, depending on severity of error
-
-12.5% for style errors. See the class coding style here.
-
-50% for memory errors
-
-100% for failure to checkin work to Github
-
-100% for failure to compile on linux using make