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 temp=0;
uint8_t reg=0;

for(int i=7; i>=0; i--)
{
    temp = val&1;
    reg |=(temp<<i);
    val>>=1;
}

    return reg;
}

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