Skip to main content

Timeline for answer to How do I convert an integer to binary in JavaScript? by AnnanFay

Current License: CC BY-SA 3.0

Post Revisions

26 events
when toggle format what by license comment
Mar 10, 2023 at 0:01 history wiki removed Makyen
Mar 9, 2023 at 23:59 history made wiki Post Made Community Wiki by Makyen
Jul 25, 2017 at 3:14 comment added barlop Let us continue this discussion in chat.
Jul 25, 2017 at 3:14 comment added AnnanFay @barlop They are homonyms.
Jul 25, 2017 at 3:13 comment added barlop Also you wrote "(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." <--- If it gives a minus sign followed by a decimal number then there it clearly isn't showing a number converted to base 2.
Jul 25, 2017 at 3:12 comment added barlop @Annan then it's not clear to me whether you're trying to say they're alternatives, like "either or" options.
Jul 25, 2017 at 3:10 comment added AnnanFay @barlop They are different. "The internal representation is a bit string that uses the positional numbering system!": Nope.
Jul 25, 2017 at 3:09 comment added barlop @Annan You write "The phrase 'convert to binary' can refer to the positional number system or bitstrings or internal representation" <-- it's not clear to me whether you're trying to say those are all the same thing, or an either or, but neither would be correct. The internal representation is a bit string that uses the positional numbering system!
Jul 25, 2017 at 2:58 history edited AnnanFay CC BY-SA 3.0
added 106 characters in body
Jul 25, 2017 at 2:48 history edited AnnanFay CC BY-SA 3.0
Further clarification that my answer covers three different interpretations of the question.
Jul 25, 2017 at 2:43 history rollback AnnanFay
Rollback to Revision 2
Jul 25, 2017 at 2:40 comment added AnnanFay @barlop Yes. The phrase 'convert to binary' can refer to the positional number system or bitstrings or internal representation (hence the three sections in my answer). This positional system is exactly the same as decimal however uses radix 2. Thus it can display negative and floating point numbers, for example -2.25 (decimal) becomes -10.01 (binary). I linked to the wikipedia article which adequately explains positional number systems. No offense but I will roll back your edits as several are better left to comments. If there is something you wish me readd please say.
Jul 24, 2017 at 22:29 history edited barlop CC BY-SA 3.0
added 55 characters in body
Jul 24, 2017 at 22:23 comment added barlop @UberMouse yes that is right.. how about x=536870912; if(x>0) prepend="0"; else prepend=""; alert(prepend+((x>>>0)).toString(2).toString());
Jul 24, 2017 at 22:06 history edited barlop CC BY-SA 3.0
added 366 characters in body
Jul 24, 2017 at 22:01 comment added barlop I have accepted this answer because it doesn't have the missing 0 issue. And i've added two notes.. But where you wrote regarding .toString(2), "it converts numbers to the base 2 positional numeral system. In this system just like in decimal." Are you sure that 2s complement isn't also a base 2 positional numeral system?!
Jul 24, 2017 at 21:59 history edited barlop CC BY-SA 3.0
added 366 characters in body
Jul 24, 2017 at 21:48 history edited barlop CC BY-SA 3.0
added 366 characters in body
Jul 24, 2017 at 21:41 vote accept barlop
Jul 25, 2017 at 3:06
Jul 24, 2017 at 21:41 comment added barlop @UberMouse yeah the >>> has the leading 0s issue, i'll accept this one.
May 23, 2017 at 12:26 history edited URL Rewriter Bot
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Aug 5, 2015 at 4:17 comment added UberMouse 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.
Jun 4, 2015 at 4:56 comment added Magus sorry, you are right. I jumped straight to the code.
Jun 4, 2015 at 3:07 comment added AnnanFay @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.
Jun 3, 2015 at 23:50 comment added Magus What is the advantage of using this function instead of using a simple Number(num).toString(2) ?
Jun 11, 2014 at 1:19 history answered AnnanFay CC BY-SA 3.0