J
Jorge
You could have used toString(radix).
var n = 101111;
n.toString(2).replace(/1/g, "ON ").
replace(/0/g,"OFF ").replace(/ $/,'');
Now that you say it... yes. I completely forgot its existence
Thanks for posting that because I have played with it a little and I
have learnt that it converts reals as well:
(3.1416).toString(2) ->
11.001001000011111111100101110010010001110100010100111
and that it will fail as miserably as mine and without warning (for
those numbers that a 'number' primitive value can't represent
accurately):
javascript:alert((9007199254740991).toString(2)) ->
11111111111111111111111111111111111111111111111111111
javascript:alert((9007199254740991.4).toString(2))->
11111111111111111111111111111111111111111111111111111
javascript:alert((9007199254740991.5).toString(2))->
100000000000000000000000000000000000000000000000000000
javascript:alert((9007199254740992).toString(2)) ->
100000000000000000000000000000000000000000000000000000
javascript:alert((9007199254740993).toString(2)) ->
100000000000000000000000000000000000000000000000000000
While writing 'my version', I was going to 'logically' test the bits
with an '&', but I have discovered that the & operator operates on
just 32 bits, not on the 53 available in the mantissa of a 'number'
primitive value:
javascript:alert(9007199254740991 & Math.pow(2,31)) ===
-10000000000000000000000000000000
javascript:alert(9007199254740991 & Math.pow(2,32)) === 0