Keep Only the Highest Set Bit

Code

#include <stdio.h>
#include <stdint.h>
uint16_t keepHighestBit(uint16_t n){
    if(n == 0) return 0;
    int pos = 0;
    while(n >> (pos + 1)){
        pos++;
    } 
    return(1U << pos);
}
int main(){
    uint16_t n;
    scanf("%hu", &n);
    printf("%hu\n", keepHighestBit(n));
    return 0;
}

Solving Approach

 

 

 

Upvote
Downvote
Loading...

Input

44

Expected Output

32