Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

# Thread: Need 2 functions; dec->bin & bin->dec

1. ## Need 2 functions; dec->bin & bin->dec

Hi all,

Is there someone who can help me create two functions:
1. Decimal -> Binair
2. Binair -> Decimal

I'd like them like this:
function dec2bin(in) {
// Well, function(s) here
// return Binair of in
}

function bin2dec(in) {
// Well, function(s) here
// return Decimal of in
}

Thanks in advance,
Chris

• Thanks man!!!

• ## what a hell of a coincidence...

just append that a few days a go , I decided to play a little bit with bitwise operators in jscript and decided to make
those two functions... with the best algorithm that I could...
unfortunatly I ready this post to late since you already have a reply, but still, where it is....

PHP Code:
``` //in -> int 255 //out -> string '11111111' function Dec2Bin(n){for(var s='';n!=0;(n>>>=1))s = ((n&1)?'1':'0') + s;return s;} //in -> string '100000000' //out -> int 256 function Bin2Dec(s){for(var i = 0,n = 0,l = s.length; i < l; i++)n += (s.charAt((s.length - 1) - i) == '1' ? Math.pow(2,i):0);return n;}  ```
NOTE: I messured the the performance on my machine and it was able to calcute the binary in a for(i< 4095) in 300+- milliseconds
and that back to the decimal in 800+-
I havent mesured the performance of the 'posted' function ... but
if you could try both of them in your application and thell me the performance off the functions in a real time application...I would be gratefull

• caldasgsm: nice & compact. I like them

• I'm fond of

function dec2bin(dec) {
return dec.toString(2);
}

And

function bin2dec(bin) {
return parseInt(bin, 2);
}

To each his own though.

• Originally posted by jkd
I'm fond of

function dec2bin(dec) {
return dec.toString(2);
}

And

function bin2dec(bin) {
return parseInt(bin, 2);
}

To each his own though.
That one's great!!!

Thanks to all of you

• ## Re: what a hell of a coincidence...

Originally posted by caldasgsm
PHP Code:
``` //in -> int 255 //out -> string '11111111' function Dec2Bin(n){for(var s='';n!=0;(n>>>=1))s = ((n&1)?'1':'0') + s;return s;} //in -> string '100000000' //out -> int 256 function Bin2Dec(s){for(var i = 0,n = 0,l = s.length; i < l; i++)n += (s.charAt((s.length - 1) - i) == '1' ? Math.pow(2,i):0);return n;}  ```
A syntactic improvement, as well as slightly more efficient version of your Dec2Bin:
Code:
```function Dec2Bin(n) {
for (var s=''; n!=0; (n>>>=1))
s += n&1;
return s;
}```
Your use of the ternary operator causes a typecast of (n&1) to a Boolean(), then a comparison against a literal true value. You really don't need that comparison.

• Originally posted by jkd
I'm fond of

function dec2bin(dec) {
return dec.toString(2);
}

And

function bin2dec(bin) {
return parseInt(bin, 2);
}

To each his own though.
lol
somebody has to say it: It's Elemental, Mr. Watson!. I think that I have to review some javascript functions...

• ## Corretions

thanxs jkd but you forgot that
s = (n&1) + s
or else it will invert the the bit order.
but i guess you knew that
so where is the 2 new functions with improvments..
PHP Code:
``` function Dec2Bin(n){for(var s='';n!=0;(n>>>=1))s=(n&1)+s;return s;} function Bin2Dec(s){for(var i=0,n=0,l=s.length,a=s.split('');i<l;i++)n+=(a[(l-1)-i]=='1'?Math.pow(2,i):0);return n;}  ```
and by the way, the purpose was not to 'overide' the built in functions of jscript.. but the 'challenge' of it... even a small algorithm, lauched me to a new thing that im doing now...

• What's the use of the ternary and shifting operators (<<,>>,>>>,<<<), etc? Ive heard of them, but never have seen them used until now.

• Ternary operator is just a shortcut carried over from C:

condition ? expr1 : expr2;

Is short for:

if (condition) {
expr1;
}
else {
expr2;
}

As for shifting operators, you need to understand binary...
http://devedge.netscape.com/library/...s.html#1042512

• Thanks- Ive used ternary before, just never heard the terms used.

•

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•