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

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

I know that 0x is a prefix for hexadecimal numbers in Javascript. For example, 0xFF stands for the number 255.
Is there something similar for binary numbers ? I would expect 0b1111 to represent the number 15, but this doesn’t work for me.

Solutions/Answers:

Solution 1:

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

Solution 2:

In ECMASCript 6 this will be supported as a part of the language, i.e. 0b1111 === 15 is true. You can also use an uppercase B (e.g. 0B1111).

Look for NumericLiterals in the ES6 Spec.

Solution 3:

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

Solution 4:

If your primary concern is display rather than coding, there’s a built-in conversion system you can use:

var num = 255;
document.writeln(num.toString(16)); // Outputs: "ff"
document.writeln(num.toString(8)); // Outputs: "377"
document.writeln(num.toString(2)); // Outputs: "11111111"

Ref: JavaScript Number Reference

Solution 5:

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;

Solution 6:

May be this will usefull:

var bin = 1111;
var dec = parseInt(bin, 2);
// 15