This displayBits function works fine in this code in displaying the bits of the input integer. I put the same thing into function reverseBits and have been trying different things to get it to reverse the order, but can't seem to get it right. I think it's a minor modification needed. Now it outputs:
255 = 00000000 00000000 00000000 11111111
but I want:
255 = 11111111 00000000 00000000 00000000

#include <stdio.h>

void displayBits(unsigned value);
void reverseBits(unsigned reverse);

int main(void)
{
  unsigned x;

  printf("Enter an Integer: ");
  scanf("%u", &x);

  displayBits(x);
  reverseBits(x);

  return 0;
}

void displayBits(unsigned value)
{
  unsigned c;
  unsigned displayMask = 1 << 31;
  
  printf("%10u = ", value);

  for(c = 1; c <= 32; c++)
  {
    putchar(value & displayMask ? '1' : '0');
    value <<= 1;
    
    if (c %8 == 0)
    {
      putchar(' ');
    }
  }
  putchar('\n');
}

void reverseBits(unsigned reverse)
{
  unsigned c;
  unsigned displayMask = 1 << 31;

  printf("%10u = ", reverse);

  for(c = 1; c <= 32; c++)
  {
    putchar(reverse & displayMask ? '1' : '0');
    reverse <<= 1 ;

    if (c %8 == 0)
    {
      putchar(' ');
    }
  }
  putchar('\n');
}

Edited 6 Years Ago by TimCereja: n/a

If you start your mask as unsigned displayMask = 1 << 31; , what is reverse after executing reverse <<= 1 ; ?

I did figure that was the problem and have set displayMask to 31 >> 1 and changed reverse <<= 1 to reverse >>= 1 . This is a step in the right direction as the output has flipped sides, but now as an example: 4 = 11100000 instead of 4 = 00100000 ...

If you output important values at key locations in your code during debugging, most questions like this could either
1) be answered without waiting 2 hours for a replay on a forum
or
2) give important information that can be posted here if the answer eludes you.

Also, why are you changing the value rather than the mask when you are checking the bits?

int main()
{
    unsigned int number = 0, i = 0;
    printf("Give the number: ");
    scanf("%u", &number);

    for(i = 0; i < sizeof(int) * 8; ++i) {
        if(0 == number % 2)
            printf("0");
        else
            printf("1") ;
        number /= 2;
   }
    return 0;
}
Comments
What's this supposed to be? Doesn't do anything like the OP wants.

all you're doing is printing the bit. you want to print the next bit, then chop it off.

  • normal order: print bit 1<<(n-1) and left shift by 1. this prints and removes the most significant bit.
  • reverse order: print bit 1 and right shift by 1. this prints and removes the least significant bit.

you don't need to get fancy, just make sure you're always working from the right side of the bit stream. :)

typedef enum
{
    MsbFirst,
    LsbFirst
} BitOrder;

void display_bits(unsigned value, BitOrder order)
{
    size_t bits = sizeof(value) * CHAR_BIT;
    size_t mask = 1;
    
    if (order == MsbFirst)
    {
        mask = 1 << (bits - 1);
    }

    while (bits-- > 0)
    {
        putchar(value & mask ? '1' : '0');

        if (bits % 8 == 0)
        {
            putchar(' ');
        }

        if (order == MsbFirst)
        {
            value <<= 1;
        }
        else
        {
            value >>= 1;
        }
    }

    putchar('\n');
}
This article has been dead for over six months. Start a new discussion instead.