# binary to decimal

• 03-12-2004
miryellis
binary to decimal
Hi there,

I am very new to c prog and most probly have thousands of questions to ask.
As for now, I have question on how do I make a conversion of Binary to Decimal. Having a standard in binary and standard out for decimal with an error msg where appropriate.

1) Do I have to define the binary string (e.g. #define BSIZE 8) if I do that, does it mean that the standard in will have to be an exact of (e.g 10010001) and I can't input it as (e.g 101)

2) How do I know when I can use a "char" or "int"? I know the function of it like char uses %d it is a single character value (e.g. ABC) and int uses %d (integer value)

3) I know how to convert from binary to decimal in mathematical terms. However, I can't seem to have the logical ability (yet) to code it into a program.
- in maths the binary will have the wt of 128, 64, 32, 16, 8, 4, 2,1
- if I've to convert a binary of 101
- it would be 101 = 4 + 1 = 5

If I have to code it will it look like (will it?)
- x=0 (binary standard in)
- a=128 (the wt of the binary)
- b=a/2 (the next wt)
- x=b

It is still a blunder to me.

Really appreciate if anyone could help to clear this mud of mine. Being a novice is never a good feeling.
:confused:
• 03-13-2004
Salem
Things like %d perform a conversion for you. So the only thing you can do for simple input is use %s

char buff[100];
scanf("%s",buff);

Now you have "101" or "11001" or "11100011001101" typed in by the user

Try converting "1234" in decimal
base = 10
result = 0
loop
- result = result * base
- result = result + numeric value of char
end loop
• 03-14-2004
manofsteel972
If you know the bitwise operators it can be very simple.
You can rotate the bits and test for 1 or 0 using a mask if you AND 1 with the rotated number it will give you the bit then use a conditional statement to print either a 0 or a 1 to the screen.

Code:

int number=3;
int number_copy=number;
_rotr(number,1);  //rotates number right _rotl() goes left

if(number & 1)  //if number & 1 is true
{
putchar('1');
}
putchar('0');

you have to work with it a bit because if you shift it the wrong direction it prints it out in reverse but you get the idea

OOPS wrong direction you wanted binary to decimal :D

You could just do the reverse. For each 1 or 0 you can use the mask to set the bits and then rotate.
• 03-14-2004
Prelude
>_rotr(number,1); //rotates number right _rotl() goes left
My compiler must be broken. I just get undefined references concerning _rotr and _rotl.
• 03-14-2004
manofsteel972
Sorry am using Visual C++ compiler
_rotl() and _rotr() are supposed to mimic the assembly language instructions for rotating bits.

I suppose you could write your own using the bitwise shift operator>> and <<. Normally when you shift the bits left or right it just discards the bit and you eventually end up with 0. If you create a mask using the number for the bit you want to use (ie to test or set the 32nd bit set the mask=2^31) then you can use the following algorithm:

for rotating right
mask_lowbit=1; //2^0 the first bit set to 1
mask_highbit=21474836848; //2^31 the 32nd bit set to 1

test lowbit for 1 or 0 by ANDing (mask_lowbit & number)
if result is 0 then simply shift number to the right
if true then shift numbers to the right and set the 32nd bit by ORing(number | mask_highestbit)

just reverse the process for shifting left
• 03-14-2004
Prelude
>_rotl() and _rotr() are supposed to mimic the assembly language instructions for rotating bits.
I'm well aware of what they're supposed to do. My point was that the two functions/macros are not standard C. Maybe I missed the OP stating a platform and compiler where your suggestion would work, but you appear to be making far too many assumptions.
• 03-14-2004
manofsteel972
Thank you for setting me straight prelude.
Your right. I just copied and paste one of my examples without thinking. I need to pay more attention to standards and compiler/op. Thank you. Anyway here is code example of home made functions.

Code:

unsigned int bit_rotate_right(unsigned int num, unsigned int numbits)
{

for(int i=0;i<numbits; i++)
{
{
num=num>>1;
}
else
{
num=num>>1;
}
}
return num;
}

unsigned int bit_rotate_left(unsigned int num, unsigned int numbits)
{

for(int i=0;i<numbits; i++)
{
{
num=num<<1;
}
else
{
num=num<<1;
}
}
return num;
}

• 03-14-2004
Dave_Sinkula
Code: