Originally Posted by
Kazumi
I am not that expert. I do not understand your code at all.
I tried pasting but just syntax error...
Oh for crying out loud... the syntax error is from my "... get number from keyboard" line. You didn't really expect that to work did you?
I was showing you the calculation...
Here is the exact code I used to test it before posting...
Code:
#include <stdio.h>
int main (void)
{
char binary[17] = {0};
int number;
printf("Enter a number from 0 to 65535 : ");
scanf("%d", &number);
for (int x = 15; x > -1; x--)
{ if (number & 1)
binary[x] = '1';
else
binary[x] = '0';
number >>= 1; }
printf("16bit binary : %s\n\n", binary);
return 0; }
... and you couldn't figure out how to type in two simple lines that you already had in your original code???
In fact I could have simplified it even further...
Code:
for (int x = 15; x > -1; x--)
{ binary[x] = '0' + (number & 1);
number >>= 1; }
now THAT I figured you wouldn't get... hense the more obvious version.