Platinum Partner
c

Print A Binary Number In C

These are two functions that print the binary representation of an integer. The first simply prints it out, while the second only prints out the relevant digits (i.e. cuts the first x 0 digits) in groups of four.

Explanation here.


#include 

/* Print n as a binary number */
void printbitssimple(int n) {
unsigned int i;
i = 1 0) {
if (n & i)
printf("1");
else
printf("0");
i >>= 1;
}
}

/* Print n as a binary number */
void printbits(int n) {
unsigned int i, step;

if (0 == n) { /* For simplicity's sake, I treat 0 as a special case*/
printf("0000");
return;
}

i = 1>= 4; /* In groups of 4 */
while (step >= n) {
i >>= 4;
step >>= 4;
}

/* At this point, i is the smallest power of two larger or equal to n */
while (i > 0) {
if (n & i)
printf("1");
else
printf("0");
i >>= 1;
}
}

int main(int argc, char *argv[]) {
int i;
for (i = 0; i 
{{ tag }}, {{tag}},

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}
{{ parent.authors[0].realName || parent.author}}

{{ parent.authors[0].tagline || parent.tagline }}

{{ parent.views }} ViewsClicks
Tweet

{{parent.nComments}}