Need some JavaScript puzzles

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
 
D

Dr J R Stockton

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:

Agreed, but one can access all bits of a Double for logical operations
by using arithmetic. Working example :
<URL:http://www.merlyn.demon.co.uk/js-misc0.htm#TC>
 
J

Jorge

Agreed, but one can access all bits of a Double for logical operations
by using arithmetic.  Working example :
<URL:http://www.merlyn.demon.co.uk/js-misc0.htm#TC>

Yes, that's what I did, see:

function toBinaryString (number) {
var weight= Math.pow(2, 52), binStr= '', decimalPoint;
do {
if (number >= weight) {
number-= weight;
binStr+= '1';
} else { binStr+= binStr ? '0' : ''; }

if (((weight/= 2) < 1) && !decimalPoint) {
decimalPoint= 1;
if (number) {
binStr+= binStr ? '.' : '0.';
} else { binStr+= binStr ? '' : '0'; }
}
} while (!decimalPoint || (number && weight))
return binStr;
};
 
P

preet

try solving problems faced by people on this newsgroup, this is a real
life situation and helps a lot.

i learnt this way.

eventually someone comes up with a solution if you get stuck
 
D

David Mark

"preet" <preetkanwaljit> wrote in message



Two months later?


Figures.

Swish! Two points for "Preet", catapulting him to #8 on the November
charts. He's a Guru to watch.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,150
Latest member
MakersCBDReviews
Top