Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

javascript

People also ask

Is 0b a binary?

The "0b" is a prefix to denote that the number is in binary. A similar thing is done in hexadecimal where numbers start with "0x".

Why is there a 0b in binary?

However, a number like 0100 might be interpreted as one-hundred in decimal. Thus, we sometimes prefix binary numbers with "0b" (zero b) to differentiate binary numbers from numbers in base 10 representation (so instead of using 0100, we would say that 4 in decimal is equivalent to 0b0100 in binary).

What is binary number in JavaScript?

JavaScript stores numbers as 64 bits floating point numbers, but all bitwise operations are performed on 32 bits binary numbers. Before a bitwise operation is performed, JavaScript converts numbers to 32 bits signed integers.

Which represents the number 0 in binary?

How does binary work? The 0s and 1s in binary represent OFF or ON, respectively. In a transistor, a "0" represents no flow of electricity, and "1" represents electricity being allowed to flow. In this way, numbers are represented physically inside the computing device, permitting calculations.


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

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.


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

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: MDN on Number.prototype.toString


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;

May be this will usefull:

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