0

Possible Duplicate:
C printing bits

Lets say I have a uint16_t number something like:

uint16_t myVar = 0x3A44;

and I want to print the binary value if 0x3A44 from the myVar, how could I iteratively access and print each bit?

Treat it as a bit array?

Community
  • 1
  • 1
Snow_Mac
  • 5,727
  • 17
  • 54
  • 80

8 Answers8

6

Integers effectively are bit arrays. You can just loop through the value of the number, shifting the value to the right 16 times.

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

int main() {
    uint16_t x = 0xFF00;
    for (int i = 0; i < 16; i++) {
        printf("%d", (x & 0x8000) >> 15);
        x <<= 1;
    }
    printf("\n");
}

Results in:

1111111100000000
R.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711
Dave
  • 5,133
  • 21
  • 27
2
while (myVar != 0) {
  printf("%c", (myVar & 0x8000) == 0 ? '0' : '1');
  myVar <<= 1;
}
printf("\n");
Adam Liss
  • 47,594
  • 12
  • 108
  • 150
2

Use something like the following:

for (i = 15; i >= 0; --i) {
    printf("%d", (bool) (myVar & (1U << i)));
} 
Abel
  • 56,041
  • 24
  • 146
  • 247
ouah
  • 142,963
  • 15
  • 272
  • 331
  • 3
    +1; this is one of the cleanest and most correct answers so far. Happy to compensate for the downvote... Perhaps `!!` would be clearer (and more widely supported) than `(bool)`. – R.. GitHub STOP HELPING ICE Mar 31 '12 at 02:48
1

Well, recursion for a change! (a lazy way to print bits in order)

print(int x) {
if(!x) return;
print(x>>1);
printf("%d", x & 1);
}
P.P
  • 117,907
  • 20
  • 175
  • 238
  • If `x` is signed and negative, shifting right is undefined. Where is the sign bit, and what happens when you shift it? – Bo Persson Mar 31 '12 at 08:49
1

Simplest I can think of:

for (int i=15; i>=0; i--) putchar('0'+((x>>i)&1);
R.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711
0

To get the lowest-order bit one would write:

int is_set = (myVar >> 0) & 1;

The first part of the expression shifts the integer's bits towards the lower bits. A shift of zero does nothing, but it is easy to see how nonzero values would be used there.

The second part of the expression masks out all the bits save for the lowest order bit.

When these two are combined, you are able to detect whether or not a single bit is set or not.

fbrereto
  • 35,429
  • 19
  • 126
  • 178
0
void print_binary(uint16_t n) {
    char buffer[17];                  /* 16 bits, plus room for a \0 */
    buffer[16] = '\0';                
    for (int i = 15; i >= 0; --i) {   /* convert bits from the end */
        buffer[i] = '0' + (n & 1);    /* '0' + bit => '0' or '1' */
        n >>= 1;                      /* make the next bit the 'low bit' */
    }

    /* At this point, `buffer` is `n` turned to a string of binary digits. */

    printf("%s\n", buffer);
}
cHao
  • 84,970
  • 20
  • 145
  • 172
0
const char *nyb[5] = {
  "0000", "0001", "0010", "0011",
  "0100", "0101", "0110", "0111",
  "1000", "1001", "1010", "1011",
  "1100", "1101", "1110", "1111",
};

#define NYB(NUM, NY) (nyb[((NUM) >> (4*(NY))) & 0xf])

/* ... */

void print_u32(unsigned long u)
{
  printf("%s%s%s%s%s%s%s%s",
         NYB(u, 7), NYB(u, 6), NYB(u, 5), NYB(u, 4),
         NYB(u, 3), NYB(u, 2), NUB(u, 1), NYB(u, 0));
}

:) :) :)

Kaz
  • 55,781
  • 9
  • 100
  • 149