## algorithm for converting DEC to BIN

### algorithm for converting DEC to BIN

Hi all, I am wondering if there is an algorithm for converting deciaml
number to binary. I am not asking for programming code, which I will
write myself, but just the algorithm. Thanks a lot. Would it be
possible to write it without using STD library... just loops

wc

### algorithm for converting DEC to BIN

>Hi all, I am wondering if there is an algorithm for converting deciaml
>number to binary. I am not asking for programming code, which I will
>write myself, but just the algorithm. Thanks a lot. Would it be
>possible to write it without using STD library... just loops

I guess you're not after how you can do it easily using the standard
library, so I'll just skip that (otherwise I'd had to look it up, I
think there's a direct conversion somewhere, possibly std::bitset).

I'll also make the simplifying assumption that you're only considering
integer values.

Then, if we number the digits from 0 for the least significant digit
to n-1 for the most significant, digit number k in radix r (which
in your case is r=2), for a positive value v, is simply

v/r^k % r

where / denotes integer division, and % denotes the modulus operation
(remainder of division).  In C and C++ the division by a power of r
can be done very efficiently when r=2, by using the shift operators
<< and >>.  Note that their usage for iostreams is a recent one, which
is in addition to their original C meaning of bitshifting.

From that you should be able to turn it into a loop; I'll refrain
from spoiling the fun.

Tip: when you have a function that works for strictly positive values,
use that from another function that adds support for 0, and then
use that from one that also considers negative values.

Hth.,

- Alf

### algorithm for converting DEC to BIN

can u explain your question in detail??
int  num=15;
num is both decimal number and binary number.

Quote:> Hi all, I am wondering if there is an algorithm for converting deciaml
> number to binary. I am not asking for programming code, which I will
> write myself, but just the algorithm. Thanks a lot. Would it be
> possible to write it without using STD library... just loops

> wc

### algorithm for converting DEC to BIN

I belive he means turn

int dec = 191;
to
char bin[] = "10111111";

///Here how i'd do it...
int dec =191; //change to you val...
int binbitvalue = 128;

//You need a str here...I am forget all the str set stuff so I leave that to
you but declare it here...

for(int i = 7; i >=0; i--)
{
if(dec - binbitvalue >=0)
{
//add "1" the end of of binary str
dec = dec - binbitvalue;
}
else
{
//add "1" the end of of binary str
}
binbitvalue >> 1;

Quote:}

adding str code a running this you get...

191 -> code -> "10111111"
189 -> code -> "10111101"
173 -> code -> "10101101"
255 -> code -> "11111111"
000 -> code -> (And Yes here it is the big nothing) "00000000"

> can u explain your question in detail??
> int  num=15;
> num is both decimal number and binary number.

> > Hi all, I am wondering if there is an algorithm for converting deciaml
> > number to binary. I am not asking for programming code, which I will
> > write myself, but just the algorithm. Thanks a lot. Would it be
> > possible to write it without using STD library... just loops

> > wc

I can get a decimal number and write ( using cout<< ) the binary and hexa
form with atoi and itoa function ( with char and string ).
A function we enter a binary ( string or char [] ) and the return value is
the same type ( string or char [] ) but in other format ( hexa, decimal )
did exist ?
I can't find it and I don't want to make that kind of function for nothing
and seing a function that do it after my function complete.