#include <stdio.h>
#include <stdint.h>
uint16_t keep_highest_bit(uint16_t reg) {
if (reg == 0) return 0; // Handle zero input
// Find the position of the highest set bit
uint16_t mask = 0x8000; // Start with MSB (bit 15)
while (mask > 0) {
if (reg & mask) {
return mask; // Found the highest set bit
}
mask >>= 1; // Check next lower bit
}
return 0; // Should not reach here for non-zero input
}
// Alternative more efficient version using bit scanning:
/*
uint16_t keep_highest_bit(uint16_t reg) {
if (reg == 0) return 0;
// Find the position of the highest set bit
uint8_t pos = 15;
while (pos > 0) {
if (reg & (1U << pos)) {
break;
}
pos--;
}
return 1U << pos;
}
*/
int main() {
uint16_t reg;
scanf("%hu", ®);
printf("%hu", keep_highest_bit(reg));
return 0;
}
Input
44
Expected Output
32