/* Include lib */
#include <stdio.h>
#include <assert.h>

/* Define */
#define GET_BIT32(num, index)   (((num) >> (index)) & 0x1)

#define REVERSE_FULL            1
#define REVERSE_PART            0

/* API get max bit count of number */
unsigned int get_max_bit_cnt(unsigned int num)
{
  int i;

  for (i = (sizeof(num) * 8 - 1); i >= 0; i--) {
    if (GET_BIT32(num, i))
      return (i + 1);
  }

  return 0;
}

/* API Reverse bit */
unsigned int reverse_bits(unsigned int num, int algo)
{
  int i, max_num, new_num = 0;

  if (algo == REVERSE_FULL)
    max_num = sizeof(num) * 8;
  else
    max_num = get_max_bit_cnt(num);
  
  for (i = 0; i < max_num; i++) {
    new_num = (new_num << 1) | GET_BIT32(num, i);
  }

  return new_num;
}

/* Main func interact with user */
int main(void)
{
  int sel;
  unsigned int value;

  while (1) {
    printf("\n\nSelection:\n");
    printf("0: Input number and get reverse in FULL 32b\n");
    printf("1: Input number and get reverse in input bit count\n");
    printf("2: Exit\n");
    printf("Select: ");
    scanf("%d", &sel);

    /* Execute */
    switch (sel) {
    case 0:
    case 1:
      printf("Input value: ");
      scanf("%x", &value);
      printf("=> 0x%x\n", reverse_bits(value, sel ? REVERSE_PART : REVERSE_FULL));
      break;
    case 2:
      return 0;
      break;
    default:
      printf("WARN: Option not available\n");
      break;
    }

  }
}

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: