AVt天堂网 手机版,亚洲va久久久噜噜噜久久4399,天天综合亚洲色在线精品,亚洲一级Av无码毛片久久精品

當(dāng)前位置:首頁 > 科技  > 軟件

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

來源: 責(zé)編: 時間:2024-06-12 08:46:55 161觀看
導(dǎo)讀JavaScript與許多其他編程語言不同,JavaScript 沒有定義不同類型的數(shù)字,如整數(shù)、短整型、長整型、浮點(diǎn)型等。整數(shù)精度(不帶小數(shù)點(diǎn)或指數(shù)表示法)最多為 15 位。小數(shù)精度的最大位數(shù)為 17 位,但浮點(diǎn)運(yùn)算并不總是 100% 準(zhǔn)確。

JavaScript與許多其他編程語言不同,JavaScript 沒有定義不同類型的數(shù)字,如整數(shù)、短整型、長整型、浮點(diǎn)型等。HEp28資訊網(wǎng)——每日最新資訊28at.com

HEp28資訊網(wǎng)——每日最新資訊28at.com

整數(shù)精度(不帶小數(shù)點(diǎn)或指數(shù)表示法)最多為 15 位。小數(shù)精度的最大位數(shù)為 17 位,但浮點(diǎn)運(yùn)算并不總是 100% 準(zhǔn)確。HEp28資訊網(wǎng)——每日最新資訊28at.com

位運(yùn)算直接計(jì)算二進(jìn)制位,位運(yùn)算直接處理每個位。它是一種非常低級的操作。優(yōu)點(diǎn)是速度極快,但缺點(diǎn)是非常不直觀,在很多場合不能使用。HEp28資訊網(wǎng)——每日最新資訊28at.com

位運(yùn)算只對整數(shù)起作用。如果操作數(shù)不是整數(shù),則在運(yùn)行前會自動轉(zhuǎn)換為整數(shù)。HEp28資訊網(wǎng)——每日最新資訊28at.com

在JavaScript內(nèi)部,值是以64位浮點(diǎn)數(shù)的形式存儲的,但是進(jìn)行位運(yùn)算時,是以32位有符號整數(shù)進(jìn)行運(yùn)算的,返回值也是32位有符號整數(shù)。HEp28資訊網(wǎng)——每日最新資訊28at.com

JS中常用的7個位運(yùn)算符HEp28資訊網(wǎng)——每日最新資訊28at.com

1.按位與(AND)&

&將二進(jìn)制數(shù)中相應(yīng)的位按照特定的方式組合并運(yùn)算,如果相應(yīng)位全為1,結(jié)果為1,如果任意位為0,結(jié)果為0。HEp28資訊網(wǎng)——每日最新資訊28at.com

// 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ū)別在于,若任意一個操作數(shù)在相應(yīng)位為1,則結(jié)果為1。HEp28資訊網(wǎng)——每日最新資訊28at.com

// 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)^

^如果兩個操作數(shù)位對應(yīng)只有一個1,則結(jié)果為1,其他都為0。HEp28資訊網(wǎng)——每日最新資訊28at.com

// 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ǔ)碼。HEp28資訊網(wǎng)——每日最新資訊28at.com

// 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

簡單記憶:一個數(shù)和它自身的取反值相加等于-1。HEp28資訊網(wǎng)——每日最新資訊28at.com

5.左移<<

<<運(yùn)算符將指定值的二進(jìn)制數(shù)的所有位向左移動指定的次數(shù)。HEp28資訊網(wǎng)——每日最新資訊28at.com

移動規(guī)則:丟棄高位,用0填充低位,即把所有數(shù)按二進(jìn)制形式向左移動相應(yīng)的位數(shù),去掉高位(丟棄),去掉低位。HEp28資訊網(wǎng)——每日最新資訊28at.com

空白處用零填充。HEp28資訊網(wǎng)——每日最新資訊28at.com

// 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. 有符號右移>>

>> 此運(yùn)算符將指定操作數(shù)的位向右移動指定的位數(shù)。向右移出的位將被丟棄,最左邊的位將被復(fù)制以填充左側(cè)。由于新的最左邊的位始終與之前相同,因此符號位不會改變。這就是為什么它被稱為“符號通信”。HEp28資訊網(wǎng)——每日最新資訊28at.com

// 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. 無符號右移>>>

>>> 該運(yùn)算符將第一個操作數(shù)向右移動指定的位數(shù)。向右移動的位被丟棄,左側(cè)用0填充。由于符號位變?yōu)?,因此,結(jié)果始終為非負(fù)數(shù)。(譯注:即使向右移動0位,結(jié)果也是非負(fù)數(shù)。)HEp28資訊網(wǎng)——每日最新資訊28at.com

對于非負(fù)數(shù),有符號和無符號右移總是返回相同的結(jié)果。例如,9 >>> 2 得到 2 和 9 >> 2 相同。HEp28資訊網(wǎng)——每日最新資訊28at.com

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

1).使用&運(yùn)算符判斷數(shù)字的奇偶性HEp28資訊網(wǎng)——每日最新資訊28at.com

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

2).使用 ~, >>, <<, >>>, | 來舍入HEp28資訊網(wǎng)——每日最新資訊28at.com

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).使用 ^ 完成值交換HEp28資訊網(wǎng)——每日最新資訊28at.com

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

4).使用&、>>、|完成rgb值與十六進(jìn)制顏色值之間的轉(zhuǎn)換HEp28資訊網(wǎng)——每日最新資訊28at.com

/**  * 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)容對你有所幫助。HEp28資訊網(wǎng)——每日最新資訊28at.com

最后,感謝你的閱讀,祝編程愉快!HEp28資訊網(wǎng)——每日最新資訊28at.com

本文鏈接:http://www.tebozhan.com/showinfo-26-93211-0.html七種 JavaScript 中位運(yùn)算符的神奇用法

聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權(quán)等問題請及時與本網(wǎng)聯(lián)系,我們將在第一時間刪除處理。郵件:2376512515@qq.com

上一篇: 掌握J(rèn)ava函數(shù)式接口,輕松實(shí)現(xiàn)依賴反轉(zhuǎn)

下一篇: 機(jī)械師 10 周年:攜全球首款“光學(xué)大師”G5PRO V2 光學(xué)手柄現(xiàn)身鄭州

標(biāo)簽:
  • 熱門焦點(diǎn)
  • Redmi Buds 4開箱簡評:才199還有降噪 可以無腦入

    在上個月舉辦的Redmi Note11T Pro系列新機(jī)發(fā)布會上,除了兩款手機(jī)新品之外,Redmi還帶來了兩款TWS真無線藍(lán)牙耳機(jī)產(chǎn)品,Redmi Buds 4和Redmi Buds 4 Pro,此前我們在Redmi Note11T
  • 7月安卓手機(jī)性價比榜:努比亞+紅魔兩款新機(jī)入榜

    7月登場的新機(jī)有努比亞Z50S Pro和紅魔8S Pro,除了三星之外目前唯二的兩款搭載超頻版驍龍8Gen2處理器的產(chǎn)品,而且努比亞和紅魔也一貫有著不錯的性價比,所以在本次的性價比榜單
  • 6月安卓手機(jī)好評榜:魅族20 Pro蟬聯(lián)冠軍

    性能榜和性價比榜之后,我們來看最后的安卓手機(jī)好評榜,數(shù)據(jù)來源安兔兔評測,收集時間2023年6月1日至6月30日,僅限國內(nèi)市場。第一名:魅族20 Pro好評率:95%5月份的時候魅族20 Pro就是
  • 一文看懂為蘋果Vision Pro開發(fā)應(yīng)用程序

    譯者 | 布加迪審校 | 重樓蘋果的Vision Pro是一款混合現(xiàn)實(shí)(MR)頭戴設(shè)備。Vision Pro結(jié)合了虛擬現(xiàn)實(shí)(VR)和增強(qiáng)現(xiàn)實(shí)(AR)的沉浸感。其高分辨率顯示屏、先進(jìn)的傳感器和強(qiáng)大的處理能力
  • 服務(wù)存儲設(shè)計(jì)模式:Cache-Aside模式

    Cache-Aside模式一種常用的緩存方式,通常是把數(shù)據(jù)從主存儲加載到KV緩存中,加速后續(xù)的訪問。在存在重復(fù)度的場景,Cache-Aside可以提升服務(wù)性能,降低底層存儲的壓力,缺點(diǎn)是緩存和底
  • 從 Pulsar Client 的原理到它的監(jiān)控面板

    背景前段時間業(yè)務(wù)團(tuán)隊(duì)偶爾會碰到一些 Pulsar 使用的問題,比如消息阻塞不消費(fèi)了、生產(chǎn)者消息發(fā)送緩慢等各種問題。雖然我們有個監(jiān)控頁面可以根據(jù) topic 維度查看他的發(fā)送狀態(tài),
  • 分布式系統(tǒng)中的CAP理論,面試必問,你理解了嘛?

    對于剛剛接觸分布式系統(tǒng)的小伙伴們來說,一提起分布式系統(tǒng),就感覺高大上,深不可測。而且看了很多書和視頻還是一臉懵逼。這篇文章主要使用大白話的方式,帶你理解一下分布式系統(tǒng)
  • 梁柱接棒兩年,騰訊音樂闖出新路子

    文丨田靜 出品丨牛刀財經(jīng)(niudaocaijing)7月5日,企鵝FM發(fā)布官方公告稱由于業(yè)務(wù)調(diào)整,將于9月6日正式停止運(yùn)營,這意味著騰訊音樂長音頻業(yè)務(wù)走向消亡。騰訊在長音頻領(lǐng)域還在摸索。為
  • 最薄的14英寸游戲筆記本電腦 Alienware X14已可以購買

    2022年1月份在國際消費(fèi)電子展(CES2022)上首次亮相的Alienware新品——Alienware X14現(xiàn)在已經(jīng)可以購買了,這款筆記本電腦被譽(yù)為世界上最薄的 14 英寸游戲筆
Top