C Lab 1
due: 1 April 2010
Part 1: Bitwise XOR – 30 points
You are to implement a program that
- inputs a line (from standard in) containing 2 integers separated by one or more spaces,
- bitwise XORs the 2 values together.
- finds the least significant 7 bits using logical operators.
- prints a single line (to standard out) containing an integer representing the 7 least significant bits
- loops until Ctrl-D
Remember:
You are to XOR integers not characters.
Your program should input multiple digit numbers and not be restricted to single digits.
Suppose I have a file test.txt with the contents
12 5
3 1
198 12
200 200
2000 192
If I execute the command ./a.out < test.txt > text.out
test.out should contain:
9
2
74
0
16
Note: My current grading program is fussy about spaces. Make sure there is no space after the number.
The following code snippet may be helpful:
#include
int main()
{
int i = 0;
while(!feof(stdin))
{
scanf("", &i);
printf("%d", i);
}
}
Part 2: Music Synth
During the Christmas break I was looking at some music software someone wrote. The melody was contained in a compressed file. When I uncompressed it, the file consisted of a single 2 byte integer per line. Each integer represented one note’s pitch and duration. The bit structure of the integer is as follows:
d d d d d d d d p p p p p p p p
The p byte represents a notes pitch. Notes are numbered starting with a low C being note 0. The C above that then is represented as 12. For example, the integer 63 (00111111) represents the note 63. The d byte represents the duration of the note in 16th notes. For example, 00000100 represents a note whose duration is 4 sixteenth notes. You are to write a program that takes as input a sequence of lines as described above and outputs (to standard out) the note number and duration separated by a tab. For example, if 1087 is input you are to output
63 4
meaning Eb played for 4 sixteenth notes.
If I have my test cases in the file test.txt the following should work: ./a.out < test.txt
If I have a file test.txt containing
24639
1085
571
576
575
And execute
./a.out < test.txt > output.txt
The output file should contain
63 96
61 4
59 2
64 2
63 2
Note: there is a single tab character between the numbers and no space before the newline.
How to submit
Submit your code to the Submit-O-Matic
Points awarded:
2.5 points for each part that you submit that produces correct Submit-O-Matic results.
1 point for each part that produces correct results locally but does not produce correct results on Submit-o_Matic.
-1,000 points if your program produces an infinite loop on Submit-O-Matic