All submissions

Bit Reversal in an 8-bit Value

Code

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

#define SET_BIT(var, bit)    ((var) |= (1U << (bit)))
#define RESET_BIT(var, bit)  ((var) &= ~(1U << (bit)))
#define GET_BIT(var, bit)    (((var)>>(bit))&1U)
uint8_t reverse_bits(uint8_t val) {
    // Your logic here
    val = ((val & 0xF0) >> 4) | ((val & 0x0F) << 4); // Swap nibbles
    val = ((val & 0xCC) >> 2) | ((val & 0x33) << 2); // Swap pairs
    val = ((val & 0xAA) >> 1) | ((val & 0x55) << 1); // Swap individual bits   

    return val;
}

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

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

Solving Approach

 

 

 

Loading...

Input

26

Expected Output

88