>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