Skip to main content

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

Current License: CC BY-SA 4.0

Post Revisions

56 events
when toggle format what by license comment
Nov 1, 2025 at 1:27 comment added fernandosavio @MrBoJangles As mentioned above, this solution only work if you can represent the number with 32bits. But totally agree, if you need this in serious project this is not a safe solution.
Jan 15, 2025 at 19:27 comment added MrBoJangles For what it's worth, this coding exercise failed on a test for 5934405041, whereas Manatok's answer with the ~ operator used on negative numbers did the trick. codewars.com/kata/526571aae218b8ee490006f4/train/javascript
Mar 10, 2023 at 0:00 history wiki removed Makyen
Mar 9, 2023 at 23:59 history made wiki Post Made Community Wiki by Makyen
Nov 30, 2021 at 14:22 comment added fernandosavio As mentioned in the answer the result is coerced to a 32bit unsigned int, so the upper limit will be Math.pow(2, 32) which is 4294967296. That's why you are getting the wrong number. Thanks for the comment, it will help future visitors on this matter. :)
Nov 28, 2021 at 19:08 comment added Дан Воронов on 6449281454 got 10000000011010000011100110101110 but answer is 110000000011010000011100110101110
Mar 15, 2021 at 22:30 history edited fernandosavio CC BY-SA 4.0
deleted 2 characters in body
S Mar 15, 2021 at 22:30 history suggested Legna CC BY-SA 4.0
Added snippet
Mar 15, 2021 at 15:54 review Suggested edits
S Mar 15, 2021 at 22:30
Aug 15, 2019 at 17:56 history edited fernandosavio CC BY-SA 4.0
deleted 192 characters in body
Jun 5, 2019 at 9:21 history edited barlop CC BY-SA 4.0
deleted 11 characters in body
Sep 19, 2018 at 21:22 history edited fernandosavio CC BY-SA 4.0
added 3 characters in body
Sep 20, 2017 at 0:35 comment added fernandosavio Nope. ~x is binary not, x >>> 0 is a bitshift operator which shift 0 bits to right. In this case it will just coerce the number to a unsigned 32 bit integer...
Sep 18, 2017 at 23:34 comment added SparK x >>> 0 does the same as ~x;
Jul 25, 2017 at 14:36 comment added barlop Also you presented a significant problem as if it was a formatting issue (padding with zeros), and as a Note 2, secondary to what you called Note 1 which was your response to that Magus commenter that had a string from textbox that was irrelevant to the question.
Jul 24, 2017 at 22:24 comment added barlop An improvement to get that important 0 at the beginning of positive numbers, would be (and you can make x= whatever number), amending first line accordingly x=536870912; if(x>0) prepend="0"; else prepend=""; alert(prepend+((x>>>0)).toString(2).toString()); I have mentioned this in my answer, that gets the correct result with >>>
Jul 24, 2017 at 21:41 comment added barlop Also not only have you removed the important point about the result being wrong for not having the leading 0 for positive numbers, there is another answer or two , one of which is simple and they don't have that mistake, so I have changed that one to the accept answer.
Jul 24, 2017 at 21:38 comment added barlop My point about the fact that the result isn't really proper 2s complement and technically the result is wrong.. unless you ensure positive numbers have a leading 0. You don't make that point clearly by stating at the end to pad accordingly, which says nothing about 2s complement, nothing about the result not really being correct without , and nothing about leading 0 for positive numbers, and just saying "accordingly" says nothing about what to do for the correct answer
Jul 24, 2017 at 21:37 comment added barlop Where you wrote -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. It actually explains very little and in fact, 2s complement is SIGNED, so to say you're getting an unsigned integer is really unclear.
Jul 24, 2017 at 17:40 comment added fernandosavio @barlop I've edited the question, I think it is much better now. Fell free to improve it if you want.
Jul 24, 2017 at 17:39 history edited fernandosavio CC BY-SA 3.0
full edit, the former answer wasn't clear and had pointless text.
Jul 23, 2017 at 16:30 comment added barlop @fernandosavio Be careful when you edit. When you first wrote the answer it was so bad that a wrong answer got more votes. It was only after I edited your answer that it got more votes than the wrong answer. Would you let me remove the entire addressing magus part? Also making a function as you did was pointless when you could've done just one line like in my example. Can I remove the magus part? Can I remove your function and just use that single line example? That would improve things a lot. Removing the magus part would be the main improvement, if you at least let me do that.
Jul 3, 2017 at 12:10 comment added fernandosavio Yeah, I know it is the correct answer. I just didn't get why test the answer now, I've tested when I posted. :)
Jul 1, 2017 at 21:27 comment added barlop @fernandosavio because that's the answer, that is -3 in 2s complement. To get -3 you take 3, then invert the bits and add 1. So 0011 becomes 1100 which becomes 1101
Jul 1, 2017 at 18:34 comment added fernandosavio 11111111111111111111111111111101 on Firefox and Chrome. Why?
Jul 1, 2017 at 12:28 comment added barlop can do quick test of javascript:alert((-3>>>0).toString(2)); <-- in address bar
Jul 1, 2017 at 12:19 history edited barlop CC BY-SA 3.0
added 9 characters in body
Jun 27, 2017 at 15:21 history edited barlop CC BY-SA 3.0
made it more clear
May 23, 2017 at 12:02 history edited URL Rewriter Bot
replaced http://stackoverflow.com/ with https://stackoverflow.com/
S Oct 13, 2016 at 9:58 history edited Aamir CC BY-SA 3.0
Removed extra bracket which results in syntax error.
S Oct 13, 2016 at 9:58 history suggested WillKre CC BY-SA 3.0
Removed extra bracket which results in syntax error. Made note bold as min char change is 6
Oct 13, 2016 at 9:43 review Suggested edits
S Oct 13, 2016 at 9:58
Aug 23, 2016 at 23:09 history edited barlop CC BY-SA 3.0
added correction
S Apr 14, 2016 at 20:32 history suggested barlop CC BY-SA 3.0
makes clearer that that last sentence is not part of the general answer, but is part of the note for Magus
Apr 14, 2016 at 20:31 review Suggested edits
S Apr 14, 2016 at 20:32
Feb 18, 2016 at 9:58 comment added barlop further explanations here stackoverflow.com/questions/1822350/…
Jan 31, 2016 at 21:48 review Suggested edits
Jan 31, 2016 at 22:49
Nov 20, 2015 at 14:22 history edited fernandosavio CC BY-SA 3.0
barlop's review
Nov 17, 2015 at 14:05 comment added barlop Your answer is so unconcisely written, that a wrong answer actually got 20 more upvots than yours. You haven't shown what -3 looks like in binary, and the whole edit about parseInt (for one commenter that is taking input from a textbox and doesn't know how to convert strings to numbers) is a big distraction that makes it look like you are fixing a broken answer. I'll leave your answer as accepted though, because you found the answer and yours is the answer. (-3>>>0).toString(2); prints 11111101 which is -3 in 2s complement.
Nov 17, 2015 at 11:28 review Suggested edits
Nov 17, 2015 at 11:50
Jun 18, 2015 at 12:33 comment added fernandosavio @PaulFitzgerald, it is bit shift operations... Here and here are good articles about it..
Jun 17, 2015 at 22:25 comment added Paul Fitzgerald @fernandosavio can you explain, or provide an article where I can see what >>> means? I have never seen it before
Jun 16, 2015 at 21:40 history edited nickf CC BY-SA 3.0
Remove the text about not knowing why it works
Jun 8, 2015 at 17:17 history edited fernandosavio CC BY-SA 3.0
Improved answer
Jun 4, 2015 at 4:55 comment added Magus @barlop I was making the answer better. Since it's javascript there are people that may come here needing to know why the .toString(2) was not working. And that was my case, I needed that function form an app in phonegap and it didn't work for me then I figured it was because I was getting the input from a text field and came back to share what I've learned. That's what the comments are for. That's how programming works, there is more then one correct answer to the same problem and I've provided one more way to solve it. In my view, better then a bitwise because it's clearer. Don't be mad.
Jun 4, 2015 at 0:13 comment added barlop @Magus this was a specific technical question not about user input. No1 is taking user input,there is no text here n if there was then no1 is scratching their head trying2figure out how2convert a string 2an int, this question is beyond that.Obviously if they did take user input and so got a string then they can use parseInt but that obviously wasn't the question.The q got2the crux of a problem and that is how a question should be.The Qgave the purest form of the problem and got a pure answer.Hence I commented (-3 >>> 0).toString(2) does it.Every ans understood that other than your comment
Jun 3, 2015 at 23:42 comment added Magus @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.
Jun 2, 2015 at 9:58 comment added barlop @Magus who is getting the input from text?!
May 30, 2015 at 18:30 comment added fernandosavio 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
May 29, 2015 at 13:47 comment added Magus 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));
Apr 10, 2015 at 10:35 comment added barlop 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
Apr 10, 2015 at 10:34 vote accept barlop
Jul 24, 2017 at 21:41
Nov 21, 2014 at 11:11 history edited fernandosavio CC BY-SA 3.0
improved answer
Jul 26, 2013 at 14:44 comment added fernandosavio Here is the explanation
May 23, 2013 at 18:17 history edited fernandosavio CC BY-SA 3.0
correcting spelling
Apr 22, 2013 at 19:43 history answered fernandosavio CC BY-SA 3.0