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 bit, newval = 0;

    for(uint8_t i = 0; i < 8; i++){
        
        // Extract each bit of val:
        bit = (val >> i) & 1u;
        // Input it into the correct index in newval:
        newval |= (bit << (7-i));
    }
    return newval;
}

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