Bit Reversal in an 8-bit Value

Code

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

uint8_t reverse_bits(uint8_t val) {
    // Your logic here
    uint8_t res=0;
    uint8_t n=8;
    while(n!=0){
        res<<=1;
        if(val&0x01)res|=0x01;
        else res &= ~0x01;
        val>>=1;
        n--;
    }
    return res;
}

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

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

Solving Approach

 

 

 

Upvote
Downvote
Loading...

Input

26

Expected Output

88