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 result = 0;
    for (int i = 0 ; i < 8 ; i++) 
    {
        // 00011010
        // 00001101
        // 10000110
        // 01000011
        // 10100001
        // 11010000
        // 01101000
        // 00110100
        result = result <<  1;

        // Take LSB from input and add to result
        result |= (val & 1);

        // Shift input right to process next bit
        val >>= 1;
        //printf("%u\n" , res);
    }
    return result; 
    return 0;
}

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