COMP1521 2022T3 Q1 · 位运算
COMP1521 2022T3 Q1 · 10 marks · C
知识点: 位运算(BIT) · 次: 移位/掩码 · 难度: 基础
Topic: Bitwise / data representation · Source: https://cgi.cse.unsw.edu.au/~cs1521/26T1/resources/practice_exams.html
一句话 / Summary: 取中间8位bit12到19
You have been given 22t3final_q1.c , a stub of a C program.
Your task is to add code to this function in 22t3final_q1.c :
uint32_t _22t3final_q1 ( uint32_t x ) {
// ADD YOUR CODE HERE
return 42 ;
}
Add code to the function _22t3final_q1 so that,
given a uint32_t value,
it returns the middle 8 bits of the numbers.
In other words, if we number the bit positions as usual 0..31 your function
should return bits 12..19 as an unsigned integer between 0 and 255 (inclusive).
For example:
make 22t3final_q1
0 == 0x00000000 0x00 == 0
./22t3final_q1 0
0
make 22t3final_q1
42 == 0x0000003A 0x00 == 0
./22t3final_q1 42
0
270336 == 0x00042000 0x42 == 66
./22t3final_q1 270336
66
305419896 == 0x12345678 0x45 == 69
./22t3final_q1 305419896
69
4294967295 == 0xFFFFFFFF 0xFF == 255
./22t3final_q1 4294967295
255
Your function should not print anything.
Your function should return an integer 0..255 inclusive.
You are not permitted to call any functions from the C standard library.
test_22t3final_q1.c includes a main function to help you test your code.
You may not change the provided test_22t3final_q1.c file.
You may not change 22t3final_q1 's prototype.
ie: its return type and its argument type.
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.
Your program may #include any standard C libraries available by default on CSE Linux systems:
Including but not limited to: stdio.h , stdlib.h , string.h , math.h , assert.h .
No error checking is necessary or required.
🤖 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.
样例
0
0
66