OlveraeUNSW
返回题目列表
COMP1521

COMP1521 2020T2 Q2 · 位运算

COMP1521 2020T2 Q2 · 10 marks · C
知识点: 位运算(BIT) · 次: 字节提取/掩码 · 难度: 基础
Topic: Bitwise / data representation · Source: https://cgi.cse.unsw.edu.au/~cs1521/26T1/resources/practice_exams.html

一句话 / Summary: 判断最低字节是否等于次低字节


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

// given a uint32_t value,
// return 1 iff the least significant (bottom) byte
// is equal to the 2nd least significant byte; and
// return 0 otherwise
int practice_q2 ( uint32_t value ) {
// PUT YOUR CODE HERE

return 42 ;
}

Add code to the function practice_q2 so that,
given a uint32_t value,
it returns 1
iff the least significant (bottom) byte of value
is equal to the second least significant byte.
practice_q2 should return 0 otherwise.

For example, given the hexadecimal value 0x12345678 ,
practice_q2 should return 0 ,
because the least significant byte, 0x78 ,
is not equal to the second least significant byte, 0x56 .

Similarly, given the hexadecimal value 0x12345656 ,
practice_q2 should return 1 ,
because the least significant byte, 0x56 ,
is equal to the second least significant byte, 0x56 .

You must use bitwise operators to implement practice_q2 .

For example:

dcc practice_q2.c test_practice_q2.c -o practice_q2
./practice_q2 0x00000000
practice_q2(0x00000000) returned 1
./practice_q2 0x00000001
practice_q2(0x00000001) returned 0
./practice_q2 0x00000100
practice_q2(0x00000100) returned 0
./practice_q2 0x00000101
practice_q2(0x00000101) returned 1
./practice_q2 0x00001212
practice_q2(0x00001212) returned 1
./practice_q2 0x00001213
practice_q2(0x00001213) returned 0
./practice_q2 0x12345678
practice_q2(0x12345678) returned 0
./practice_q2 0x12345656
practice_q2(0x12345656) returned 1
./practice_q2 0x12345634
practice_q2(0x12345634) returned 0
./practice_q2 0x12345666
practice_q2(0x12345666) returned 0
./practice_q2 0x12121212
practice_q2(0x12121212) returned 1
./practice_q2 0x37861212
practice_q2(0x37861212) returned 1
./practice_q2 0x12121221
practice_q2(0x12121221) returned 0

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

make practice_q2

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.

You are not permitted to change
practice_q2 's prototype
(its return type and argument types).

No error checking is necessary.

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, or 0x-prefixed hex). Print ONLY the integer result followed by a newline. Exit 0.

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

样例

stdin
期望输出
1
stdin
期望输出
0
stdin
期望输出
0
c-dcc
14