← 返回题目列表
UNSW COMP1521 · 简单

COMP1521 2022T2 Q1 · 位运算

COMP1521 2022T2 Q1 · 10 marks · C 知识点: 位运算(BIT) · 次: popcount · 难度: 基础 Topic: Bitwise / data representation · Source: https://cgi.cse.unsw.edu.au/~cs1521/26T1/resources/practice_exams.html

一句话 / Summary: 统计32位数中0比特个数


Count zero bits is an operation on a list of bits that counts how many zero-bits are present.

For example, in the case of an 8-bit number,

count_zero_bits(0b11111111) == 0 count_zero_bits(0b111 0 1111) == 1 count_zero_bits(0b1 00 11111) == 2 count_zero_bits(0b 000 11 00 1) == 5 count_zero_bits(0b 000 1 0000 ) == 7 count_zero_bits(0b 00000000 ) == 8

You must implement this operation for 32-bit values .

You have been given 22t2final_q1.c :

// COMP1521 22T2 ... final exam, question 1

#include <stdio.h> #include <stdlib.h> #include <stdint.h>

int count_zero_bits ( uint32_t x ) { // TODO return 42 ; }

Add code to the function count_zero_bits so that it counts and returns the number of zero bits in a 32-bit unsigned integer x .

For example:

make 22t2final_q1 0 == 0x00000000 ./22t2final_q1 0 32 305419896 == 0x12345678 ./22t2final_q1 305419896 19 4294967295 == 0xFFFFFFFF ./22t2final_q1 4294967295 0


🤖 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.