0

Assume that I can only use the bitwise operators <<, >>, &

I have the following,

unsigned char c = 181;

I want to print out the bits in this char from most significant bit to least significant bit. How do I go about doing it?

Human Cyborg Relations
  • 1,202
  • 5
  • 27
  • 52

3 Answers3

3

There are as many different ways to approach this as you want to explore. Do you want to pad the output? Do you want to include separators? (e.g. 1111-0110)? Do you want to limit the output to X number of bits? And on and on.

The most basic approach is the one you are asking about, but when implementing a solution, why hard-code limitations of 8-bits when you can write a simple generic routine to handle any unsigned type (up to what you specify as the parameter). Also, when bit-twiddling, it is recommended to use exact size types (e.g. uint8_t, ... uint64_t) so there is no chance of platform differences. (exact types are defined in stdint.h) The corresponding conversion macros for the exact width types are provided in inttypes.h.

Putting the pieces together, you could write your print of the unpadded binary representation of any type value up to uint64_t something like the following:

/** unpadded binary representation of 'v'. */
void binprn (const uint64_t v)
{
    if (!v)  { putchar ('0'); return; };        /* handle v = 0 simply */

    size_t sz = sizeof v * CHAR_BIT;            /* set the number of bits */
    uint64_t rem = 0;                           /* value holding remaining bits */

    while (sz--)                                /* for each bit */
        if ((rem = v >> sz))                    /* if bits remain */
            putchar ((rem & 1) ? '1' : '0');    /* output the bit */
}

Putting it together with a short example for c = 181, you could do something like:

#include <stdio.h>
#include <limits.h>     /* for CHAR_BIT */
#include <stdint.h>     /* for exact size type ints */
#include <inttypes.h>   /* conversion macros for exact size types */

/** unpadded binary representation of 'v'. */
void binprn (const uint64_t v)
{
    if (!v)  { putchar ('0'); return; };        /* handle v = 0 simply */

    size_t sz = sizeof v * CHAR_BIT;            /* set the number of bits */
    uint64_t rem = 0;                           /* value holding remaining bits */

    while (sz--)                                /* for each bit */
        if ((rem = v >> sz))                    /* if bits remain */
            putchar ((rem & 1) ? '1' : '0');    /* output the bit */
}

int main (void) {

    uint8_t c = 181;

    printf ("The bits in unsigned char c = '%" PRIu8 "' are:\n\n  ", c);
    binprn (c);
    putchar ('\n'); /* tidy up */

    return 0;
}

Example Use/Output

$ ./bin/cbits
The bits in unsigned char c = '181' are:

  10110101

Look over all the answers, and let me know if you have any questions.

David C. Rankin
  • 81,885
  • 6
  • 58
  • 85
2

A simple solution that does not involve magic numbers like 8.

#include <stdio.h>

void print_out_the_bits(unsigned char x) {
  if (x > 1) print_out_the_bits(x >> 1);  // print most significant bits first
  putchar("01"[x & 1]);
}

int main() {
  unsigned char c = 181;
  print_out_the_bits(c);
}

Output

10110101
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
1

Apply a mask and isolate one bit at a time:

int i;
char c = 181;
for (i = 7; i >= 0; i--)
{
    putchar((c >> i) & 1 ? '1' : '0');
}
putchar('\n');
codingEnthusiast
  • 3,800
  • 2
  • 25
  • 37