OlveraeUNSW
Back to problems
COMP1521

COMP1521 2022T2 Q3 — Bitwise / data representation

COMP1521 2022T2 Q3 · 10 marks · C
Topic: Bitwise / data representation · Source: https://cgi.cse.unsw.edu.au/~cs1521/26T1/resources/practice_exams.html

Summary: Reverse the byte order of a uint32 (endianness)


You have been given 22t2final_q3.c ,
which contains a C function 22t2final_q3 ,
that currently takes one integer value,
and then returns the value unchanged.

Add code to the function 22t2final_q3 so that,
given a uint32_t value,
it returns the provided value but with its bytes reversed.

For example, 0x12345678 becomes 0x78563412

Note that your task is to
reverse the order of bytes ,
not to reverse the order of bits.

For example:

./22t2final_q3 0x12345678
22t2final_q3(0x12345678) returned 0x78563412
./22t2final_q3 0x43000090
22t2final_q3(0x43000090) returned 0x90000043
./22t2final_q3 0x55001248
22t2final_q3(0x55001248) returned 0x48120055
./22t2final_q3 0x55000001
22t2final_q3(0x55000001) returned 0x01000055
./22t2final_q3 0x10000080
22t2final_q3(0x10000080) returned 0x80000010
./22t2final_q3 0xFF00FF00
22t2final_q3(0xFF00FF00) returned 0x00FF00FF
./22t2final_q3 0xFF0000FF
22t2final_q3(0xFF0000FF) returned 0xFF0000FF
./22t2final_q3 0x00000001
22t2final_q3(0x00000001) returned 0x01000000
./22t2final_q3 0x10000000
22t2final_q3(0x10000000) returned 0x00000010
./22t2final_q3 0x00000000
22t2final_q3(0x00000000) returned 0x00000000
./22t2final_q3 0xFFFFFFFF
22t2final_q3(0xFFFFFFFF) returned 0xFFFFFFFF

You can use make(1) to build your code:

make 22t2final_q3

You are not permitted to call any functions from the C standard library.

22t2final_q3_main.c includes a main function to help you test your code.

You may not change the provided 22t2final_q3_main.c file.

You may not change 22t2final_q3 's prototype.

i.e. : its return type and its argument types.

You may define and call your own functions if you wish.

Your solution must be in C only.

Your solution must not be in MIPS , C++ , Python , or any other language.

Your program cannot call external programs.

Your program cannot use any of: fork() / exec() , posix_spawn() , system() , or any other similar functions.

No error checking is necessary or required.


🤖 Online-judge contract (adapted): write a COMPLETE C program. argv[1] is the value (decimal, or 0x-prefixed hex). Print ONLY the result as 0xXXXXXXXX (8 upper-case hex digits) + newline. Exit 0.

Examples (input → expected) are shown as sample tests below.

Samples

stdin
expected
0x78563412
stdin
expected
0x90000043
stdin
expected
0x48120055
c-dcc
14