# Thread: Typing decimal number and cover it binary number!

1. Yeah, either shift the number OR shift the mask. Not both.

2. I know NOTHING of masks, however if that's not an exact part of your assignment, this can easily be done with nothing but a printf statement, a for loop and a couple lines for the math.

3. ok whiteflags you are right.
shifting both makes nonsene.

But even if i remove that line number = number >> 1;
its still not working.

meanwhile I tried to do what Subsonics said:

the beginning is same:

number = the positive number you type in

Code:
```else if ( number != 0 )

while ( b != 0 )
{

if ( b & number == 0)
{
printf ("0");
b = b >> 1;
}

else ( b & number != 0 );
printf ("1");
b = b >> 1;

}

getchar ();
getchar ();

}```

4. Hmmm... everyone's off in the wrong direction here...

Code:
```char binary[17] = {0};

... get number from 0 to 65535 from keyboard.

for (int x = 15; x > -1 ; x--)
{ if (number & 1)
binary[x] = '1';
else
binary[x] = '0';
number >>= 1; }

printf("%s\n", binary);```

5. You are missing some braces and you should not use a semicolon after the else case. If you want to keep the condition then it should be "else if", but it's not really necessary after the first test.

Code:
```while ( b != 0 )
{
if ( b & number == 0)
{
printf ("0");
b = b >> 1;
}
else if ( b & number != 0 )
{
printf ("1");
b = b >> 1;
}
}```
You can also move the 'b = b >> 1' part to the end of the loop, functionally it's the same however.

Edit:

So this, is functionally the same.

Code:
```while ( b != 0 )
{
if ( b & number == 0)
{
printf ("0");
}
else
{
printf ("1");
}
b = b >> 1;
}```

6. yea, subsonics but its still not working....

@CommonTater I dont know what you mean....

almost 20 responds but problem still have not been solved.... >_<

Can anyone just code it and then copy it here?

7. Originally Posted by Kazumi
@CommonTater I dont know what you mean....
Did you even try it?

8. I am not that expert. I do not understand your code at all.

I tried pasting but just syntax error...

9. Originally Posted by Kazumi
yea, subsonics but its still not working....
You actually need to wrap up your condition like this:

if( (b & number) == 0 )

if you are going to test it against 0, but post the code.

10. Kazumi, you might want to read this about the most significant bit. I'm sorry it's quite technical, but the point is you can't just assume 0x8000 is going to be the most significant anything. I mean, what you're saying is true, concerning the most significant bit and how it relates to the first byte (0x8000) but it's going to be wrong for any piece of information longer than that byte.

Commontater showed you another way to do it, but you're pretty close using a mask to do this anyway, so it's up to you what to do. We're not "off in the wrong direction" as he puts it.

11. @whiteflags

I dont have to read it.
Thats why I use "unsigned"
if I didnt use unsigned it depends on the compiler.

you should rather read it =)

12. MUAHAHAHAHHA DONE

thanks special to Subsonics

you have to use brackets for & TTTTTTTTTTTT
if (( number & b ) != 0 )

So all others completely failed =)
same beginners as me though 4k posts^^

here the working code for future noobs

Working CODE 1
Code:
```#include <stdio.h>

void main ()

{
unsigned short int number; //Typed Number
unsigned short int b = 0X8000; // Mask
int i = 0;

printf ("Pleae type a postive number: ");
scanf ("%d", &number);

// Number = 0 = 0000.0000.0000.0000
if ( number == 0 )
{ 	printf ("\nResult: 0000.0000.0000.0000");
}

else if ( number != 0 )
{

while ( i < 16 )

{
i = i + 1;

if ( ( number & b ) != 0)
{   printf ("1");
b = ( b >> 1 );
}

else if ( ( number & b ) == 0)
{
printf ("0");
b = ( b >> 1 );

}

}

}

printf ("\n\nDone by Kazumi =)");

getchar ();
getchar ();

}```

13. Well, you're not really helping by just saying it doesn't work all the time. I mean, if you could show us what you get for output, or ...anything it would help my detective work.

Assuming you're really working with short integers, and I can trust that this is still your code:
Code:
``` while ( b != 0 )
{

if ( b & number == 0)
{
printf ("0");
b = b >> 1;
}

else ( b & number != 0 );
printf ("1");
b = b >> 1;

}```
The only other thing I think that might be wrong is the scanf call.

Is it still

Code:
`scanf("%d", &number);`
and is number still a short? If number is a short, then that call is wrong. Rather it should be

Code:
`scanf("%hu", &number);`
I can only speculate what happens when the format string doesn't match the type of the argument, but I know that it's undefined behavior.

So if you actually enter 15 and 00001111 doesn't come out, that could be a reason.

14. It's working now. didnt read my previous code?
and I also make the 2nd way working.

No Problem if you know the mistake.

And how was I supposed to tell a mistake if there wasnt a syntax error?

Anyway...

All done yet =)

*Happy Day*

WORKING CODE 2
Code:
```#include <stdio.h>

void main ()

{
unsigned short int number; //Typed Number
unsigned short int b = 0X8000; // Mask
int i = 0;

printf ("Pleae type a postive number: ");
scanf ("%d", &number);

// Number = 0 = 0000.0000.0000.0000
if ( number == 0 )
{ 	printf ("\nResult: 0000.0000.0000.0000");
}

else if ( number != 0 )

{
while  ( b != 0 )
{

if ( ( b & number ) == 0 )
{

printf ("0");
b = ( b >> 1 );

}

else if ( ( b & number ) != 0 )
{

printf ("1");
b = ( b >> 1 );

}

}

}

getchar ();
getchar ();

}```
that was Subsonics mention before. =)

15. Originally Posted by Kazumi
It's working now. didnt read my previous code?
and I also make the 2nd way working.

No Problem if you know the mistake.

And how was I supposed to tell a mistake if there wasnt a syntax error?
I'm just saying you weren't telling us everything we could know.

Compiling: sandbox.c
C:\Documents and Settings\Owner\My Documents\sandbox\sandbox.c:3:6: warning: return type of 'main' is not 'int'
C:\Documents and Settings\Owner\My Documents\sandbox\sandbox.c: In function 'main':
C:\Documents and Settings\Owner\My Documents\sandbox\sandbox.c:12:3: warning: format '%d' expects type 'int *', but argument 2 has type 'short unsigned int *'
C:\Documents and Settings\Owner\My Documents\sandbox\sandbox.c:27:4: warning: suggest parentheses around comparison in operand of '&'
C:\Documents and Settings\Owner\My Documents\sandbox\sandbox.c:35:4: warning: suggest parentheses around comparison in operand of '&'
Process terminated with status 0 (0 minutes, 0 seconds)
0 errors, 4 warnings
That was the digest produced by my compiler from your first post, which I did just now. The compiler lays out in black and white, all of your problems. But you never posted such information. So who's responsibility is it to compile what you write? If you didn't get anything like this (although I find that weird, because I don't need warnings on to get this), I suspect you could at least show us what does happen when you execute the program.

In other words, you could show us WHY it didn't work. So you know, the whole 20 post thread, and the getting privilege of calling me an idiot is rather your own fault.