If you are trying to actually convert a decimal number into it's binary equivilent:
decimal 23 -> binary 10111
There there are a couple of things which make it easier:
1) A loop
2) The Binary AND operation
3) The bitwise shift operators.
The first thing to realize is that you don't want to start with the bottom bit of the entered number - that is the least significant bit, and you want to print that on the right hand side. So, start with the left most bit. Assuming we are only dealing with 8 bit numbers (i.e. decimal 0 to 255 inclusive) you want to print the top most bit as a "1" or a "0"
This is where the Binary AND operator ('&') comes in.
if ((myNumber & 0x80) != 0)
{
printf("1");
}
else
{
printf("0");
}
The next thing you need to do is move the bit you just checked out of the way. That is where the bitwise shift operators ("<<" and ">>") come in handy.
myNumber = myNumber << 1;
Moves the whole number one place to the left - the equivalent of multiplying it by two.
Then all you need to do is set up a loop:
for (i = 0; i < 8; i++)
{
...
}
This isn't the most efficient way to do it - there are a lot of much better ones - but it does give you the basics. All you have to do is bolt the bits together, and see what happens!
"Thank you very much, I understood this one till a certain point...
What do I write in order to set the loop? I tried a couple of things and althouth I get no error the program writes "0", no matter what number I enter...
And also could you tell me what "myNumber & 0x80" means?Cause i can't understant it..! :-("
Sorry! I assumed a few things too many... :laugh:
OK: the loop:
for (i = 0; i < 8; i++)
{
...
}
This sets up a loop, and runs it. A
for
loop has three parts, separated by ';' characters:
for ( set up code ; continue while still true condition ; do every time you get to the end of the loop code )
So the code above sets an integer called
i
(which you have to declare at the top of your method) to the value zero
i = 0
And then runs around the loop adding one to
i
each time:
i++
While
i
is still less than eight
i < 8
So, it goes round the loop a total of eight times, once for each bit in the eight bit number you are working with. Try it! Paste the code into your program, and add the line
printf("%d\n", i);
in between the curly brackets.
The other bit isn't too complex either:
myNumber & 0x80
myNumber
is a variable, which holds the number you want to convert to binary. '&' is the Binary AND operator, and 0x80 is a way of writing numbers which are not in base ten: the "0x" bit tells the compiler that what follows is a number in base 16, or Hex. Why do we use hex instead of decimal or binary? Because binary gets very long, very quickly, and decimal is difficult to mentally convert to what the machine is doing. It's a lot easier to work with hex when numbers get big and you need a sense for how big exactly. Wiki should have some good stuff on number bases, but for the moment just accept that it is easier to "see" which bits are set in hex than it is in decimal. :laugh:
In this case, 0x80 is the hex equivalent of 128 in decimal, or 10000000 in binary. What it actually is in a number with just the top bit out of the eight set to one. When you use the binary AND operation, it combines the two numbers together, looking at each bit individually. If the same bit is set (or equal to 1) in both numbers, then it is set to 1 in the output. In all other cases it is set to 0.
So a binary AND of 0x80 and any number will be zero if the eighth bit in the number is zero, and non-zero if it is one.
Does that make sense, or is it a bit much to understand - it's a lot easier with pictures!
I wrote this and although the compiler says that there are no errors, as soon as I type a number it stops working...!:-(
#include
#include
int main()
{
int myNumber, i;
printf("Give number: ");
scanf("%d", myNumber);
if (myNumber & 0x80 != 0)
{
printf("1");
}
else
{
printf("0");
}
myNumber = myNumber << 1;
for (i = 0; i < 8; i++)
{
printf("%d\n", i);
}
system("pause");
}
I'm certain that I've made mistakes, but I can't find them...
Your answer was really very very helpful!!! :-)
First off - indent your code! It makes it a lot easier to see at a glance what is going on:
int main()
{
int myNumber, i;
printf("Give number: ");
scanf("%d", myNumber);
if ((myNumber & 0x80) != 0)
{
printf("1");
}
else
{
printf("0");
}
myNumber = myNumber << 1;
for (i = 0; i < 8; i++)
{
printf("%d\n", i);
}
system("pause");
}
What that will do is ask for a number, read it from the user and crash. :D
Why will it crash? Because you are trying to read the number into the wrong place. You need to give it the address of myNumber rather than the content:
scanf("%d", myNumber);
Becomes
scanf("%d", &myNumber);
(Don't worry too much about what that means - your course will explain it pretty soon)
Then, it will process the top bit of the eight, before printing out the loop count each time it goes round.
Try moving the
if-else stuff
inside the loop:
int main()
{
int myNumber = 0, i;
printf("Give number: ");
scanf("%d", &myNumber);
for (i = 0; i < 8; i++)
{
if ((myNumber & 0x80) != 0)
{
printf("1");
}
else
{
printf("0");
}
myNumber = myNumber << 1;
}
system("pause");
}
(See what I mean about indenting?)
[edit]Added brackets to if condition to handle precedence rules :o - OriginalGriff[/edit]
[edit]Changed "Right" to "Left". Bitwise shift operator << moves to the Left, not the Right :o - OriginalGriff[/edit]