久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

<i id='UsxLr'><tr id='UsxLr'><dt id='UsxLr'><q id='UsxLr'><span id='UsxLr'><b id='UsxLr'><form id='UsxLr'><ins id='UsxLr'></ins><ul id='UsxLr'></ul><sub id='UsxLr'></sub></form><legend id='UsxLr'></legend><bdo id='UsxLr'><pre id='UsxLr'><center id='UsxLr'></center></pre></bdo></b><th id='UsxLr'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='UsxLr'><tfoot id='UsxLr'></tfoot><dl id='UsxLr'><fieldset id='UsxLr'></fieldset></dl></div>

          <bdo id='UsxLr'></bdo><ul id='UsxLr'></ul>

        <tfoot id='UsxLr'></tfoot>

      1. <legend id='UsxLr'><style id='UsxLr'><dir id='UsxLr'><q id='UsxLr'></q></dir></style></legend>
      2. <small id='UsxLr'></small><noframes id='UsxLr'>

        如何在 JavaScript 中將整數(shù)轉(zhuǎn)換為二進(jìn)制?

        How do I convert an integer to binary in JavaScript?(如何在 JavaScript 中將整數(shù)轉(zhuǎn)換為二進(jìn)制?)
          <tbody id='XbEpX'></tbody>
          • <bdo id='XbEpX'></bdo><ul id='XbEpX'></ul>
          • <tfoot id='XbEpX'></tfoot>

                  <small id='XbEpX'></small><noframes id='XbEpX'>

                  <i id='XbEpX'><tr id='XbEpX'><dt id='XbEpX'><q id='XbEpX'><span id='XbEpX'><b id='XbEpX'><form id='XbEpX'><ins id='XbEpX'></ins><ul id='XbEpX'></ul><sub id='XbEpX'></sub></form><legend id='XbEpX'></legend><bdo id='XbEpX'><pre id='XbEpX'><center id='XbEpX'></center></pre></bdo></b><th id='XbEpX'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='XbEpX'><tfoot id='XbEpX'></tfoot><dl id='XbEpX'><fieldset id='XbEpX'></fieldset></dl></div>

                  <legend id='XbEpX'><style id='XbEpX'><dir id='XbEpX'><q id='XbEpX'></q></dir></style></legend>
                  本文介紹了如何在 JavaScript 中將整數(shù)轉(zhuǎn)換為二進(jìn)制?的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

                  問題描述

                  我想查看整數(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)!

                  【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

                  相關(guān)文檔推薦

                  Browser waits for ajax call to complete even after abort has been called (jQuery)(即使在調(diào)用 abort (jQuery) 之后,瀏覽器也會(huì)等待 ajax 調(diào)用完成)
                  JavaScript innerHTML is not working for IE?(JavaScript innerHTML 不適用于 IE?)
                  XMLHttpRequest cannot load, No #39;Access-Control-Allow-Origin#39; header is present on the requested resource(XMLHttpRequest 無法加載,請(qǐng)求的資源上不存在“Access-Control-Allow-Origin標(biāo)頭) - IT屋-程序員軟件開發(fā)技術(shù)分
                  Is it possible for XHR HEAD requests to not follow redirects (301 302)(XHR HEAD 請(qǐng)求是否有可能不遵循重定向 (301 302))
                  NETWORK_ERROR: XMLHttpRequest Exception 101(NETWORK_ERROR:XMLHttpRequest 異常 101)
                  XMLHttpRequest 206 Partial Content(XMLHttpRequest 206 部分內(nèi)容)

                      <bdo id='j53Jv'></bdo><ul id='j53Jv'></ul>
                      <legend id='j53Jv'><style id='j53Jv'><dir id='j53Jv'><q id='j53Jv'></q></dir></style></legend>
                    • <tfoot id='j53Jv'></tfoot>

                        <small id='j53Jv'></small><noframes id='j53Jv'>

                        1. <i id='j53Jv'><tr id='j53Jv'><dt id='j53Jv'><q id='j53Jv'><span id='j53Jv'><b id='j53Jv'><form id='j53Jv'><ins id='j53Jv'></ins><ul id='j53Jv'></ul><sub id='j53Jv'></sub></form><legend id='j53Jv'></legend><bdo id='j53Jv'><pre id='j53Jv'><center id='j53Jv'></center></pre></bdo></b><th id='j53Jv'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='j53Jv'><tfoot id='j53Jv'></tfoot><dl id='j53Jv'><fieldset id='j53Jv'></fieldset></dl></div>
                            <tbody id='j53Jv'></tbody>

                            主站蜘蛛池模板: 成人网av | 精品一二三 | 影视先锋av资源噜噜 | 亚洲精品视频免费观看 | 中文字幕一页二页 | h免费观看 | 日韩网站在线观看 | 视频一区在线 | 亚洲网一区 | 成人免费在线电影 | 国产成都精品91一区二区三 | 日韩精品一区二区三区四区 | 精品久久中文 | 天天天操天天天干 | 日韩成人免费 | 久久国产精品网 | 欧美男人天堂 | 欧美日韩一区不卡 | 精精国产xxxx视频在线播放7 | 免费在线看a | 亚洲精品66 | 亚洲精品片 | 国产中文区二幕区2012 | 欧美视频一区 | 中文字幕电影在线观看 | 欧美精品一区二区在线观看 | 久久这里只有精品首页 | 国产欧美精品一区二区 | 成人精品一区二区三区中文字幕 | 黄色免费在线观看 | 久久另类 | 日韩欧美二区 | 国产精品久久久久久久白浊 | 亚洲福利在线观看 | 91精品中文字幕一区二区三区 | 成人激情视频免费观看 | 亚洲二区视频 | 亚洲网站在线观看 | 99在线国产 | 日韩久久久久 | 国产高清在线观看 |