Keep Only the Highest Set Bit

Code

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

uint16_t pos[] = {
    0x8000, 0x4000, 0x2000, 0x1000,
    0x800,  0x400, 0x200,   0x100,
    0x80,   0x40,   0x20,   0x10,
    0x8,    0x4,    0x2,    0x1
};


// Complete the function
uint16_t highest_set_bit(uint16_t reg) {
    // Your logic here
    for(size_t i = 0; i < 16; ++i)
    {
        if(reg & pos[i]) return pos[i];
    }
    return 0;
}

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

    uint16_t result = highest_set_bit(reg);
    printf("%hu", result);
    return 0;
}

Solving Approach

 

 

 

Upvote
Downvote
Loading...

Input

44

Expected Output

32