Hey everyone!
I am looking for a genius who can help to solve my very easy problem!)
My kinda homework is: Type a decimal number and the C should spit it out as binary.
e.g. 15 (decimal)-> 1111 (binary)
1. I'd create a mask c = 0X800
2. I would use the operator & to compare the typed number and the mask.
3. if the value = 0, print 0 and Shift mask 1 to right
4. Otherwise print 1 Shift mask and number 1 to right
I am playing with this already for hours.... just no positive result.
thanks for help and tips.
Code:
#include <stdio.h>
void main ()
{
unsigned short int number; //Typed Number
unsigned short int b = 0x8000; // Mask
printf ("Pleae type a postive number: ");
scanf ("%d", &number);
if ( number == 0 )
{ printf ("\nResult: 0000.0000.0000.0000");
}
else if ( number != 0 )
{
while ( number & b != 0 );
{
printf ("0");
b = b >> 1;
}
while ( number & b != 0);
{
printf ("1");
b = b >> 1;
number = number >> 2;
}
}
getchar ();
getchar ();
}
http://img845.imageshack.us/img845/4589/unledif.jpg