3.5.2 bitwise operator (study notes)

3.5.2 bitwise operator

18 32-bit numerical representation: 00000000000000000000000000010010

Wherein the first 31 bits represent an integer value, the sign of the value represented by 32-bit, 0 for positive, 1 for negative.

31 in each bit represents a power of two. The first represents 0 , represents the second 1 , and so on.

img

Similarly a negative number in binary code storage, but using twos complement. Need to go through the following three steps:

  • The absolute values ​​find binary code
  • Seeking the binary one (replace 0 to 1, 0 to replace 1)
  • The resulting binary one + 1

18 in binary code requirements:

1, seeking binary code 18:

0000 0000 0000 0000 0000 0000 0001 0010

2, the 0 and 1 interchanged

1111 1111 1111 1111 1111 1111 1110 1101

3, a binary one + 1

1111 1111 1111 1111 1111 1111 1110 1101
+                                     1
---------------------------------------
1111 1111 1111 1111 1111 1111 1110 1110

Bitwise not (NOT)

- said anti-return code values.

const num = 25;
num.toString(2); // '11001' <-- 十进制转二进制
// 00000000000000000000000000011001
const num2 = ~num;
console.log(num2); // -26,操作数的负数 -1 
// 等同于
const num3 = -num - 1;

Bitwise operation is performed in the bottom value indicated, and therefore faster.

Bitwise AND (AND)

& It represents, each aligned with one of two values, according to the following rules in the table, two numbers of the same position on the AND operation.

A bit value Bit of the second value result
1 1 1
1 0 0
0 1 0
0 0 0

1 and 4 bits for the operation.

1 & 4; // 0
// 1 的二进制位
0000 0000 0000 0000 0000 0000 0000 0001
// 4 的二进制位
0000 0000 0000 0000 0000 0000 0000 0100
// AND --------------------------------
0000 0000 0000 0000 0000 0000 0000 0000

Bitwise OR (OR)

| Representation.

A bit value Bit of the second value result
1 1 1
1 0 1
0 1 1
0 0 0

1 and 4 bits for the operation or.

1 | 4; // 5
// 1 的二进制位
0000 0000 0000 0000 0000 0000 0000 0001
// 4 的二进制位
0000 0000 0000 0000 0000 0000 0000 0100
// OR --------------------------------
0000 0000 0000 0000 0000 0000 0000 0101
parseInt('00000000000000000000000000000101', 2); // 转十进制,5

Bitwise exclusive OR (XOR)

^ Representation.

A bit value Bit of the second value result
1 1 0
1 0 1
0 1 1
0 0 0

Two bit value, only a 1, returns 1.

1 and 4 bits of the XOR operation.

1 ^ 4; // 5
// 1 的二进制位
0000 0000 0000 0000 0000 0000 0000 0001
// 4 的二进制位
0000 0000 0000 0000 0000 0000 0000 0100
// XOR --------------------------------
0000 0000 0000 0000 0000 0000 0000 0101
parseInt('00000000000000000000000000000101', 2); // 转十进制,5

Of 4 and 4-bit XOR operation.

4 ^ 4; // 0
// 4 的二进制位
0000 0000 0000 0000 0000 0000 0000 0100
// 4 的二进制位
0000 0000 0000 0000 0000 0000 0000 0100
// XOR --------------------------------
0000 0000 0000 0000 0000 0000 0000 0000
parseInt('00000000000000000000000000000000', 2); // 转十进制,0

Left (<<)

The value of all the bits to the left moves the specified number of digits.

const val = 2; // 二进制:10
const val2 = val << 3; // 二进制:10000 十进制:16

Signed right shift (>>)

const val = 16; // 二进制:10000
const val2 = val >> 3; // 二进制:10 十进制:2

Unsigned right shift (>>>)

// -16 的二进制码
11111111111111111111111111110000
// -16 >>> 3
00011111111111111111111111111110 // 右移后 以 0 填充左侧的位
parseInt('00011111111111111111111111111110', 2); // 十进制:536870910

Guess you like

Origin www.cnblogs.com/lwl0812/p/11224899.html