1
console.log("0b111" ^ "0b001"); // 6
console.log(0b111 ^ 0b001); // 6

Though I can do this, is there any way to convert "0b111" to numeric literals 0b111 ? thanks.

Ari
  • 45
  • 4
justin87qq
  • 101
  • 9
  • 1
    Remove the quotes like you've done or use `parseInt("0b111".slice(2), 2)` if you need to do it programmatically. – ggorlen May 28 '20 at 18:29
  • Does this answer your question? [How to convert binary representation of number from string to integer number in JavaScript?](https://stackoverflow.com/questions/11103487/how-to-convert-binary-representation-of-number-from-string-to-integer-number-in) – ggorlen May 28 '20 at 18:32
  • ```const toBinaryFormat = (n) => `0b${n.toString(2)}` ```, and then `toBinaryFormat(6) ==> "0b110"`. – Scott Sauyet May 28 '20 at 18:33
  • thanks, but parseInt("0b111".slice(2), 2) will return 7 which is decimal, not a binary literal – justin87qq May 28 '20 at 18:40
  • @justin87qq if you want it to "appear" as a "binary literal" in the console like a RegEx literal does, that is not how numbers work and it is not possible – user120242 May 28 '20 at 18:45
  • Scott's function would return string which is not a numeric literal – justin87qq May 28 '20 at 18:46
  • console.log(typeof toBinaryFormat(6)); // string – justin87qq May 28 '20 at 18:46
  • console.log(typeof 0b110); // number – justin87qq May 28 '20 at 18:46
  • 2
    "`numeric` literal" is "`6`". that is how console displays `number`s. \x0001 and 0b111 are syntax representations of a `number`. what you are trying to ask for isn't possible and doesn't really make sense – user120242 May 28 '20 at 18:47
  • thank you, I got it :D – justin87qq May 28 '20 at 18:48
  • As far as the code is concerned, all numeric values are ultimately binary. How the console displays it is irrelevant for calculation purposes, and if you want to display it a certain way on the screen you'll have to use a string for that. – John Montgomery May 28 '20 at 22:22

0 Answers0