#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#define CHAR_BITS 8 // size of character
#define INT_BITS ( sizeof(uint32_t) * CHAR_BITS)
uint32_t ReverseTheBits(uint32_t num)
{
uint32_t count = (INT_BITS -1);
uint32_t tmp = num; // Assign num to the tmp
num >>= 1; // shift num because LSB already assigned to tmp
while(num)
{
tmp <<= 1; //shift the tmp because alread have the LSB of num
tmp |= num & 1; // putting the set bits of num
num >>= 1;
count--;
}
tmp <<= count; //when num become zero shift tmp from the remaining counts
return tmp;
}
int main()
{
/* 8-bitovy polynomial */
uint8_t test = (uint8_t)(ReverseTheBits(0x8C) >> 24);
printf("\r\n8-bit: 0x%X", test);
/* 16-bitovy polynomial */
uint16_t testS = (uint16_t)(ReverseTheBits(0x8C8C) >> 16);
printf("\r\n16-bit: 0x%X", testS);
/* 32-bitovy polynomial */
uint32_t testT = ReverseTheBits(0XEDB88320);
printf("\r\n32-bit: 0x%X", testT);
return 0;
}