#include <stdio.h>
#include <stdint.h>
// Complete the function
uint16_t highest_set_bit(uint16_t reg) {
if (reg == 0) return 0;
int watermark = 0;
for (int i = 0; i < 16; i++) {
if (reg & (1 << i)) {
watermark = i;
}
}
return (1 << watermark);
}
int main() {
uint16_t reg;
scanf("%hu", ®);
uint16_t result = highest_set_bit(reg);
printf("%hu", result);
return 0;
}Lets get the 0 case out of the way first, if the register is 0, then there will be no highest set bit, return 0.
To check if a value is set we AND with 1, In a loop of 16 to check each bit, store the index of the highest set bit and return 1 left shifted to that index.
Input
44
Expected Output
32