<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          7種JavaScript 中位運(yùn)算符的神奇用法

          共 6442字,需瀏覽 13分鐘

           ·

          2024-08-06 09:15

          點(diǎn)擊上方 前端Q,關(guān)注公眾號(hào)

          回復(fù)加群,加入前端Q技術(shù)交流群

          JavaScript與許多其他編程語言不同,JavaScript 沒有定義不同類型的數(shù)字,如整數(shù)、短整型、長整型、浮點(diǎn)型等。


          整數(shù)精度(不帶小數(shù)點(diǎn)或指數(shù)表示法)最多為 15 位。小數(shù)精度的最大位數(shù)為 17 位,但浮點(diǎn)運(yùn)算并不總是 100% 準(zhǔn)確。

          位運(yùn)算直接計(jì)算二進(jìn)制位,位運(yùn)算直接處理每個(gè)位。它是一種非常低級的操作。優(yōu)點(diǎn)是速度極快,但缺點(diǎn)是非常不直觀,在很多場合不能使用。

          位運(yùn)算只對整數(shù)起作用。如果操作數(shù)不是整數(shù),則在運(yùn)行前會(huì)自動(dòng)轉(zhuǎn)換為整數(shù)。

          在JavaScript內(nèi)部,值是以64位浮點(diǎn)數(shù)的形式存儲(chǔ)的,但是進(jìn)行位運(yùn)算時(shí),是以32位有符號(hào)整數(shù)進(jìn)行運(yùn)算的,返回值也是32位有符號(hào)整數(shù)。

          JS中常用的7個(gè)位運(yùn)算符

          1.按位與(AND)&

          &將二進(jìn)制數(shù)中相應(yīng)的位按照特定的方式組合并運(yùn)算,如果相應(yīng)位全為1,結(jié)果為1,如果任意位為0,結(jié)果為0。

          // The binary representation of 1 is: 00000000 00000000 00000000 00000001// The binary representation of 3 is: 00000000 00000000 00000000 00000011// -----------------------------// The binary representation of 1 is: 00000000 00000000 00000000 00000001console.log(1 & 3) // 1

          2. 按位或(OR)|

          | 該運(yùn)算符與&的區(qū)別在于,若任意一個(gè)操作數(shù)在相應(yīng)位為1,則結(jié)果為1。

          // The binary representation of 1 is: 00000000 00000000 00000000 00000001// The binary representation of 3 is: 00000000 00000000 00000000 00000011// -----------------------------// The binary representation of 3 is: 00000000 00000000 00000000 00000011console.log(1 | 3) // 3

          3. 按位異或(XOR)^

          ^如果兩個(gè)操作數(shù)位對應(yīng)只有一個(gè)1,則結(jié)果為1,其他都為0。

          // The binary representation of 1 is: 00000000 00000000 00000000 00000001// The binary representation of 3 is: 00000000 00000000 00000000 00000011// -----------------------------// The binary representation of 2 is: 00000000 00000000 00000000 00000010console.log(1^3) // 2

          4. 按位非(NOT)~

          ~ 該運(yùn)算符是將位取反,1變成0,0變成1,也就是求二進(jìn)制的補(bǔ)碼。

          // The binary representation of 1 is: 00000000 00000000 00000000 00000001// The binary representation of 3 is: 00000000 00000000 00000000 00000011// -----------------------------// 1's inverse binary representation: 11111111 11111111 11111111 11111110// Since the first bit (sign bit) is 1, this number is a negative number. JavaScript internally uses complement code to represent negative numbers, that is, you need to subtract 1 from this number, take the inverse again, and then add a negative sign to get the decimal value corresponding to the negative number.// -----------------------------// The inverse of 1 minus 1: 11111111 11111111 11111111 11111101// Negative code: 00000000 00000000 00000000 00000010// Represented as decimal plus minus sign: -2console.log(~ 1) // -2

          簡單記憶:一個(gè)數(shù)和它自身的取反值相加等于-1。

          5.左移<<

          <<運(yùn)算符將指定值的二進(jìn)制數(shù)的所有位向左移動(dòng)指定的次數(shù)。

          移動(dòng)規(guī)則:丟棄高位,用0填充低位,即把所有數(shù)按二進(jìn)制形式向左移動(dòng)相應(yīng)的位數(shù),去掉高位(丟棄),去掉低位。

          空白處用零填充。

          // The binary representation of 1 is: 00000000 00000000 00000000 00000001// -----------------------------// The binary representation of 2 is: 00000000 00000000 00000000 00000010console.log(1 << 1) // 2

          6. 有符號(hào)右移>>

          >> 此運(yùn)算符將指定操作數(shù)的位向右移動(dòng)指定的位數(shù)。向右移出的位將被丟棄,最左邊的位將被復(fù)制以填充左側(cè)。由于新的最左邊的位始終與之前相同,因此符號(hào)位不會(huì)改變。這就是為什么它被稱為“符號(hào)通信”。

          // The binary representation of 1 is: 00000000 00000000 00000000 00000001// -----------------------------// The binary representation of 0 is: 00000000 00000000 00000000 00000000console.log(1 >> 1) // 0

          7. 無符號(hào)右移>>>

          >>> 該運(yùn)算符將第一個(gè)操作數(shù)向右移動(dòng)指定的位數(shù)。向右移動(dòng)的位被丟棄,左側(cè)用0填充。由于符號(hào)位變?yōu)?,因此,結(jié)果始終為非負(fù)數(shù)。(譯注:即使向右移動(dòng)0位,結(jié)果也是非負(fù)數(shù)。)

          對于非負(fù)數(shù),有符號(hào)和無符號(hào)右移總是返回相同的結(jié)果。例如,9 >>> 2 得到 2 和 9 >> 2 相同。

          js中位運(yùn)算符的妙用

          1).使用&運(yùn)算符判斷數(shù)字的奇偶性

          // even & 1 = 0// odd & 1 = 1console.log(2 & 1) // 0console.log(3 & 1) // 1

          2).使用 ~, >>, <<, >>>, | 來舍入

          console.log(~~ 6.83) // 6console.log(6.83 >> 0) // 6console.log(6.83 << 0) // 6console.log(6.83 | 0) // 6// >>> cannot round negative numbersconsole.log(6.83 >>> 0) // 6

          3).使用 ^ 完成值交換

          var a = 5var b = 8a ^= bb ^= aa ^= bconsole.log(a)   // 8console.log(b)   // 5

          4).使用&、>>、|完成rgb值與十六進(jìn)制顏色值之間的轉(zhuǎn)換

          /**  * Hexadecimal color value to RGB  * @param {String} hex hexadecimal color string  * @return {String} RGB color string  */   function hexToRGB(hex) {     var hexx = hex. replace('#', '0x')     var r = hexx >> 16     var g = hexx >> 8 & 0xff     var b = hexx & 0xff     return `rgb(${r}, ${g}, ${b})`   }
          /** * RGB color to hexadecimal color * @param {String} rgb RGB color string * @return {String} Hexadecimal color string */ function RGBToHex(rgb) { var rgbArr = rgb. split(/[^\d]+/) var color = rgbArr[1]<<16 | rgbArr[2]<<8 | rgbArr[3] return '#'+ color.toString(16) }// ------------------------------------------------ -hexToRGB('#ffffff') // 'rgb(255,255,255)'RGBToHex('rgb(255,255,255)') // '#ffffff'

          總結(jié)

          以上就是我今天與你分享的全部內(nèi)容,希望今天的內(nèi)容對你有所幫助。


          往期推薦


          threejs做特效:實(shí)現(xiàn)物體的發(fā)光效果-EffectComposer詳解!
          Node.js + typescript 寫一個(gè)命令批處理輔助工具
          源碼視角,Vue3為什么推薦使用ref而不是reactive

          最后


          • 歡迎加我微信,拉你進(jìn)技術(shù)群,長期交流學(xué)習(xí)...

          • 歡迎關(guān)注「前端Q」,認(rèn)真學(xué)前端,做個(gè)專業(yè)的技術(shù)人...

          點(diǎn)個(gè)在看支持我吧

          瀏覽 76
          點(diǎn)贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評論
          圖片
          表情
          推薦
          點(diǎn)贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  国产成人毛片 | 久草视频在线播放 | 激情乱AV| 日本女人牲交视频 | 翔田千里无码播放HD |