Part 2 (programming parts):
due by 11:59pm Thursday, Sept. 11
This lab assignment includes a written part that you will hand in at the beginning of class on Thursday and the programming part that you will submit electronically by Thursday evening using git. Use the setup31 script to create the appropriate directories for your first labs/01 homework folder. Note: if you did this already in lab, you can skip this step.
[~]$ cd [~]$ setup31 labs/01 [~]$ cd ~/cs31/labs/01 [~]$ cp ~adanner/public/cs31/labs/01/* ./ [~]$ ls Makefile lab1.cNow add the files you just copied to git. If you need to get the original copies back if you make a mistake, we can show you how to retrieve the files from git.
[~]$ cd ~/cs31/labs/01 [~]$ git add Makefile lab1.c [~]$ git commit -m "lab1 starter code" [~]$ git push
$ lpr filename $ enscript -2rG filenameIf you write up your answers in vim (or emacs) make sure to not have lines longer than 80 characters (explicitly hit the Enter key to start a new line). If you have lines longer than 80 characters they will either be cut off by the printer or wrapped strangely. One way to ensure this is to work in a terminal 80 characters wide when you run vim so you can see when the currently line is too long and starts to wrap around. Typing in CNTRL-L in your file will print a page break:
hello1 # this will print on the first page ^L # this is typed in using CNTRL-L hello2 # this will print on the second page
For these problems you are graded on showing how you applied the operation or conversion method we described in class: you must show your work or explain how you got the result to receive credit. Check your answers for correctness by either writing a C program to do some of the computation and printing result values or by using gdb's print command. See the weekly lab page for details on using gdb.
Answer the questions below showing your work and/or explaining your answer. For these questions, if a question specifies a number of bits, your answer should be in a corresponding number of digits. For example, if the question asks to add 4 bit values together your answer should be a 4 bit value not a 64 bit one. Also, assume that for signed values, the high-order bit is the sign bit. For example, 1000 should be interpreted as negative as a 4-bit signed value, but positive as an 8-bit signed value (00001000).
value 1: 01011101 value 2: 01100101
$ ./lab1 Question 1: my answer to question 1 is ... This can be verified by examining the result of the expression ... when x is the int value ... and y is ... the expression is ... when x is ... and y is ... the expression is ... Question 2: my answer to question 2 is ... This can be verified by ...Each answer should include printing out the value(s) of COMPUTATION(s) that demonstrates your answer's correctness. DON'T just print something like this:
printf("The answer to question 1, what 0x2 + 0x6, is 0x8\n");Instead, have C code that computes the answer to show or to prove that your answer is correct:
unsigned x, y, z; x = 0x2; y = 0x6; z = x+y; printf("The answer to question 1, what %x + %x is %x\n", x, y, z);For some questions, the code proving your answer correct may be as simple as the example above. For others, however, you will have to think about how to constructing some arithmetic expressions that will demonstrate the correctness of your answer.
You may want to try printing some values and expressions in gdb in binary, hexadecimal, and decimal to help you figure out good values to test in your C program to ensure you considering all cases.
C's bit-wise OR and C's logical OR when applied to the same two int or unsigned values both result in the identical int or unsigned values (i.e. the value of (a || b) is equal to the value of (a | b) for all values of a and b). (T or F)? Give at least two examples to support your answer.
l = x && y; // && is C's logical AND operator b = x & y; // & is C's bitwise AND operator
int main() { question1(); // call the question1 function question2(); ...And, you are welcome to add additional helper functions.
int x; x = (3 && 5) printf("%d\n", x);
int x, y; x = 3; y = 6; printf ("%d + %d = %d\n", x, y, (x+y));
Use git add, git commit and git push to submit your changes. You should probably only need to add lab1.c
[~]$ cd ~/cs31/labs/01 [01]$ git add lab1.c [01]$ git commit -m "finished lab" [01]$ git push
I can only see and grade changes that you push. Git will only push changes you commit. Git will only commit things you add. Git command will only work in your ~/cs31/labs/01 directory, so be sure your are in the right path when you submit. Use git status to see if you need to perform any operations. Do not submit your compiled binary files. It is a best practice to run make clean before running git add. You can run git add, commit and push multiple times
Below is a detailed run of using git status to guide the changes you should make. Let's start by running make clean
[~]$ cd ~/cs31/labs/01 [01]$ ls lab1* lab1.c Makefile [01]$ make clean rm -f lab1 [01]$ ls lab1.c MakefileNot the binary file lab1 was removed. That's OK. It is built automatically and so we don't need to save changes to that file or maintain it under version control. You only need to add and commit source files like .c, .h, .txt, or Makefiles. Let's run git status and see what we need to do.
[01]$ git status On branch master Changes not staged for commit: (use "git addOh, we made changes to lab1.c. Since that probably has the solutions to our lab assignment, we should definitely add, commit, and push that. Start with git add and check the status..." to update what will be committed) (use "git checkout -- ..." to discard changes in working directory) modified: lab1.c no changes added to commit (use "git add" and/or "git commit -a")
[01]$ git add lab1.c [01]$ git status: On branch master Changes to be committed: (use "git reset HEADSo now git sees that changes to lab1.c are ready to commit. We just need to run git commit with a descriptive message of what we did...." to unstage) modified: lab1.c
[01]$ git commit -m "finished lab" [master 2102244] finished lab 1 file changed, 1 insertion(+), 1 deletion(-) [01]$ git status On branch master Your branch is ahead of 'origin/master' by 2 commits. (use "git push" to publish your local commits) nothing to commit, working directory cleanThe new status says there is nothing to commit, which is a good sign. It also says that we are ahead of 'origin/master' by 2 commits., meaning we are out of sync with the git repository that the instructor can see. Let's push so we can get credit for our hard work.
[01]$ git push Counting objects: 8, done. Delta compression using up to 16 threads. Compressing objects: 100% (6/6), done. Writing objects: 100% (6/6), 584 bytes | 0 bytes/s, done. Total 6 (delta 4), reused 0 (delta 0) To /home/adas/share/cs31_f14/labs/01 b86bb63..2102244 master -> master [01]$ git status On branch master Your branch is up-to-date with 'origin/master'. nothing to commit, working directory cleanThese last three lines of "up-to-date" and "nothing to commit" are what we want to see. Note it may be ok if you are "up-to-date" but have some "Untracked files". This usually happens when you have the binary executable lab1 in your directory.
[01]$ make [01]$ ls lab1* lab1.c Makefile [01]$ git status On branch master Your branch is up-to-date with 'origin/master'. Untracked files: (use "git addlab1 is untracked, but since it a binary, automatically generated file, we should not add it to out get repo. The basic rule is if a file goes away when you run make clean it should not be in the git repository...." to include in what will be committed) lab1 nothing added to commit but untracked files present (use "git add" to track)