前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >揭秘 JavaScript 位运算符:7个实用的用法

揭秘 JavaScript 位运算符:7个实用的用法

作者头像
前端达人
发布2024-06-26 09:56:04
1670
发布2024-06-26 09:56:04
举报
文章被收录于专栏:前端达人前端达人
JavaScript与许多其他编程语言不同,JavaScript 没有定义不同类型的数字,如整数、短整型、长整型、浮点型等。

整数精度(不带小数点或指数表示法)最多为 15 位。小数精度的最大位数为 17 位,但浮点运算并不总是 100% 准确。

位运算直接计算二进制位,位运算直接处理每个位。它是一种非常低级的操作。优点是速度极快,但缺点是非常不直观,在很多场合不能使用。

位运算只对整数起作用。如果操作数不是整数,则在运行前会自动转换为整数。

在JavaScript内部,值是以64位浮点数的形式存储的,但是进行位运算时,是以32位有符号整数进行运算的,返回值也是32位有符号整数。

JS中常用的7个位运算符

1.按位与(AND)&

&将二进制数中相应的位按照特定的方式组合并运算,如果相应位全为1,结果为1,如果任意位为0,结果为0。

代码语言:javascript
复制
// 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。

代码语言:javascript
复制
// 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。

代码语言:javascript
复制
// 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,也就是求二进制的补码。

代码语言:javascript
复制
// 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填充低位,即把所有数按二进制形式向左移动相应的位数,去掉高位(丢弃),去掉低位。

空白处用零填充。

代码语言:javascript
复制
// 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. 有符号右移>>

>> 此运算符将指定操作数的位向右移动指定的位数。向右移出的位将被丢弃,最左边的位将被复制以填充左侧。由于新的最左边的位始终与之前相同,因此符号位不会改变。这就是为什么它被称为“符号通信”。

代码语言:javascript
复制
// 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).使用&运算符判断数字的奇偶性

代码语言:javascript
复制
// even & 1 = 0
// odd & 1 = 1
console.log(2 & 1) // 0
console.log(3 & 1) // 1

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

代码语言:javascript
复制
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).使用 ^ 完成值交换

代码语言:javascript
复制
var a = 5
var b = 8
a ^= b
b ^= a
a ^= b
console.log(a)   // 8
console.log(b)   // 5

4).使用&、>>、|完成rgb值与十六进制颜色值之间的转换

代码语言:javascript
复制
/**
  * 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'

总结

以上就是我今天与你分享的全部内容,希望今天的内容对你有所帮助。

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2024-06-17,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 前端达人 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档