#include <stdio.h>
#include <stdint.h>
// Complete the function
uint16_t highest_set_bit(uint16_t reg) {
// Your logic here
int high =0, cur = 0,count=16;
if (reg>0){
while(count!=0){
if(reg & (1<<cur))
high = cur;
cur++;
count--;
}
reg &= 0;
reg |= (1<<high);
}
return reg;
}
int main() {
uint16_t reg;
scanf("%hu", ®);
uint16_t result = highest_set_bit(reg);
printf("%hu", result);
return 0;
}
The goal of the function highest_set_bit is to find the most significant bit (MSB) that is set to 1 in the given 16-bit number and return a number with only that bit set.
Initialization:
Bit Scanning:
Construct Result:
Return: The function returns the modified reg with only the highest set bit preserved.
Example:
If reg = 42 (101010 in binary), the highest set bit is at position 5 (zero-based). The result will be 32 (100000 in binary).,
Input
44
Expected Output
32