R
RobG
I'm working on a function to convert decimal integers to binary. The
version below is an example of my implementation of a halving
algorithm, real code works with integer strings of any length and
handles sign. I've reduced the functionality a bit so I don't have to
post the supporting functions for large ints.
This one only works within the range of values supported by the
ECMAScript implementation it's running in and with unsigned integers
(don't use numbers larger than 9 digits).
Anyhow, I remember working on something similar that used a bitwise
operator (around August 2006?) but I can't find it in the archives.
This one seems a bit long and is likely a bit slow, can anyone suggest
some optimisation tips?
// n must be an unsigned string of digits only
function toBin(n) {
var result = [],
i = 0,
d,
len;
// Trivial cases
if (n == '0' || n == '1') {
return n;
}
while (n != '1') {
len = n.length - 1;
// Get last digit
d = n.substring(len);
// If n is not even (i.e. d is odd)
if (d % 2) {
result.push('1');
// Fast subtract one from n to make it even
// d must be odd and 1 to 9 inclusive
n = n.substring(0, len) + --d;
} else {
result.push('0');
}
// Subtract half of n
n = n - (n/2 | 0) + '';
}
return '1' + result.reverse().join('');
}
Incidentally, I'm working on an unlimited precision integer library.
I've done +, -, *, pow, toBinary and a few others, once it's working
properly I'll post the interesting bits.
The pow function uses fast exponentiation by squares, which is pretty
efficient and why I want a more efficient toBin function (though the
toBin part does not use a large part of the computation time, every
little bit helps). It calculates numbers like 77616237978^123 (which
has a result of 1340 digits and is way beyond the native ECMAScript
capability) almost instantly, but I want to make it quicker. It should
be possible to combine toBin with the pow function to make it faster
again.
version below is an example of my implementation of a halving
algorithm, real code works with integer strings of any length and
handles sign. I've reduced the functionality a bit so I don't have to
post the supporting functions for large ints.
This one only works within the range of values supported by the
ECMAScript implementation it's running in and with unsigned integers
(don't use numbers larger than 9 digits).
Anyhow, I remember working on something similar that used a bitwise
operator (around August 2006?) but I can't find it in the archives.
This one seems a bit long and is likely a bit slow, can anyone suggest
some optimisation tips?
// n must be an unsigned string of digits only
function toBin(n) {
var result = [],
i = 0,
d,
len;
// Trivial cases
if (n == '0' || n == '1') {
return n;
}
while (n != '1') {
len = n.length - 1;
// Get last digit
d = n.substring(len);
// If n is not even (i.e. d is odd)
if (d % 2) {
result.push('1');
// Fast subtract one from n to make it even
// d must be odd and 1 to 9 inclusive
n = n.substring(0, len) + --d;
} else {
result.push('0');
}
// Subtract half of n
n = n - (n/2 | 0) + '';
}
return '1' + result.reverse().join('');
}
Incidentally, I'm working on an unlimited precision integer library.
I've done +, -, *, pow, toBinary and a few others, once it's working
properly I'll post the interesting bits.
The pow function uses fast exponentiation by squares, which is pretty
efficient and why I want a more efficient toBin function (though the
toBin part does not use a large part of the computation time, every
little bit helps). It calculates numbers like 77616237978^123 (which
has a result of 1340 digits and is way beyond the native ECMAScript
capability) almost instantly, but I want to make it quicker. It should
be possible to combine toBin with the pow function to make it faster
again.