成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

七種 JavaScript 中位運算符的神奇用法

開發 前端
JavaScript與許多其他編程語言不同,JavaScript 沒有定義不同類型的數字,如整數、短整型、長整型、浮點型等。

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

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

位運算直接計算二進制位,位運算直接處理每個位。它是一種非常低級的操作。優點是速度極快,但缺點是非常不直觀,在很多場合不能使用。

位運算只對整數起作用。如果操作數不是整數,則在運行前會自動轉換為整數。

在JavaScript內部,值是以64位浮點數的形式存儲的,但是進行位運算時,是以32位有符號整數進行運算的,返回值也是32位有符號整數。

JS中常用的7個位運算符

1.按位與(AND)&

&將二進制數中相應的位按照特定的方式組合并運算,如果相應位全為1,結果為1,如果任意位為0,結果為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 00000001
console.log(1 & 3) // 1

2. 按位或(OR)|

| 該運算符與&的區別在于,若任意一個操作數在相應位為1,則結果為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 00000011
console.log(1 | 3) // 3

3. 按位異或(XOR)^

^如果兩個操作數位對應只有一個1,則結果為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 00000010
console.log(1^3) // 2

4. 按位非(NOT)~

~ 該運算符是將位取反,1變成0,0變成1,也就是求二進制的補碼。

// 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: -2
console.log(~ 1) // -2

簡單記憶:一個數和它自身的取反值相加等于-1。

5.左移<<

<<運算符將指定值的二進制數的所有位向左移動指定的次數。

移動規則:丟棄高位,用0填充低位,即把所有數按二進制形式向左移動相應的位數,去掉高位(丟棄),去掉低位。

空白處用零填充。

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

6. 有符號右移>>

>> 此運算符將指定操作數的位向右移動指定的位數。向右移出的位將被丟棄,最左邊的位將被復制以填充左側。由于新的最左邊的位始終與之前相同,因此符號位不會改變。這就是為什么它被稱為“符號通信”。

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

7. 無符號右移>>>

>>> 該運算符將第一個操作數向右移動指定的位數。向右移動的位被丟棄,左側用0填充。由于符號位變為0,因此,結果始終為非負數。(譯注:即使向右移動0位,結果也是非負數。)

對于非負數,有符號和無符號右移總是返回相同的結果。例如,9 >>> 2 得到 2 和 9 >> 2 相同。

js中位運算符的妙用

1).使用&運算符判斷數字的奇偶性

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

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

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

3).使用 ^ 完成值交換

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

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

/**
  * 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}, $)`
   }


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

總結

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

最后,感謝你的閱讀,祝編程愉快!

責任編輯:華軒 來源: web前端開發
相關推薦

2022-05-10 08:08:01

find命令Linux

2021-05-07 06:27:29

JavaScript運算符開發

2013-01-07 10:14:06

JavaJava枚舉

2010-10-08 09:17:07

JavaScript表JavaScript運

2025-01-15 10:46:23

開發JavaScript集合

2017-06-14 16:44:15

JavaScript原型模式對象

2022-09-07 00:04:37

JavaScript運算符技巧

2025-01-16 08:44:55

2010-07-20 14:52:51

Perl語法

2025-01-24 08:32:00

運算符C#

2017-05-11 16:38:07

javascript邏輯運算符

2021-02-20 23:34:22

JavaScript運算符開發

2020-03-27 22:33:30

JavaScript運算符逗號

2020-06-04 08:13:36

JavaScriptObject.is()運算符

2020-06-18 10:26:43

JavaScript開發技術

2010-07-14 17:43:25

Perl運算符

2025-02-24 11:16:20

2022-05-18 09:01:31

JavaScriptEvalErrorURIError

2025-05-13 08:20:58

2021-07-12 15:35:56

JavaScript代碼運算符
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 欧美激情亚洲 | 久久综合久色欧美综合狠狠 | 久久久精品影院 | 97精品超碰一区二区三区 | 嫩草视频在线 | 色综合九九 | 国产精品免费一区二区三区四区 | 国产精品国产三级国产aⅴ中文 | 久久国际精品 | yeyeav| 99精品视频一区二区三区 | 日韩三级在线 | www.亚洲精品| 黄色免费在线观看网址 | www.久| 亚洲久久久 | 亚洲精品在线视频 | 中文字幕亚洲无线 | 亚洲一区二区av | 日韩精品人成在线播放 | 7777在线视频 | 亚洲精品国产电影 | 一区二区三区免费 | 成年人视频在线免费观看 | 国产精品99久久久精品免费观看 | 色吧综合网 | 小h片免费观看久久久久 | 欧美在线综合 | 久久青 | 天天综合网91 | 99re在线视频 | 成人精品一区亚洲午夜久久久 | 日韩精品一区二区三区在线观看 | 欧美一区二区三区免费电影 | 精品免费国产视频 | 亚洲精品久久 | 麻豆国产一区二区三区四区 | 久久久国产一区二区三区四区小说 | 日韩欧美在线播放 | 日韩精品 电影一区 亚洲 | 亚洲欧美一区二区三区情侣bbw |