Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 13 of 13
  1. #1
    Regular Coder
    Join Date
    Jun 2002
    Location
    the Netherlands
    Posts
    215
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

    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
    }

    Thanks in advance,
    Chris
    My Website
    010100010011110101110100011011110111000001101000

  • #2
    Regular Coder
    Join Date
    Aug 2002
    Location
    Spain
    Posts
    420
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Don't resist to assimilation. Billions of Borgs can't be wrong!

  • #3
    Regular Coder
    Join Date
    Jun 2002
    Location
    the Netherlands
    Posts
    215
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks man!!!
    My Website
    010100010011110101110100011011110111000001101000

  • #4
    New Coder
    Join Date
    Jul 2002
    Location
    Portugal
    Posts
    44
    Thanks
    0
    Thanked 0 Times in 0 Posts

    what a hell of a coincidence...

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

    PHP Code:
    //in -> int 255 //out -> string '11111111'
    function Dec2Bin(n){for(var s='';n!=0;(n>>>=1))= ((n&1)?'1':'0') + s;return s;}
    //in -> string '100000000' //out -> int 256
    function Bin2Dec(s){for(var 0,0,s.lengthli++)+= (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
    You don't want to know...

  • #5
    Regular Coder
    Join Date
    Aug 2002
    Location
    Spain
    Posts
    420
    Thanks
    0
    Thanked 0 Times in 0 Posts
    caldasgsm: nice & compact. I like them
    Don't resist to assimilation. Billions of Borgs can't be wrong!

  • #6
    jkd
    jkd is offline
    Senior Coder jkd's Avatar
    Join Date
    May 2002
    Location
    metro DC
    Posts
    3,163
    Thanks
    1
    Thanked 18 Times in 18 Posts
    I'm fond of

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

    And

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

    To each his own though.

  • #7
    Regular Coder
    Join Date
    Jun 2002
    Location
    the Netherlands
    Posts
    215
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.
    That one's great!!!

    Thanks to all of you
    My Website
    010100010011110101110100011011110111000001101000

  • #8
    jkd
    jkd is offline
    Senior Coder jkd's Avatar
    Join Date
    May 2002
    Location
    metro DC
    Posts
    3,163
    Thanks
    1
    Thanked 18 Times in 18 Posts

    Re: what a hell of a coincidence...

    Originally posted by caldasgsm
    PHP Code:
    //in -> int 255 //out -> string '11111111'
    function Dec2Bin(n){for(var s='';n!=0;(n>>>=1))= ((n&1)?'1':'0') + s;return s;}
    //in -> string '100000000' //out -> int 256
    function Bin2Dec(s){for(var 0,0,s.lengthli++)+= (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:
    Code:
    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.

  • #9
    Regular Coder
    Join Date
    Aug 2002
    Location
    Spain
    Posts
    420
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.
    lol
    somebody has to say it: It's Elemental, Mr. Watson!. I think that I have to review some javascript functions...
    Last edited by Borgtex; 01-11-2003 at 09:16 PM.
    Don't resist to assimilation. Billions of Borgs can't be wrong!

  • #10
    New Coder
    Join Date
    Jul 2002
    Location
    Portugal
    Posts
    44
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Corretions

    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..
    PHP Code:
    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...
    You don't want to know...

  • #11
    Regular Coder
    Join Date
    Aug 2002
    Location
    IN, USA
    Posts
    121
    Thanks
    0
    Thanked 0 Times in 0 Posts
    What's the use of the ternary and shifting operators (<<,>>,>>>,<<<), etc? Ive heard of them, but never have seen them used until now.
    Just call me Crash...
    "Crash- and BURN!"
    -Cereal Killer

  • #12
    jkd
    jkd is offline
    Senior Coder jkd's Avatar
    Join Date
    May 2002
    Location
    metro DC
    Posts
    3,163
    Thanks
    1
    Thanked 18 Times in 18 Posts
    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/...s.html#1042512

  • #13
    Regular Coder
    Join Date
    Aug 2002
    Location
    IN, USA
    Posts
    121
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks- Ive used ternary before, just never heard the terms used.
    Just call me Crash...
    "Crash- and BURN!"
    -Cereal Killer


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •