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.
asked May 10, 2010 at 13:59
Misha MoroshkoMisha Moroshko
160k220 gold badges492 silver badges727 bronze badges
6
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
Qwerty
25.8k21 gold badges101 silver badges124 bronze badges
answered May 10, 2010 at 14:04
14
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.
Olga
1,6181 gold badge22 silver badges31 bronze badges
answered Sep 1, 2014 at 22:20
urishurish
8,8108 gold badges52 silver badges74 bronze badges
0
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
ruffin
15.4k8 gold badges82 silver badges129 bronze badges
answered Sep 26, 2012 at 7:27
Juan GarciaJuan Garcia
2593 silver badges3 bronze badges
1
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
ruffin
15.4k8 gold badges82 silver badges129 bronze badges
answered May 10, 2010 at 14:18
PopsPops
29.5k36 gold badges133 silver badges151 bronze badges
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;
answered Sep 12, 2012 at 21:52
frodeborlifrodeborli
1,4611 gold badge20 silver badges30 bronze badges
0
May be this will usefull:
var bin = 1111;
var dec = parseInt[bin, 2];
// 15
answered Dec 9, 2011 at 12:25
Sergey MetlovSergey Metlov
25k27 gold badges91 silver badges150 bronze badges
1
No, but you can use parseInt and optionally omit the quotes.
parseInt[110, 2]; // this is 6
parseInt["110", 2]; // this is also 6
The only disadvantage of omitting the quotes is that, for very large numbers, you will overflow faster:
parseInt[10000000000000000000000, 2]; // this gives 1
parseInt["10000000000000000000000", 2]; // this gives 4194304
answered Oct 14, 2014 at 13:11
0
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
answered May 26, 2015 at 12:15
epelegepeleg
10k17 gold badges100 silver badges150 bronze badges
Convert binary strings to numbers and visa-versa.
var b = function[n] {
if[typeof n === 'string']
return parseInt[n, 2];
else if [typeof n === 'number']
return n.toString[2];
throw "unknown input";
};
answered Jan 4, 2013 at 11:04
jpillorajpillora
5,1042 gold badges42 silver badges55 bronze badges
Using Number[] function works...
// using Number[]
var bin = Number['0b1111']; // bin will be set to 15
var oct = Number['0o17']; // oct will be set to 15
var oxx = Number['0xF']; // hex will be set to 15
// making function convTo
const convTo = [prefix,n] => {
return Number[`${prefix}${n}`] //Here put prefix 0b, 0x and num
}
console.log[bin]
console.log[oct]
console.log[oxx]
// Using convTo function
console.log[convTo['0b',1111]]
answered Nov 22, 2021 at 16:55
RazerJsRazerJs
1641 silver badge11 bronze badges