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 access this language documentation while attempting this test:

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:

mkdir test03
cd test03
1521 fetch test03

Or, if you're not working on CSE, you can download the provided code as a zip file or a tar file.

weekly test question:
Swap the bytes of a 16-bit Value

Your task is to add code to this function in short_swap.c:

// given uint16_t value return the value with its bytes swapped
uint16_t short_swap(uint16_t value) {
    // PUT YOUR CODE HERE

    return 42;
}

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:

./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:

make    # or 'make short_swap'

When you think your program is working you can autotest to run some simple automated tests:

1521 autotest short_swap
When you are finished working on this exercise you must submit your work by running give:
give cs1521 test03_short_swap short_swap.c

weekly test question:
Count the 1 Bits of a 64-bit Value

Your task is to add code to this function in bit_count.c:

// return how many 1 bits value contains
int bit_count(uint64_t value) {
    // PUT YOUR CODE HERE

    return 42;
}

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:

./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'

When you think your program is working you can autotest to run some simple automated tests:

1521 autotest bit_count
When you are finished working on this exercise you must submit your work by running give:
give cs1521 test03_bit_count bit_count.c

weekly test question:
Swap Pairs of Bits of a 64-bit Value

Your task is to add code to this function in bit_swap.c:

// return value with pairs of bits swapped
uint64_t bit_swap(uint64_t value) {
    // PUT YOUR CODE HERE

    return 42;
}

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:

./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'

When you think your program is working you can autotest to run some simple automated tests:

1521 autotest bit_swap
When you are finished working on this exercise you must submit your work by running give:
give cs1521 test03_bit_swap bit_swap.c

Submission

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 Wednesday 01 January 00: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 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.