All submissions

Keep Only the Highest Set Bit

Code

#include <stdio.h>
#include <stdint.h>

// Function to find highest set bit
uint16_t highest_set_bit(uint16_t reg) {
    int n1 = -1; // position of highest set bit

    for (int i = 0; i < 16; i++) {
        if (reg & (1 << i)) { // check if bit i is set
            n1 = i;           // update highest bit position
        }
    }

    if (n1 == -1) return 0; // if no bits set, return 0

    return (1 << n1);        // keep only highest set bit
}

int main() {
    uint16_t reg;
    scanf("%hu", &reg);

    uint16_t result = highest_set_bit(reg);
    printf("%hu\n", result);

    return 0;
}

Solving Approach

 

 

 

Loading...

Input

44

Expected Output

32