Is there “0b” or something similar to represent a binary number in Javascript

后端 未结 10 1199
太阳男子
太阳男子 2020-11-29 02:32

I know that 0x is a prefix for hexadecimal numbers in Javascript. For example, 0xFF stands for the number 255.

Is there something similar f

相关标签:
10条回答
  • 2020-11-29 03:11

    Update:

    Newer versions of JavaScript -- specifically ECMAScript 6 -- have added support for binary (prefix 0b), octal (prefix 0o) and hexadecimal (prefix: 0x) numeric literals:

    var bin = 0b1111;    // bin will be set to 15
    var oct = 0o17;      // oct will be set to 15
    var oxx = 017;       // oxx will be set to 15
    var hex = 0xF;       // hex will be set to 15
    // note: bB oO xX are all valid
    

    This feature is already available in Firefox and Chrome. It's not currently supported in IE, but apparently will be when Spartan arrives.

    (Thanks to Semicolon's comment and urish's answer for pointing this out.)

    Original Answer:

    No, there isn't an equivalent for binary numbers. JavaScript only supports numeric literals in decimal (no prefix), hexadecimal (prefix 0x) and octal (prefix 0) formats.

    One possible alternative is to pass a binary string to the parseInt method along with the radix:

    var foo = parseInt('1111', 2);    // foo will be set to 15
    
    0 讨论(0)
  • 2020-11-29 03:11

    As far as I know it is not possible to use a binary denoter in Javascript. I have three solutions for you, all of which have their issues. I think alternative 3 is the most "good looking" for readability, and it is possibly much faster than the rest - except for it's initial run time cost. The problem is it only supports values up to 255.

    Alternative 1: "00001111".b()

    String.prototype.b = function() { return parseInt(this,2); }
    

    Alternative 2: b("00001111")

    function b(i) { if(typeof i=='string') return parseInt(i,2); throw "Expects string"; }
    

    Alternative 3: b00001111

    This version allows you to type either 8 digit binary b00000000, 4 digit b0000 and variable digits b0. That is b01 is illegal, you have to use b0001 or b1.

    String.prototype.lpad = function(padString, length) {
        var str = this;
        while (str.length < length)
            str = padString + str;
        return str;
    }
    for(var i = 0; i < 256; i++)
        window['b' + i.toString(2)] = window['b' + i.toString(2).lpad('0', 8)] = window['b' + i.toString(2).lpad('0', 4)] = i;
    
    0 讨论(0)
  • 2020-11-29 03:12

    I know this does not actually answer the asked Q (which was already answered several times) as is, however I suggest that you (or others interested in this subject) consider the fact that the most readable & backwards/future/cross browser-compatible way would be to just use the hex representation.

    From the phrasing of the Q it would seem that you are only talking about using binary literals in your code and not processing of binary representations of numeric values (for which parstInt is the way to go).

    I doubt that there are many programmers that need to handle binary numbers that are not familiar with the mapping of 0-F to 0000-1111. so basically make groups of four and use hex notation.

    so instead of writing 101000000010 you would use 0xA02 which has exactly the same meaning and is far more readable and less less likely to have errors.

    Just consider readability, Try comparing which of those is bigger:
    10001000000010010 or 1001000000010010

    and what if I write them like this:
    0x11012 or 0x9012

    0 讨论(0)
  • 2020-11-29 03:17

    I know that people says that extending the prototypes is not a good idea, but been your script...

    I do it this way:

    Object.defineProperty(Number.prototype, 'b', {set:function(){return false;},get:function(){return parseInt(this, 2);}});
    
    100..b       // returns 4
    11111111..b  // returns 511
    10..b+1      // returns 3
    
    // and so on
    
    0 讨论(0)
提交回复
热议问题