An operation

Bitwise operations in JavaScript treat operands as 32-bit binary strings that return standard JavaScript values.

In JavaScript, values are stored as 64-bit floating-point numbers, but are converted to 32-bit signed integers when two integers are bitwise computed.

So numbers are finite in computers, and they’re finite in JavaScript.

So there’s a notion of a safe number. The so-called safe number is in this range there is a positive and negative one-to-one correspondence.

The safe Number for JavaScript in ES6 is number.max_safe_INTEGER.

But bitwise operations only work on 32-bit integers. The floating-point decimal will be converted to a 32-bit binary string before calculation.

An operator

Operator Usage An operator
The bitwise and ADD a & b If the corresponding bits of both a and B are 1, return 1; otherwise, return 0
Bitwise OR OR a b
Bitwise XOR a ^ b If the corresponding bits of a and B are the same, return 1; otherwise, return 0
Bitwise NOT NOT ~a I’m going to invert each of the terms of A
The left shift a << b Move the binary string of A to the left by b bits and fill the right with zeros
Arithmetic moves to the right a >> b Move the binary string of A b bits to the right, discarding any bits removed
Unsigned shift to the right a >>> b Move the binary string of A b bits to the right, discarding all the bits removed, and filling the bits left empty with zeros

Bitwise operation technique

Move left & move right

Moving to the left is thought to be the same as multiplying by two, and moving to the right by one is the same as dividing by two and rounding down (not rounding zero).

let a = 2
console.log(a << 2) // 8 = 2 * 2 * 2

console.log(a >> 2) // 0 = Math.floor(2 / 2 / 2)
Copy the code

Exclusive or

Xor operations can be understood in two ways

  1. Equal to 1, different to 0
  2. We don’t add digits

The xOR operation satisfies commutative law and binding rate

0 xor any number is itself, and a number xor is itself 0

let a = 8
console.log(a ^ a) / / 0
console.log(a ^ 0) / / 8
Copy the code

It is also possible to swap values without using extra space by xOR

let a = 8
let b = 7
a = a ^ b
b = a ^ b
a = a ^ b

console.log(a, b) / / 7, 8
Copy the code

However, there is a limitation to this method. The memory addresses of a and B must be different, otherwise 0 will be returned

Let’s take the 1 on the far right


let a = 9
let rightOne = a & (~a + 1)
console.log(parseInt(a).toString(2), parseInt(rightOne).toString(2)); / / 1 1001
Copy the code

Gets a bit of a number’s binary

// Get the BTH bit of a, with the least significant being 0
function getBit(a, b) {
    return (a >> b) & 1
}
console.log(parseInt(getBit(15.2)).toString(2)); / / 1
Copy the code

Sets one bit of a binary number to 0

// Set the b bit of a to 0
function unsetBit(a, b) {
    return a & ~(1 << b)
}
Copy the code

Sets one bit of a binary number to 1

// Set the b bit of a to 1 and the lowest bit of a to 0
function setBit(a, b) {
    return a | (1 << b)
}
Copy the code

Invert one bit of a binary

// select * from a; // select * from a
function flapBit(a, b) {
    return a ^ (1 << b)
}
Copy the code