algorithm for converting DEC to BIN

algorithm for converting DEC to BIN

Post by Dolly Ke » Mon, 09 Sep 2002 13:22:38



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

Post by Alf P. Steinba » Mon, 09 Sep 2002 14:38:33



>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

Post by bookstore77 » Mon, 09 Sep 2002 14:43:17


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

Post by lewi990 » Mon, 09 Sep 2002 15:43:34


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

 
 
 

1. bin to dec, hex to dec

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.

Did someone know a function about convertion with radix 2,10,16 ?

Thanks.

2. have samba working but want to setup NT Domain logins & roam profiles

3. convert from hex to dec

4. Wanted: Sun 3/50, 3/60

5. IRCAM DSP software for DEC/ALPHA and DEC/MIPS

6. Hollywood+ Drivers give BAD_POOL_CALLER on install

7. algorithm for converting Bytes to KB

8. GA source code for scheduling

9. Analysing OO-Algorithms vs. Standard algorithms.

10. Ole Convert (Convert Dialog)

11. Need help on Ole Convert (Convert Dialog)

12. convert 4 bytes in Int , convert 4 bytes in float, ....

13. selecting printer bins