2020/6/21 COMP1521 20T2 ¡ª Week 03 Weekly Test Questions
Week 03 Weekly Test Questions Test Conditions
These questions must be completed under self-administered exam-like conditions. You must time the test yourself and ensure you comply with the conditions below.
You may complete this test in CSE labs or elsewhere using your own machine.
You may complete this test at any time before Thursday 25 June 21:00.
Weekly tests are designed to act like a past paper – to give you an idea of how well you are progressing in the course, and what you need to work on. Many of the questions in weekly tests are from past final exams.
Once the first hour has finished, you must submit all questions you’ve worked on.
You should then take note of how far you got, which parts you didn’t understand.
You may choose then to keep working and submit test question anytime up to Thursday 25 June 21:00
However the maximum mark for any question you submit after the first hour will be 50% You may access this language documentation while attempting this test:
C quick reference
You may also access manual entries (the man command).
Any violation of the test conditions will results in a mark of zero for the entire weekly test component.
Set up for the test by creating a new directory called test03, changing to this directory, and fetching the provided code by running these commands:
Or, if you’re not working on CSE, you can download the provided code as a zip file or a tar file.
$ mkdir test03
$ cd test03
$ 1521 fetch test03
Test in Progress ¡ª reading time
You have just over 4 minutes left in reading time. You should not write any code.
:
Swap the bytes of a 16-bit Value
Your task is to add code to this function in short_swap.c:
Add code to the function short_swap so that, given a uint16_t value, it returns the value with its bytes swapped. You should use bitwise operators to do this.
For example:
// given uint16_t value return the value with its bytes swapped
uint16_t short_swap(uint16_t value) { // PUT YOUR CODE HERE
return 42; }
$ ./short_swap 0x1234 short_swap(0x1234) returned 0x3412 $ ./short_swap 0xfade short_swap(0xfade) returned 0xdefa $ ./short_swap 0x01080 short_swap(0x1080) returned 0x8010
Use make(1) to build your code:
https://cgi.cse.unsw.edu.au/~cs1521/20T2/test/03/questions 1/4
2020/6/21 COMP1521 20T2 ¡ª Week 03 Weekly Test Questions
$ make # or ‘make short_swap’
NOTE:
You may define and call your own functions if you wish.
You are not permitted to call any functions from the C library, or to use division (/), multiplication (*), or modulus (%).
You are not permitted to change the main function you have been given, or to change short_swap’s prototype (its return type and argument types).
When you think your program is working you can autotest to run some simple automated tests: When you are finished working on this exercise you must submit your work by running give:
Your task is to add code to this function in bit_count.c:
Add code to the function bit_count so that, given a uint64_t value, it returns how many 1 bits it contains. You should use bitwise operators to do this.
For example:
$ 1521 autotest short_swap
$ give cs1521 test03_short_swap short_swap.c
:
Count the 1 Bits of a 64-bit Value
// return how many 1 bits value contains
int bit_count(uint64_t value) { // PUT YOUR CODE HERE
return 42; }
$ ./bit_count 0x123456789abcdef0 bit_count(0x123456789abcdef0) returned 32 $ ./bit_count 0xffffffffffffffff bit_count(0xffffffffffffffff) returned 64 $ ./bit_count 0x0000000000000000 bit_count(0x0000000000000000) returned 0 $ ./bit_count 0x0000000400000000 bit_count(0x0000000400000000) returned 1 $ ./bit_count 0x8000000000000001 bit_count(0x8000000000000001) returned 2
Use make(1) to build your code:
$ make # or ‘make bit_count’
NOTE:
You may define and call your own functions if you wish.
You are not permitted to call any functions from the C library, or to use division (/), multiplication (*), or modulus (%).
You are not permitted to change the main function you have been given, or to change bit_count’s prototype (its return type and argument types).
When you think your program is working you can autotest to run some simple automated tests: When you are finished working on this exercise you must submit your work by running give:
$ 1521 autotest bit_count
$ give cs1521 test03_bit_count bit_count.c
https://cgi.cse.unsw.edu.au/~cs1521/20T2/test/03/questions
2/4
2020/6/21 COMP1521 20T2 ¡ª Week 03 Weekly Test Questions
:
Swap Pairs of Bits of a 64-bit Value
Your task is to add code to this function in bit_swap.c:
Add code to the function bit_swap so that, given a uint64_t value, it returns the same value with each pair of bits swapped: bit 0 should be swapped with bit 1, bit 2 should be swapped with bit 3, bit 4 should be swapped with bit 5, and so on. You should use bitwise operators to do this.
For example:
// return value with pairs of bits swapped
uint64_t bit_swap(uint64_t value) { // PUT YOUR CODE HERE
return 42; }
$ ./bit_swap 0x1111111111111111 bit_swap(0x1111111111111111) returned 0x2222222222222222 $ ./bit_swap 0x1111888855553333 bit_swap(0x1111888855553333) returned 0x22224444aaaa3333 $ ./bit_swap 0x0000000400000000 bit_swap(0x0000000400000000) returned 0x0000000800000000 $ ./bit_swap 0x8000000000000001 bit_swap(0x8000000000000001) returned 0x4000000000000002 $ ./bit_swap 0x123456789abcdef0 bit_swap(0x123456789abcdef0) returned 0x2138a9b4657cedf0
Use make(1) to build your code:
$ make # or ‘make bit_swap’
NOTE:
You may define and call your own functions if you wish.
You are not permitted to call any functions from the C library, or to use division (/), multiplication (*), or modulus (%).
You are not permitted to change the main function you have been given, or to change bit_swap’s prototype (its return type and argument types).
When you think your program is working you can autotest to run some simple automated tests: When you are finished working on this exercise you must submit your work by running give:
When you are finished each exercise make sure you submit your work by running give.
You can run give multiple times. Only your last submission will be marked.
Don’t submit any exercises you haven’t attempted.
If you are working at home, you may find it more convenient to upload your work via give’s web interface. Remember you have until Thursday 25 June 21:00 to complete this test.
Automarking will be run by the lecturer several days after the submission deadline for the test, using test cases that you haven’t seen: different to the test cases autotest runs for you.
(Hint: do your own testing as well as running autotest ) Test Marks
After automarking is run by the lecturer you can view it here the resulting mark will also be available via via give’s web interface or by running this command on a CSE machine:
$ 1521 autotest bit_swap
$ give cs1521 test03_bit_swap bit_swap.c
Submission
https://cgi.cse.unsw.edu.au/~cs1521/20T2/test/03/questions
3/4
2020/6/21 COMP1521 20T2 ¡ª Week 03 Weekly Test Questions
running this command on a CSE machine:
$ 1521 classrun -sturec
The test exercises for each week are worth in total 1 marks.
The best 6 of your 8 test marks for weeks 3-10 will be summed to give you a mark out of 9.
COMP1521 20T2: Computer Systems Fundamentals is brought to you by the School of Computer Science and Engineering
at the University of New South Wales, Sydney.
For all enquiries, please email the class account at cs1521@cse.unsw.edu.au CRICOS Provider 00098G
https://cgi.cse.unsw.edu.au/~cs1521/20T2/test/03/questions
4/4