Typing decimal number and cover it binary number!

Show 80 post(s) from this thread on one page
Page 1 of 3 123 Last
• 04-16-2011
Kazumi
Typing decimal number and convert it to binary number!
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
• 04-16-2011
mike65535
1. Congrats for using CODE tags. However, the purpose of using CODE tags is to retain whitespace and the purpose of whitespace is to make the code easier to follow. Your whitespace is pretty bad - nothing lines up.

2. Some will tell you your main should be

int main (void), not
void main ()

3. You test for number == 0 twice. Why?
• 04-16-2011
Kazumi
sorry, I edited it but still doesnt work.

I am still trying it over and over again
• 04-16-2011
mike65535
Define still doesnt work
• 04-16-2011
Kazumi
I attached screenshot.
Now you can see it in color.

I would really appreciate a solution

Edit: sofar we just learned it void main ()

I dont think this is the mistake
• 04-16-2011
Subsonics
you only need one while loop. The condition should be, while there is still "mask" left. Inside the loop you test mask against your number. If true '1' else '0'. Reduce mask to test against the next digit.

Do you fully understand what the bit shift operator does?
• 04-16-2011
mike65535
Not sure I'd use the pair of while loops.

Pseudo code

Test (next) bit of number using mask
If set, print '1'
else print '0'
• 04-16-2011
Kazumi
I absolutely understand what the bit shift operator does.
short int = 16 bit.
a = 0XF = 1111;

0XF >> 1 = 0000.0000.0000.0111
0XF << 1 = 0000.0000.0001.1110

//Edit: Yea, two loops are probably wrong but now C is doing just nothing... >_<

by the way, this is 1st Semester 2nd Month task...
• 04-16-2011
Subsonics
Great, then you will be able to solve this in no time! 0x8000 is: 1000 0000 0000 0000

By shifting this in each iteration of the loop you will get: 0100 0000 0000 0000,

and so on, until eventually you are left with 0.
• 04-16-2011
Kazumi
yea, and what was my programm doing? exactly it should do this but it wasnt working thats why I came here

Now I wrote it with just one loop but it shows just nothing...

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;
number = number >> 1;
}

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

}

}

}

getchar ();
getchar ();

}

• 04-16-2011
mike65535
Quote:

sorry, I edited it but still doesnt work
That worries me...
And why the screenshot? To prove what you showed us is what you compiled? Worried again...

2. Copy and paste it into here with CODE tags so we actually get the code you compiled and ran, not something you transcribed and into which you may have introduced errors.

I still see two number == 0 tests. Get it?
• 04-16-2011
mike65535
Why the while?

Before you start, you know how many times you are going to test number - it's 16 - so use a for loop.
• 04-16-2011
Subsonics
Ok, here's some pseudo code of a simple approach.

Code:

print 1
else
print 0

• 04-16-2011
Kazumi
can anyone tell me what is exactly wrong with the upper 2nd code?

I changed the double ==
it's doing something but not what I want.

I mostly doubt if the brackets syntax is correct
• 04-16-2011
whiteflags
Code:

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

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

}

Well, that defeats the purpose of a mask. AND is going to return the information you need to decide what to print. You shift the mask to move the algorithm forward. The use of the mask will let you keep the number constant.

I think the only problem then is that you change number unnecessarily in this attempt.
Show 80 post(s) from this thread on one page
Page 1 of 3 123 Last