COMP1521 2020T3 Q2 · 9 marks · C 知识点: 位运算(BIT) · 次: LSB/MSB比较 · 难度: 基础 Topic: Bitwise / data representation · Source: https://cgi.cse.unsw.edu.au/~cs1521/26T1/resources/practice_exams.html
一句话 / Summary: 判断最低位是否等于最高位
You have been given 20t3final_q2.c , a stub of a C program.
Your task is to add code to this function in 20t3final_q2.c :
// given a uint32_t, // return 1 iff the least significant bit // is equal to the most significant bit // return 0 otherwise int final_q2 ( uint32_t value ) { ( void ) value ; // REPLACE ME WITH YOUR CODE return 42 ; // REPLACE ME WITH YOUR CODE }
Add code to the function final_q2 so that, given a uint32_t value , it returns 1 iff the least significant (bottom) bit of value is equal to the most significant (top) bit of value
final_q2 should return 0 otherwise.
For example, given the hexadecimal value 0x12345678 which is 00010010001101000101011001111000 in binary, final_q2 should return 1 , because the least significant bit is 0 and the most most significant bit is 0 .
Similarly, given the hexadecimal value 0x12345679 which is 00010010001101000101011001111001 in binary, final_q2 should return 0 , because the least significant bit is 1 and the most most significant bit is 0 .
You must only use bitwise operators to implement 20t3final_q2 .
For example:
dcc 20t3final_q2.c test_20t3final_q2.c -o 20t3final_q2 ./20t3final_q2 0x00000000 20t3final_q2(0x00000000) returned 1 ./20t3final_q2 0x00000001 20t3final_q2(0x00000001) returned 0 ./20t3final_q2 0x00000100 20t3final_q2(0x00000100) returned 1 ./20t3final_q2 0x00000101 20t3final_q2(0x00000101) returned 0 ./20t3final_q2 0x12345656 20t3final_q2(0x12345656) returned 1 ./20t3final_q2 0x12345657 20t3final_q2(0x12345657) returned 0 ./20t3final_q2 0xffffffff 20t3final_q2(0xffffffff) returned 1 ./20t3final_q2 0xfffffffe 20t3final_q2(0xfffffffe) returned 0 ./20t3final_q2 0x7fffffff 20t3final_q2(0x7fffffff) returned 0 ./20t3final_q2 0x7ffffffe 20t3final_q2(0x7ffffffe) returned 1
You can also use make(1) to build your code:
make 20t3final_q2
No error checking is necessary.
You are not permitted to call any functions from the C standard library.
You are not permitted to use: division ( / ), multiplication ( * ), or modulus ( % ).
You are not permitted to change the main function you have been given.
The main function you have been given is in test_20t3final_q2.c ,
not in 20t3final_q2.c .
You are not permitted to change 20t3final_q2 's prototype,
(its return type and argument types).
You may define and call your own functions if you wish.
🤖 Online-judge contract (adapted): write a COMPLETE C program.
argv[1]is the value (decimal, or0x-prefixed hex). Print ONLY the integer result followed by a newline. Exit 0.Examples (input → expected) are shown as sample tests below.