tags:

views:

76

answers:

2

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.

+7  A: 

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
LukeH
Pretty disappointing...
Misha Moroshko
A: 

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

Lord Torgamus