OlveraeUNSW
Back to problems
COMP1521

COMP1521 2022T3 Q4 — Bitwise / data representation

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

Summary: Length of the longest run of consecutive 1 bits


Your task in this question is to find the length of the
longest consecutive sequence of bits set to 1 in an integer.

For example, in 305419896 == 0b0001001000110100010101100 1111 000
the longest consecutive sequence of bits set to 1 is 4 long.

You have been given 22t3final_q4.c :

// COMP1521 22T3 ... final exam, question 4

#include <stdint.h>

int _22t3final_q4 ( uint32_t x ) {
// TODO: complete this function
return 42 ;
}

Add code to the function _22t3final_q4
so that it counts and returns
the length of the longest sequence of bits set to 1 in
a 32-bit unsigned integer x .

For example:

make 22t3final_q4
305419896 == 0x12345678
./22t3final_q4 305419896
4
0 == 0x00000000
./22t3final_q4 0
0
4 == 0x00000004
./22t3final_q4 4
1
15 == 0x000000f
./22t3final_q4 15
4
4294967295 == 0xFFFFFFFF
./22t3final_q4 4294967295
32
2863311530 == 0xAAAAAAAA
./22t3final_q4 2863311530
1
268566401 == 0x1001FF81
./22t3final_q4 268566401
10

Your function should not print anything.

Your function should return an integer 0..32 inclusive.

You are not permitted to call any functions from the C standard library.

test_22t3final_q4.c includes a main function to help you test your code.

You may not change the provided test_22t3final_q4.c file.

You may not change _22t3final_q4 '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, 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
4
stdin
expected
0
stdin
expected
1
c-dcc
14