All submissions

Bit Reversal in an 8-bit Value

Code

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

uint8_t reverse_bits(uint8_t val) 
{
    int i;
    uint8_t ans=0x00;
    for(i=0;i<8;i++)
    {
       if(val&(1<<i)) //ex: i=0; 
       {
         ans=ans|(1<<(8-i-1)); //we need to set bit on pos:8-i-1.Then only we can reverse the bits.We need to set bit at that position for reversing the number.
       }
    }
    return ans;
}

int main() {
    uint8_t val;
    scanf("%hhu", &val);

    uint8_t result = reverse_bits(val);
    printf("%u", result);
    return 0;
}

Solving Approach

 

 

 

Loading...

Input

26

Expected Output

88