All submissions

Keep Only the Highest Set Bit

Code

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

uint16_t highest_set_bit(uint16_t reg) {
    int a=0,b,c=0;
    while(reg){
      b = reg & (1<<15);     
      if(b==0){
        a++;
        reg = reg << 1;
      }
      else {
        c = (1 << (15 - a)); // ✅ corrected shift
        break;               // ✅ prevent infinite loop
      }
    }
    return c;
}


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

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

Solving Approach

 

 

 

Loading...

Input

44

Expected Output

32