529

I’d like to see integers, positive or negative, in binary.

Rather like this question, but for JavaScript.

11
  • 5
    the a.toString(2) examples don't seem to work for -1
    – barlop
    Commented Mar 30, 2012 at 9:01
  • 2
    It's also possible to convert from binary to decimal: stackoverflow.com/questions/11103487/… Commented Jan 21, 2013 at 21:52
  • And when I said "in binary", that may be a bit ambiguous. I mean the internal bit string representation, which is 2s complement, so positive numbers would be in base 2, and with a leading 0, (and negative numbers wouldn't be written with a minus symbol or with sign magnitude representation, but as a function of their positive eqivalent)
    – barlop
    Commented Jun 5, 2019 at 9:37
  • This question is incredibly misleading. "Maybe a bit ambiguous" is put too mildly. Why is 2s complement not appearing in the title nor in the body of the question, if that is the thing you actually want? Neither is a more or less equivalent expression like 'internal bit-string-representation'. From your answer and your comments on other answers I conclude you don't care about binary but rather about 2s complement. The questions you criticize look to me as if they answered exactly your question... It's not those answers that are bad - your question is. Commented Jan 11, 2021 at 16:22
  • @dingalapadum Well consider this. How many different ways (in practise), do computers represent positive and negative integers? There's the sign magnitude method, there's 1s complement, and 2s complement. And there's "some custom method". If somebody is going to show a way, then they should state the name of the representation. If somebody writes "-1101" that's not binary is it. 'cos a minus sign is not a binary digit. If somebody is going to say that 1 is "1" and -1 is "11111" well, what's the mechanical way you are distinguishing these. What's the name of that system.
    – barlop
    Commented Jan 11, 2021 at 20:09

17 Answers 17

827

function dec2bin(dec) {
  return (dec >>> 0).toString(2);
}

console.log(dec2bin(1)); // 1
console.log(dec2bin(-1)); // 11111111111111111111111111111111
console.log(dec2bin(256)); // 100000000
console.log(dec2bin(-256)); // 11111111111111111111111100000000

You can use Number.toString(2) function, but it has some problems when representing negative numbers. For example, (-1).toString(2) output is "-1".

To fix this issue, you can use the unsigned right shift bitwise operator (>>>) to coerce your number to an unsigned integer.

If you run (-1 >>> 0).toString(2) you will shift your number 0 bits to the right, which doesn't change the number itself but it will be represented as an unsigned integer. The code above will output "11111111111111111111111111111111" correctly.

This question has further explanation.

-3 >>> 0 (right logical shift) coerces its arguments to unsigned integers, which is why you get the 32-bit two's complement representation of -3.

27
  • 12
    Here is the explanation Commented Jul 26, 2013 at 14:44
  • been a while since I tried javascript but trying it here w3schools.com/js/tryit.asp?filename=tryjs_output_alert with this <script> window.alert((-3 >>> 0).toString(2)); </script> yeah it worked
    – barlop
    Commented Apr 10, 2015 at 10:35
  • 2
    toString(2) doesn't work because you are getting the input from text. Use this: function decToBase(dec, base){ return parseInt(dec).toString(base); } alert(decToBase(dec, 2));
    – Magus
    Commented May 29, 2015 at 13:47
  • 1
    You are assuming that the input is text, but the function in the answer expect a integer... So, if input is a text just convert it to integer, use the fake bitshift and it's done Commented May 30, 2015 at 18:30
  • 1
    @barlop when you get the value from any input its type defaults to text, that's what I assumed the other guy is doing, probably getting it's input from the user which would give you a, for example, "10". That's why I suggested the parseInt, the bitwise operation suggested works because it uses a specific operator(>>>) that results into a integer, if he did it with the + operator then it would concatenate a "0" because the first number type would be a string. parsetInt() is more readable, just that.
    – Magus
    Commented Jun 3, 2015 at 23:42
317

Try

num.toString(2);

The 2 is the radix and can be any base between 2 and 36

source here

UPDATE:

This will only work for positive numbers, Javascript represents negative binary integers in two's-complement notation. I made this little function which should do the trick, I haven't tested it out properly:

function dec2Bin(dec)
{
    if(dec >= 0) {
        return dec.toString(2);
    }
    else {
        /* Here you could represent the number in 2s compliment but this is not what 
           JS uses as its not sure how many bits are in your number range. There are 
           some suggestions https://stackoverflow.com/questions/10936600/javascript-decimal-to-binary-64-bit 
        */
        return (~dec).toString(2);
    }
}

I had some help from here

6
  • 1
    doesn't work for -1. a=-1; document.write(Number(a.toString(2))); displays -1
    – barlop
    Commented Mar 30, 2012 at 9:05
  • The update still doesn't appear to work for negative numbers (-3 returns 1). Also I believe dec > 0 should be dec >= 0, which should at least fix 0. Because dec2Bin(0) returns 10. Commented Apr 15, 2014 at 21:17
  • Both cases in above comments return correct result in my chrome console - var a = -1; a.toString(2); "-1" var a = -3; a.toString(2); "-11" Commented Jul 9, 2014 at 12:01
  • @AnmolSaraf I see what you mean, and while colloquially when people say what is -5 in decimal, and the answer is -5 When it comes to negative numbers in binary , in a sense yeah you could stick a minus sign there so 5 is 101 and -5 is -101 but since computers don't store minus signs, they just represent 1s and 0s, so when we say negative numbers in binary, we really mean putting the negative number (minus sign included) in 1s and 0s. Some ways include 1s complement, 2s complement, and 'sign and magnitude'. So -101010101 or -0101010 is not what people mean by a negative number in binary.
    – barlop
    Commented Apr 10, 2015 at 10:59
  • This link may be of interest to some stackoverflow.com/questions/12337360/… anyhow, Your answer contradicts itself, you write "Javascript represents negative binary integers in two's-complement notation." And your code says " Here you could represent the number in 2s compliment but this is not what JS as uses as [nonsense reason] " And you give no reference either.
    – barlop
    Commented Feb 22, 2016 at 12:05
93

A simple way is just...

Number(42).toString(2);

// "101010"
10
  • 34
    I would prefer (42).toString(2) Commented Apr 15, 2014 at 16:57
  • 50
    Or even shorter 42..toString(2)
    – kapex
    Commented Jul 26, 2014 at 2:38
  • 10
    People are struggling with this. The answer is correct because it casts the input (42) to an integer and that line is needed. If you get your 'number' from an text input the toString(2) wouldn't work.
    – Magus
    Commented Jun 3, 2015 at 23:46
  • 5
    @Kapep, Dude that's genius. How did you know about that?
    – Pacerier
    Commented Feb 17, 2017 at 4:30
  • 11
    @BatuG. The syntax for numbers allows you to omit the part after the decimal separator. You can write 1. which is the same as 1.0 or just 1 (and similarly you can also omit the part before and write .5 instead of 0.5). So in the example the first dot is the decimal separator which is part of the number and the second dot is the dot operator for calling the method on that number. You have to use two dots (or wrap the number in parenthesis) and can't just write 42.toString(2) because the parser sees the dot as decimal separator and throws an error because of a missing dot operator.
    – kapex
    Commented Dec 12, 2018 at 8:47
60

The binary in 'convert to binary' can refer to three main things. The positional number system, the binary representation in memory or 32bit bitstrings. (for 64bit bitstrings see Patrick Roberts' answer)

1. Number System

(123456).toString(2) will convert numbers to the base 2 positional numeral system. In this system negative numbers are written with minus signs just like in decimal.

2. Internal Representation

The internal representation of numbers is 64 bit floating point and some limitations are discussed in this answer. There is no easy way to create a bit-string representation of this in javascript nor access specific bits.

3. Masks & Bitwise Operators

MDN has a good overview of how bitwise operators work. Importantly:

Bitwise operators treat their operands as a sequence of 32 bits (zeros and ones)

Before operations are applied the 64 bit floating points numbers are cast to 32 bit signed integers. After they are converted back.

Here is the MDN example code for converting numbers into 32-bit strings.

function createBinaryString (nMask) {
  // nMask must be between -2147483648 and 2147483647
  for (var nFlag = 0, nShifted = nMask, sMask = ""; nFlag < 32;
       nFlag++, sMask += String(nShifted >>> 31), nShifted <<= 1);
  return sMask;
}

createBinaryString(0) //-> "00000000000000000000000000000000"
createBinaryString(123) //-> "00000000000000000000000001111011"
createBinaryString(-1) //-> "11111111111111111111111111111111"
createBinaryString(-1123456) //-> "11111111111011101101101110000000"
createBinaryString(0x7fffffff) //-> "01111111111111111111111111111111"
14
  • 1
    What is the advantage of using this function instead of using a simple Number(num).toString(2) ?
    – Magus
    Commented Jun 3, 2015 at 23:50
  • 6
    @Magus I think I explain adequately the differences between numerals and binary strings. A 32 bit binary string is always thirty-two characters long comprised of "1"s and "0"s. toString returns an actual number represented using the positional number systems with the given base. It depends why you want the string, they have very different meanings.
    – AnnanFay
    Commented Jun 4, 2015 at 3:07
  • sorry, you are right. I jumped straight to the code.
    – Magus
    Commented Jun 4, 2015 at 4:56
  • 1
    Had an issue with leading 0s using the other posted methods (specifically on this number 536870912, the two leading zeroes are removed), but this solution handled it correctly.
    – UberMouse
    Commented Aug 5, 2015 at 4:17
  • @UberMouse yeah the >>> has the leading 0s issue, i'll accept this one.
    – barlop
    Commented Jul 24, 2017 at 21:41
36

This answer attempts to address inputs with an absolute value in the range of 214748364810 (231) – 900719925474099110 (253-1).


In JavaScript, numbers are stored in 64-bit floating point representation, but bitwise operations coerce them to 32-bit integers in two's complement format, so any approach which uses bitwise operations restricts the range of output to -214748364810 (-231) – 214748364710 (231-1).

However, if bitwise operations are avoided and the 64-bit floating point representation is preserved by using only mathematical operations, we can reliably convert any safe integer to 64-bit two's complement binary notation by sign-extending the 53-bit twosComplement:

function toBinary (value) {
  if (!Number.isSafeInteger(value)) {
    throw new TypeError('value must be a safe integer');
  }

  const negative = value < 0;
  const twosComplement = negative ? Number.MAX_SAFE_INTEGER + value + 1 : value;
  const signExtend = negative ? '1' : '0';

  return twosComplement.toString(2).padStart(53, '0').padStart(64, signExtend);
}

function format (value) {
  console.log(value.toString().padStart(64));
  console.log(value.toString(2).padStart(64));
  console.log(toBinary(value));
}

format(8);
format(-8);
format(2**33-1);
format(-(2**33-1));
format(2**53-1);
format(-(2**53-1));
format(2**52);
format(-(2**52));
format(2**52+1);
format(-(2**52+1));
.as-console-wrapper{max-height:100%!important}

For older browsers, polyfills exist for the following functions and values:

As an added bonus, you can support any radix (2–36) if you perform the two's complement conversion for negative numbers in ⌈64 / log2(radix)⌉ digits by using BigInt:

function toRadix (value, radix) {
  if (!Number.isSafeInteger(value)) {
    throw new TypeError('value must be a safe integer');
  }

  const digits = Math.ceil(64 / Math.log2(radix));
  const twosComplement = value < 0
    ? BigInt(radix) ** BigInt(digits) + BigInt(value)
    : value;

  return twosComplement.toString(radix).padStart(digits, '0');
}

console.log(toRadix(0xcba9876543210, 2));
console.log(toRadix(-0xcba9876543210, 2));
console.log(toRadix(0xcba9876543210, 16));
console.log(toRadix(-0xcba9876543210, 16));
console.log(toRadix(0x1032547698bac, 2));
console.log(toRadix(-0x1032547698bac, 2));
console.log(toRadix(0x1032547698bac, 16));
console.log(toRadix(-0x1032547698bac, 16));
.as-console-wrapper{max-height:100%!important}

If you are interested in my old answer that used an ArrayBuffer to create a union between a Float64Array and a Uint16Array, please refer to this answer's revision history.

7
  • Thanks, it is good that this works for 64bit.. Can you let me know any advantages of this answer over annan's answer?
    – barlop
    Commented Jul 24, 2017 at 21:51
  • 4
    Much larger range? It works for -(2**53)-1 to 2**53-1 instead of just -(2**31) to 2**31-1 like annan's answer. Commented Jul 24, 2017 at 22:01
  • Yes that is a big advantage, I get that, and that will do, though it is quite a bit more code, but what I meant was, i'm curious if any other advantages?
    – barlop
    Commented Jul 25, 2017 at 14:31
  • 1
    from 2**32+1 on, last (rightmost) bit is cleared when it should be set.
    – Lovro
    Commented Nov 10, 2018 at 17:25
  • 1
    Works when the line is: var exponent = ((uint16[3] & 0x7FF0) >> 4) - 1023 + 1;
    – Lovro
    Commented Nov 10, 2018 at 17:33
33

A solution i'd go with that's fine for 32-bits, is the code the end of this answer, which is from developer.mozilla.org(MDN), but with some lines added for A)formatting and B)checking that the number is in range.

Some suggested x.toString(2) which doesn't work for negatives, it just sticks a minus sign in there for them, which is no good.

Fernando mentioned a simple solution of (x>>>0).toString(2); which is fine for negatives, but has a slight issue when x is positive. It has the output starting with 1, which for positive numbers isn't proper 2s complement.

Anybody that doesn't understand the fact of positive numbers starting with 0 and negative numbers with 1, in 2s complement, could check this SO QnA on 2s complement. What is “two's complement”?

A solution could involve prepending a 0 for positive numbers, which I did in an earlier revision of this answer. And one could accept sometimes having a 33bit number, or one could make sure that the number to convert is within range -(2^31)<=x<2^31-1. So the number is always 32bits. But rather than do that, you can go with this solution on mozilla.org

Patrick's answer and code is long and apparently works for 64-bit, but had a bug that a commenter found, and the commenter fixed patrick's bug, but patrick has some "magic number" in his code that he didn't comment about and has forgotten about and patrick no longer fully understands his own code / why it works.

Annan had some incorrect and unclear terminology but mentioned a solution by developer.mozilla.org

Note- the old link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators now redirects elsewhere and doesn't have that content but the proper old link , which comes up when archive.org retrieves pages!, is available here https://web.archive.org/web/20150315015832/https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators

The solution there works for 32-bit numbers.

The code is pretty compact, a function of three lines.

But I have added a regex to format the output in groups of 8 bits. Based on How to format a number with commas as thousands separators? (I just amended it from grouping it in 3s right to left and adding commas, to grouping in 8s right to left, and adding spaces)

And, while mozilla made a comment about the size of nMask(the number fed in)..that it has to be in range, they didn't test for or throw an error when the number is out of range, so i've added that.

Regarding why they named their parameter 'nMask', I wasn't sure but commenter Magne mentioned that it "might be because the integer is treated as a bitmask, and nMask then refers to multiple masks (a set of one or more masks, joined into one). See section "Automate Mask Creation" at that Mozille bitwise operators link.

https://web.archive.org/web/20150315015832/https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators

function createBinaryString(nMask) {
  // nMask must be between -2147483648 and 2147483647
  if (nMask > 2**31-1) 
     throw "number too large. number shouldn't be > 2**31-1"; //added
  if (nMask < -1*(2**31))
     throw "number too far negative, number shouldn't be < -(2**31)" //added
  for (var nFlag = 0, nShifted = nMask, sMask = ''; nFlag < 32;
       nFlag++, sMask += String(nShifted >>> 31), nShifted <<= 1);
  sMask=sMask.replace(/\B(?=(.{8})+(?!.))/g, " ") // added
  return sMask;
}


console.log(createBinaryString(-1))    // "11111111 11111111 11111111 11111111"
console.log(createBinaryString(1024))  // "00000000 00000000 00000100 00000000"
console.log(createBinaryString(-2))    // "11111111 11111111 11111111 11111110"
console.log(createBinaryString(-1024)) // "11111111 11111111 11111100 00000000"

//added further console.log example
console.log(createBinaryString(2**31 -1)) //"01111111 11111111 11111111 11111111"  

2
  • nMask name might be because the integer is treated as a bitmask, and nMask then refers to multiple masks (a set of one or more masks, joined into one). See section "Automate Mask Creation": developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
    – Magne
    Commented Jun 24, 2020 at 7:59
  • 1
    @Magne thanks, i've incorporated that into my answer now
    – barlop
    Commented Nov 17, 2023 at 0:19
10

You can write your own function that returns an array of bits. Example how to convert number to bits

Divisor| Dividend| bits/remainder

2 | 9 | 1

2 | 4 | 0

2 | 2 | 0

~ | 1 |~

example of above line: 2 * 4 = 8 and remainder is 1 so 9 = 1 0 0 1

function numToBit(num){
    var number = num
    var result = []
    while(number >= 1 ){
        result.unshift(Math.floor(number%2))
        number = number/2
    }
    return result
}

Read remainders from bottom to top. Digit 1 in the middle to top.

2
  • 1
    Btw, why do you Math.floor(number%2) instead of number = Math.floor(number/2)?
    – Pacerier
    Commented Feb 17, 2017 at 4:39
  • 2
    The reason is number%2 is not equal to number/2. We are interested in remainder not quotient. Commented Feb 18, 2017 at 18:58
5

This is how I manage to handle it:

const decbin = nbr => {
  if(nbr < 0){
     nbr = 0xFFFFFFFF + nbr + 1
  }
  return parseInt(nbr, 10).toString(2)
};

got it from this link: https://locutus.io/php/math/decbin/

3
  • @barlop thanks for raising the issues, it's now edited
    – gildniy
    Commented Aug 10, 2020 at 7:17
  • 1
    can you explain the logic behind that code, how it works.. that by adding 0xFFFFFFFF+1 to negative numbers then it works... and if you got the code from somewhere can you link to a source? Thanks
    – barlop
    Commented Aug 10, 2020 at 10:18
  • @barlop, got it from this link: locutus.io/php/math/decbin
    – gildniy
    Commented Aug 10, 2020 at 10:28
3

An actual solution that logic can be implemented by any programming language:

If you sure it is positive only:

var a = 0;
var n = 12; // your input
var m = 1;
while(n) {
    a = a + n%2*m;
    n = Math.floor(n/2);
    m = m*10;
}

console.log(n, ':', a) // 12 : 1100

If can negative or positive -

(n >>> 0).toString(2)
5
  • You haven't even read the first line of the question, which said for negative numbers too. Your solution doesn't work for negative numbers.
    – barlop
    Commented May 12, 2022 at 22:38
  • @barlop you should respect the ideal thing, not you're looking for. Js is a loosely language you know. you can easily do it by (n >>> 0).toString(2)
    – Yadab Sd
    Commented May 14, 2022 at 8:10
  • 1
    Try reading other answers instead of posting an answer that doesn't answer it(doesn't address negative numbers), then adjusting your answer with a copy of a solution that is mentioned in another answer, to do it for negatives. By the way, MDN has a solution too, but all that is mentioned in other answers, so you aren't adding anything here.
    – barlop
    Commented Jul 6, 2022 at 18:08
  • @barlop then you should search for similar answers instead of asking 😅...
    – Yadab Sd
    Commented Jul 6, 2022 at 18:11
  • What are you talking about. Nobody asked you anything. And this question was already answered long before you posted an answer
    – barlop
    Commented Jul 6, 2022 at 18:35
2

we can also calculate the binary for positive or negative numbers as below:

function toBinary(n){
    let binary = "";
    if (n < 0) {
      n = n >>> 0;
    }
    while(Math.ceil(n/2) > 0){
        binary = n%2 + binary;
        n = Math.floor(n/2);
    }
    return binary;
}

console.log(toBinary(7));
console.log(toBinary(-7));

3
  • questioned wanted negative too
    – barlop
    Commented Aug 26, 2020 at 13:53
  • converted the negative numbers to unsigned representation. Now the logic will work for positive or negative numbers. Thanks @barlop Commented Aug 27, 2020 at 7:22
  • putting the positive numbers starting with 1 e.g. 7 as 111, are a problem. 'cos if you want positive numbers to start from 1, then how do you know what 111 is, whether it's 7 or -1. Your program puts -1 as 11111111111111111111111111111111 and 7 as 111. In 2s complement, 1111111 and 111 are the same number. -1.
    – barlop
    Commented Aug 27, 2020 at 15:55
2

You could use a recursive solution:

function intToBinary(number, res = "") {
  if (number < 1)
    if (res === "") return "0"
      else 
     return res
  else return intToBinary(Math.floor(number / 2), number % 2 + res)
}
console.log(intToBinary(12))
console.log(intToBinary(546))
console.log(intToBinary(0))
console.log(intToBinary(125))
Works only with positive numbers.

2

I’d like to see integers, positive or negative, in binary.

This is an old question and I think there are very nice solutions here but there is no explanation about the use of these clever solutions.

First, we need to understand that a number can be positive or negative. Also, JavaScript provides a MAX_SAFE_INTEGER constant that has a value of 9007199254740991. The reasoning behind that number is that JavaScript uses double-precision floating-point format numbers as specified in IEEE 754 and can only safely represent integers between -(2^53 - 1) and 2^53 - 1.

So, now we know the range where numbers are "safe". Also, JavaScript ES6 has the built-in method Number.isSafeInteger() to check if a number is a safe integer.

Logically, if we want to represent a number n as binary, this number needs a length of 53 bits, but for better presentation lets use 7 groups of 8 bits = 56 bits and fill the left side with 0 or 1 based on its sign using the padStart function.

Next, we need to handle positive and negative numbers: positive numbers will add 0s to the left while negative numbers will add 1s. Also, negative numbers will need a two's-complement representation. We can easily fix this by adding Number.MAX_SAFE_INTEGER + 1 to the number.

For example, we want to represent -3 as binary, lets assume that Number.MAX_SAFE_INTEGER is 00000000 11111111 (255) then Number.MAX_SAFE_INTEGER + 1 will be 00000001 00000000 (256). Now lets add the number Number.MAX_SAFE_INTEGER + 1 - 3 this will be 00000000 11111101 (253) but as we said we will fill with the left side with 1 like this 11111111 11111101 (-3), this represent -3 in binary.

Another algorithm will be we add 1 to the number and invert the sign like this -(-3 + 1) = 2 this will be 00000000 00000010 (2). Now we invert every bit like this 11111111 11111101 (-3) again we have a binary representation of -3.

Here we have a working snippet of these algos:

function dec2binA(n) {
    if (!Number.isSafeInteger(n)) throw new TypeError('n value must be a safe integer')
    if (n > 2**31) throw 'number too large. number should not be greater than 2**31'
    if (n < -1*(2**31)) throw 'number too far negative, number should not be lesser than 2**31'

    const bin = n < 0 ? Number.MAX_SAFE_INTEGER + 1 + n : n
    const signBit = n < 0 ? '1' : '0'

    return parseInt(bin, 10).toString(2)
        .padStart(56, signBit)
        .replace(/\B(?=(.{8})+(?!.))/g, ' ')
}


function dec2binB(n) {
    if (!Number.isSafeInteger(n)) throw new TypeError('n value must be a safe integer')
    if (n > 2**31) throw 'number too large. number should not be greater than 2**31'
    if (n < -1*(2**31)) throw 'number too far negative, number should not be lesser than 2**31'

    const bin = n < 0 ?  -(1 + n) : n
    const signBit = n < 0 ? '1' : '0'

    return parseInt(bin, 10).toString(2)
        .replace(/[01]/g, d => +!+d)
        .padStart(56, signBit)
        .replace(/\B(?=(.{8})+(?!.))/g, ' ')
}



const a = -805306368
console.log(a)
console.log('dec2binA:', dec2binA(a))
console.log('dec2binB:', dec2binB(a))

const b = -3
console.log(b)
console.log('dec2binA:', dec2binA(b))
console.log('dec2binB:', dec2binB(b))

0

One more alternative

const decToBin = dec => {
  let bin = '';
  let f = false;

  while (!f) {
    bin = bin + (dec % 2);    
    dec = Math.trunc(dec / 2);  

    if (dec === 0 ) f = true;
  }

  return bin.split("").reverse().join("");
}

console.log(decToBin(0));
console.log(decToBin(1));
console.log(decToBin(2));
console.log(decToBin(3));
console.log(decToBin(4));
console.log(decToBin(5));
console.log(decToBin(6));
4
  • Please see Vincent's answer and the comment on it, it'd apply to your posting too
    – barlop
    Commented Apr 26, 2020 at 19:07
  • This is what was posted in comment on his answer, without disagreement, and with some agreement from others, "That may be useful for studying computer science to see how to do it manually, so as to teach yourself, but that is not what I am asking! If you are going to reinvent the wheel doing it manually like that, then it should be at least with the advantage of increased efficiency or some advantage like increase in the size of the values it can cope with. I don't see any discussion from you stating any such advantage there."
    – barlop
    Commented Apr 26, 2020 at 19:08
  • Furthermore, your solution completely fails, it makes positive numbers start with a 1 and completely fails for negative numbers, and my question mentioned positive or negative
    – barlop
    Commented Apr 26, 2020 at 19:11
  • So your "answer" is wrong on many many levels. And you should always review other answers before posting an answer
    – barlop
    Commented Apr 26, 2020 at 19:11
-1

I used a different approach to come up with something that does this. I've decided to not use this code in my project, but I thought I'd leave it somewhere relevant in case it is useful for someone.

  • Doesn't use bit-shifting or two's complement coercion.
  • You choose the number of bits that comes out (it checks for valid values of '8', '16', '32', but I suppose you could change that)
  • You choose whether to treat it as a signed or unsigned integer.
  • It will check for range issues given the combination of signed/unsigned and number of bits, though you'll want to improve the error handling.
  • It also has the "reverse" version of the function which converts the bits back to the int. You'll need that since there's probably nothing else that will interpret this output :D

function intToBitString(input, size, unsigned) {
	if ([8, 16, 32].indexOf(size) == -1) {
		throw "invalid params";
	}
	var min = unsigned ? 0 : - (2 ** size / 2);
        var limit = unsigned ? 2 ** size : 2 ** size / 2;
	if (!Number.isInteger(input) || input < min || input >= limit) {
		throw "out of range or not an int";
	}
	if (!unsigned) {
		input += limit;
	}
	var binary = input.toString(2).replace(/^-/, '');
	return binary.padStart(size, '0');
}

function bitStringToInt(input, size, unsigned) {
	if ([8, 16, 32].indexOf(size) == -1) {
		throw "invalid params";
	}
	input = parseInt(input, 2);
	if (!unsigned) {
		input -= 2 ** size / 2;
	}
	return input;
}


// EXAMPLES

var res;
console.log("(uint8)10");
res = intToBitString(10, 8, true);
console.log("intToBitString(res, 8, true)");
console.log(res);
console.log("reverse:", bitStringToInt(res, 8, true));
console.log("---");

console.log("(uint8)127");
res = intToBitString(127, 8, true);
console.log("intToBitString(res, 8, true)");
console.log(res);
console.log("reverse:", bitStringToInt(res, 8, true));
console.log("---");

console.log("(int8)127");
res = intToBitString(127, 8, false);
console.log("intToBitString(res, 8, false)");
console.log(res);
console.log("reverse:", bitStringToInt(res, 8, false));
console.log("---");

console.log("(int8)-128");
res = intToBitString(-128, 8, false);
console.log("intToBitString(res, 8, true)");
console.log(res);
console.log("reverse:", bitStringToInt(res, 8, true));
console.log("---");

console.log("(uint16)5000");
res = intToBitString(5000, 16, true);
console.log("intToBitString(res, 16, true)");
console.log(res);
console.log("reverse:", bitStringToInt(res, 16, true));
console.log("---");

console.log("(uint32)5000");
res = intToBitString(5000, 32, true);
console.log("intToBitString(res, 32, true)");
console.log(res);
console.log("reverse:", bitStringToInt(res, 32, true));
console.log("---");

9
  • doesn't your own test data fail, clearly -128 is not 00000000
    – barlop
    Commented Aug 26, 2020 at 13:55
  • @barlop the signed data type int8 goes from -128 (00000000) to 127 (11111111), so this is as I've intended. My needs didn't need interoperability with another scheme.
    – braks
    Commented Mar 14, 2021 at 9:08
  • well if -128 is all zeros in your representations, then how are you going to be representing 0?
    – barlop
    Commented Mar 12, 2022 at 10:56
  • I'm sure if you put your mind to it you can figure it out
    – braks
    Commented Mar 16, 2022 at 14:51
  • look, -128 should not be all 000000 you have got it completely wrong and your claim about signed int being -128 (00000000) to 127 (11111111) is just made up and totally wrong..
    – barlop
    Commented Mar 16, 2022 at 17:29
-1

This is a method that I use. It's a very fast and concise method that works for whole numbers.

If you want, this method also works with BigInts. You just have to change each 1 to 1n.

// Assuming {num} is a whole number
function toBin(num){
    let str = "";
    do {
        str = `${num & 1}${str}`;
        num >>= 1;
    } while(num);
    return str
}

Explanation

This method, in a way, goes through all the bits of the number as if it's already a binary number.

It starts with an empty string, and then it prepends the last bit. num & 1 will return the last bit of the number (1 or 0). num >>= 1 then removes the last bit and makes the second-to-last bit the new last bit. The process is repeated until all the bits have been read.

Of course, this is an extreme simplification of what's actually going on. But this is how I generalize it.

1
  • your first sentence says ".toString(2) is probably your best bet.". -- Now try reading the first comment to the question. You see it says it only works for positive numbers. And try reading all the other answers that mention that issue with .toString(2). As for your function, it's interesting, you should state whether it works for positive numbers only, or for positive and negative numbers.
    – barlop
    Commented Mar 12, 2022 at 10:51
-2

This is my code:

var x = prompt("enter number", "7");
var i = 0;
var binaryvar = " ";

function add(n) {
    if (n == 0) {
        binaryvar = "0" + binaryvar; 
    }
    else {
        binaryvar = "1" + binaryvar;
    }
}

function binary() {
    while (i < 1) {
        if (x == 1) {
            add(1);
            document.write(binaryvar);
            break;
        }
        else {
            if (x % 2 == 0) {
                x = x / 2;
                add(0);
            }
            else {
                x = (x - 1) / 2;
                add(1);
            }
        }
    }
}

binary();
1
  • 3
    That may be useful for studying computer science to see how to do it manually, so as to teach yourself, but that is not what I am asking! If you are going to reinvent the wheel doing it manually like that, then it should be at least with the advantage of increased efficiency or some advantage like increase in the size of the values it can cope with. I don't see any discussion from you stating any such advantage there.
    – barlop
    Commented Nov 7, 2017 at 15:41
-3

This is the solution . Its quite simple as a matter of fact

function binaries(num1){ 
        var str = num1.toString(2)
        return(console.log('The binary form of ' + num1 + ' is: ' + str))
     }
     binaries(3

)

        /*
         According to MDN, Number.prototype.toString() overrides 
         Object.prototype.toString() with the useful distinction that you can 
         pass in a single integer argument. This argument is an optional radix, 
         numbers 2 to 36 allowed.So in the example above, we’re passing in 2 to 
         get a string representation of the binary for the base 10 number 100, 
         i.e. 1100100.
        */
2
  • 1
    That solution has already been proposed many times and, as commented by OP already on Mar 30 '12 at 9:01, does not work for negative numbers.
    – Adrian W
    Commented Jun 12, 2018 at 8:25
  • 1
    @AdrianW I suggest downvoting this. I notice you haven't. What does it take for you to downvote an answer then?!
    – barlop
    Commented Jun 12, 2018 at 23:34

Not the answer you're looking for? Browse other questions tagged or ask your own question.