...

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

chrisvmarle
01-11-2003, 03:14 PM
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
}

Chris

Borgtex
01-11-2003, 04:35 PM
http://javascript.internet.com/miscellaneous/binary-to-decimal.html

chrisvmarle
01-11-2003, 04:44 PM
Thanks man!!! :)

caldasgsm
01-11-2003, 07:59 PM
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....

//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

Borgtex
01-11-2003, 08:26 PM
caldasgsm: nice & compact. I like them :)

jkd
01-11-2003, 08:46 PM
I'm fond of

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

And

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

To each his own though. :D

chrisvmarle
01-11-2003, 08:55 PM
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. :D

That one's great!!!

Thanks to all of you :)

jkd
01-11-2003, 09:11 PM
Originally posted by caldasgsm
//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:

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. :)

Borgtex
01-11-2003, 10:14 PM
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. :D

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

caldasgsm
01-12-2003, 03:14 AM
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..

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...

CRASH_OVERRIDE
01-12-2003, 03:35 AM
What's the use of the ternary and shifting operators (<<,>>,>>>,<<<), etc? Ive heard of them, but never have seen them used until now.

jkd
01-12-2003, 03:37 AM
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/manuals/2000/javascript/1.5/reference/ops.html#1042512

CRASH_OVERRIDE
01-12-2003, 03:55 AM
Thanks- Ive used ternary before, just never heard the terms used. ;)