問題描述
我想查看整數(shù),無論??是正整數(shù)還是負(fù)整數(shù).
I’d like to see integers, positive or negative, in binary.
比較喜歡這個(gè)問題,但對(duì)于 JavaScript.
Rather like this question, but for JavaScript.
推薦答案
我會(huì)采用的解決方案適用于 32 位,是這個(gè)答案結(jié)尾的代碼,來自 developer.mozilla.org(MDN),但添加了一些行用于 A) 格式化和 B) 檢查數(shù)字是否在范圍內(nèi).
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.
一些建議的 x.toString(2)
不適用于負(fù)數(shù),它只是在其中貼一個(gè)減號(hào),這不好.
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 提到了 (x>>>0).toString(2);
的簡(jiǎn)單解決方案,它適用于負(fù)數(shù),但當(dāng) x 為正數(shù)時(shí)有一個(gè)小問題.它的輸出以 1 開頭,對(duì)于正數(shù),它不是正確的 2s 補(bǔ)碼.
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.
任何不了解以 0 開頭的正數(shù)和以 1 開頭的負(fù)數(shù)在 2s 補(bǔ)碼中的事實(shí)的任何人都可以檢查此 SO QnA on 2s 補(bǔ)碼.什么是2的補(bǔ)碼"?
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 "2's Complement"?
解決方案可能涉及為正數(shù)添加 0,這是我在此答案的早期版本中所做的.有時(shí)可以接受 33 位數(shù)字,或者可以確保要轉(zhuǎn)換的數(shù)字在 -(2^31)<=x<2^31-1 范圍內(nèi).所以這個(gè)數(shù)字總是32位.但是,您可以在 mozilla.org 上使用此解決方案,而不是這樣做
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 的答案和代碼很長,顯然適用于 64 位,但有一個(gè)評(píng)論者發(fā)現(xiàn)的錯(cuò)誤,評(píng)論者修復(fù)了 patrick 的錯(cuò)誤,但 patrick 的代碼中有一些他沒有評(píng)論的神奇數(shù)字"關(guān)于并且已經(jīng)忘記并且帕特里克不再完全理解他自己的代碼/它為什么起作用.
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 有一些不正確和不清楚的術(shù)語,但提到 developer.mozilla.org 的解決方案 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators 這適用于 32 位數(shù)字.
Annan had some incorrect and unclear terminology but mentioned a solution by developer.mozilla.org https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators This works for 32-bit numbers.
代碼很緊湊,三行函數(shù).
The code is pretty compact, a function of three lines.
但是我已經(jīng)添加了一個(gè)正則表達(dá)式來格式化輸出 8 位組.基于 如何打印數(shù)字逗號(hào)作為 JavaScript 中的千位分隔符(我只是將其修改為從右到左將其分組為 3s 并添加 commas,以分組為 8s 從右到左,并添加 空格)
But I have added a regex to format the output in groups of 8 bits. Based on How to print a number with commas as thousands separators in JavaScript (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)
而且,雖然 mozilla 對(duì) nMask 的大小(輸入的數(shù)字)發(fā)表了評(píng)論..它必須在范圍內(nèi),但當(dāng)數(shù)字超出范圍時(shí),他們沒有測(cè)試或拋出錯(cuò)誤,所以我已經(jīng)添加了.
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.
我不確定他們?yōu)槭裁磳?shù)命名為nMask",但我會(huì)保持原樣.
I'm not sure why they named their parameter 'nMask' but i'll leave that as is.
參考: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"
這篇關(guān)于如何在 JavaScript 中將整數(shù)轉(zhuǎn)換為二進(jìn)制?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!