导航


HTML

CSS

JavaScript

浏览器 & 网络

版本管理

框架

构建工具

TypeScript

性能优化

软实力

算法

UI、组件库

Node

冷门技能

认识位运算

计算机当中存储的信息单位 - 比特和字节

比特 bit(b)

8位:00000001

字节 byte(B)

1B = 8b

00000001 = 一个字节

机器数

定义:整数在计算机中存储的方式

整数在计算机中是以二进制表示

第一位是符号位:0 为正号,1 为负号

10: === 二进制 ===> 1010 === 正数 ===> 01010

-10: === 二进制 ===> 1010 === 负数 ===> 11010

三种形式 - 原码、反码、补码

选择位数:8位

整数 原码 反码 补码
1 00000001 00000001 00000001
-1 10000001 11111110 11111111
5 00000101 00000101 00000101
-5 10000101 11111010 11111011
6 00000110 00000110 00000110
-6 10000110 11111001 11111010

JS 中 1 + (-1) 的计算

(+1) + (-1) = 0

 1 = 00000000000000000000000000000001
-1 = 11111111111111111111111111111111 +
		 --------------------------------
	   00000000000000000000000000000000 // = 0 (相加后全是0)

& 按位与 - 对每一位进行AND操作

规则

举例

9 & 14

 9(base 10) =	00000000000000000000000000001001 (base 2)
14(base 10) = 00000000000000000000000000001110 (base 2)
							--------------------------------
							00000000000000000000000000001000 (base 2) = 1 * 2 ^ 3 = 8 (base 10)

注意:算出来的是补码,如果第一位是0,就可以当做原码用,是1则代表负数,需要回退到反码最后转原码

总结

9 & 0

 9(base 10) =	00000000000000000000000000001001 (base 2)
 0(base 10) = 00000000000000000000000000000000 (base 2)
							--------------------------------
							00000000000000000000000000000000 (base 2) = 0 (base 10)
9 & -1

 9(base 10) =	00000000000000000000000000001001 (base 2)
-1(base 10) = 11111111111111111111111111111111 (base 2)
							--------------------------------
							00000000000000000000000000001001 (base 2) = 9 (base 10)

实际应用

⚪️ 判断一个数的奇偶

1(base 10) = 00000000000000000000000000000001 (base 2)
2(base 10) = 00000000000000000000000000000010 (base 2)
3(base 10) = 00000000000000000000000000000011 (base 2)
5(base 10) = 00000000000000000000000000000101 (base 2)
7(base 10) = 00000000000000000000000000000111 (base 2)
9(base 10) = 00000000000000000000000000001001 (base 2)

得出结论:奇数的最后一位必然是1

3 & 1

 3(base 10) =	00000000000000000000000000000011 (base 2)
 1(base 10) = 00000000000000000000000000000001 (base 2)
							--------------------------------
							00000000000000000000000000000001 (base 2) = 1 (base 10)

// 判断是否是奇数
function isOdd(val) {
  // true: 奇数
 	// false: 偶数
	return (val & 1) === 1;
}

isOdd(7);

| 按位或 - 对每一位进行OR操作

规则

举例

9 | 14

 9(base 10) =	00000000000000000000000000001001 (base 2)
14(base 10) = 00000000000000000000000000001110 (base 2)
							--------------------------------
							00000000000000000000000000001111 (base 2)
              = 1*2^3+1*2^2+1*2^1+1*2^0 = 15 (base 10)

总结

9 | 0

 9(base 10) =	00000000000000000000000000001001 (base 2)
 0(base 10) = 00000000000000000000000000000000 (base 2)
							--------------------------------
							00000000000000000000000000001001 (base 2) = 9 (base 10)
9 | -1

 9(base 10) =	00000000000000000000000000001001 (base 2)
-1(base 10) = 11111111111111111111111111111111 (base 2)
							--------------------------------
							11111111111111111111111111111111 (base 2) = -1 (base 10)

^ 按位异或 - 对每一位进行 XOR 操作

规则:两两相加

9 ^ 14

 9(base 10) =	00000000000000000000000000001001 (base 2)
14(base 10) = 00000000000000000000000000001110 (base 2)
							--------------------------------
							00000000000000000000000000000111 (base 2) = 4 + 2 + 1 = 7 (base 10)

总结

9 ^ 0

 9(base 10) =	00000000000000000000000000001001 (base 2)
 0(base 10) = 00000000000000000000000000000000 (base 2)
							--------------------------------
							00000000000000000000000000001001 (base 2) = 9 (base 10)
9 ^ -1

 9(base 10) =	00000000000000000000000000001001 (base 2)
-1(base 10) = 11111111111111111111111111111111 (base 2)
							--------------------------------
							11111111111111111111111111110110 (base 2) = -10 (base 10)

11111111111111111111111111110110 这是个补码,得转成原码得,就得-1,也就是加上-1
11111111111111111111111111111111
================================
11111111111111111111111111110101 这是反码,转反码得
10000000000000000000000000001010 这是原码 = 1*2^3+1*2^1=10,加上负号 = -10

~ 按位非 - 对每一位进行 NOT 操作

规则:

视频说的 ~a是a的反码?

~a = -(a + 1)

整数 原码 反码 补码
1 00000001 00000001 00000001
-1 10000001 11111110 11111111
5 00000101 00000101 00000101
-5 10000101 11111010 11111011
6 00000110 00000110 00000110
-6 10000110 11111001 11111010
9 和 ~9

 9(base 10) =	00000000000000000000000000001001 (base 2)
					    --------------------------------
~9(base 10) = 11111111111111111111111111110110 (base 2) = -10 (base 10)

总结

~a = -(a + 1)

实际应用

const str = 'boy';
const searchFor = 'e';

if (~str.indexOf(searchFor)) {
	console.log('包含');
} else {
  // 不包含的时候 indexOf = -1
  // 根据 ~a = -(a + 1)
  // 所以 ~(-1) = -(-1 + 1) = 0
  // 也就是不包含的情况
	console.log('不包含');
}

按位移动操作符

<< 左移

公式

m左移n位:m * 2 ^ n

左移2位

 		 9 (base 10) =	00000000000000000000000000001001 (base 2)
					    --------------------------------
9 << 2 (base 10) =  00000000000000000000000000100100 (base 2) = 36 (base 10)

// 9 * 2 ^ 2 = 36