Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

The use of bit operators in JS


May 30, 2021 Article blog


Table of contents


Bit by Bit and (AND)

Convert the numbers into binary, then work with them, and then back to decimal

  1. // 1 的二进制表示为 00000000 00000000 00000000 00000001
  2. // 3 的二进制表示为 00000000 00000000 00000000 00000011
  3. // --------------------------------------------------
  4. // 1 的二进制表示为 00000000 00000000 00000000 00000001
  5. console.log(1 & 3) // 1

| by bit or (OR).

Convert the number to binary, then do or operate, and then back to decimal

  1. // 1 的二进制表示为 00000000 00000000 00000000 00000001
  2. // 3 的二进制表示为 00000000 00000000 00000000 00000011
  3. // --------------------------------------------------
  4. // 3 的二进制表示为 00000000 00000000 00000000 00000011
  5. console.log(1 | 3) // 3

By bit or (XOR).

Convert the number to binary, then make a different or operation, and then convert it back to decimal

  1. // 1 的二进制表示为 00000000 00000000 00000000 00000001
  2. // 3 的二进制表示为 00000000 00000000 00000000 00000011
  3. // --------------------------------------------------
  4. // 2 的二进制表示为 00000000 00000000 00000000 00000010
  5. console.log(1 ^ 3) // 2

Bit-by-bit non-(NOT) to

Convert the number to binary, then do the non-operation, and then convert back to the decimal, which is also the binary anticode

  1. // 1 反码二进制表示为 11111111 11111111 11111111 11111110
  2. // 由于第一位(符号位)是1,所以这个数是负数。JavaScript 内部采用补码形式表示负数,即需要将这个数减去 1,再去一次反,然后加上负号才能得到这个负数对应的十进制数值
  3. // 1 的反码减一表示为 11111111 11111111 11111111 11111101
  4. // 取反             00000000 00000000 00000000 00000010
  5. // 表示为 -2
  6. console.log(~1) // -2

Left shift <<

Convert the number to binary, then discard the high and make up 0 low

  1. // 1 的二进制表示为 00000000 00000000 00000000 00000001
  2. // 2 的二进制表示为 00000000 00000000 00000000 00000010
  3. console.log(1 << 1) // 2

The symbol moves right >>

Turn the number into binary, then discard the low bit and copy the leftmost bit to fill the left

  1. // 1 的二进制表示为 00000000 00000000 00000000 00000001
  2. // 0 的二进制表示为 00000000 00000000 00000000 00000000
  3. console.log(1 >> 1) // 0

Unsigned move right >>>

Turn the number into binary, then discard the low bit and make up 0 on the left, so it is always non-negative.

For non-negative numbers, the results of symbolic right-shifting and unsigned right-shifting are always equal.

The use of bit operators in JS

Judge the odd couple

  1. // 偶数 & 1 = 0
  2. // 奇数 & 1 = 1
  3. console.log(2 & 1) // 0
  4. console.log(3 & 1) // 1

Round up

  1. console.log(~~6.83// 6
  2. console.log(6.83 >> 0// 6
  3. console.log(6.83 << 0// 6
  4. console.log(6.83 | 0// 6
  5. // 不可对负数取整
  6. console.log(6.83 >>> 0) /6

Swap values

  1. var a = 6
  2. var b = 8
  3. a ^= b
  4. b ^= a
  5. a ^= b
  6. console.log(a) // 8
  7. console.log(b) // 6

The RGB value and the 16-in color value are converted

  1. function hexToRGB(hex: string): string{
  2.   const hexx = hex.replace('#''0x')
  3.   const r = hexx >> 16
  4.   const g = hexx >> 8 & 0xff
  5.   const b = hexx & 0xff
  6.   return `rgb(${r}${g}${b})`
  7. }
  8. function RGBToHex(rgb: string): string{
  9.   const rgbArr = rgb.split(/[^\d]+/)
  10.   const color = rgbArr[1] | rgbArr[2] << 8 | rgbArr[3]
  11.   return `#${color.toString(16)}`
  12. }

Recommended lessons:

Little White Front End: JavaScript Zero Basic Getting Started to Advanced (2020 Edition)

JavaScript Live: Dynamic website development

Javascript mobile app live development