#include <stdio.h>
#include <stdint.h>
// Complete the function
uint16_t highest_set_bit(uint16_t reg) {
// Your logic here
if (reg == 0)
return 0;
//Spread the highest bit to the right using a loop and Shift distances: 1, 2, 4, 8 till 16 bits
for (uint8_t shift = 1; shift < 16; shift <<= 1)
{
// OR the value with itself shifted right by 'shift' bits
reg |= (reg >> shift); // propagates the highest set bit to the right
}
// isolate only the highest set bit and clear all bits except the leftmost
return reg & ~(reg >> 1);
}
int main() {
uint16_t reg;
scanf("%hu", ®);
uint16_t result = highest_set_bit(reg);
printf("%hu", result);
return 0;
}
Input
44
Expected Output
32