OlveraeUNSW
Back to problems
COMP1521

COMP1521 2022T2 Q1 — Bitwise / data representation

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

Summary: Count the zero bits in a 32-bit number


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.

Samples

stdin
expected
32
stdin
expected
0
stdin
expected
31
c-dcc
14