Account Options

  1. Sign in
The old Google Groups will be going away soon.
Switch to the new Google Groups.
Google Groups Home
« Groups Home
Message from discussion Need some JavaScript puzzles
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Jorge  
View profile  
 More options Sep 29 2008, 4:02 pm
Newsgroups: comp.lang.javascript
From: Jorge <jo...@jorgechamorro.com>
Date: Mon, 29 Sep 2008 13:02:58 -0700 (PDT)
Local: Mon, Sep 29 2008 4:02 pm
Subject: Re: Need some JavaScript puzzles
On Sep 28, 9:04 pm, dhtml <dhtmlkitc...@gmail.com> wrote:

> Jorge wrote:
> > On Sep 24, 3:14 am, dhtml <dhtmlkitc...@gmail.com> wrote:
> >> 1. write a function return the binary representation of a given number
> >> in "ON" and "OFF".  For example, if the input is 47 (101111 in binary),
> >> it should return "ON OFF ON ON ON ON".

> >http://jorgechamorro.com/cljs/016/

> 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

--
Jorge.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.