Bit Reversal in an 8-bit Value

Code

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

uint8_t reverse_bits(uint8_t val) {
    uint8_t result = 0;
    for (int i = 0; i < 8; i++) {
        result <<= 1;          // Shift result left to make space
        result |= (val & 1);   // Copy LSB of val into result
        val >>= 1;             // Shift val right to process next bit
    }
    return result;
}

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

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

Solving Approach

// Shift result left to make space

    // Copy LSB of val into result

      // Shift val right to process next bit

 

 

Upvote
Downvote
Loading...

Input

26

Expected Output

88