导航
8位:00000001
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 |
(+1) + (-1) = 0
1 = 00000000000000000000000000000001
-1 = 11111111111111111111111111111111 +
--------------------------------
00000000000000000000000000000000 // = 0 (相加后全是0)
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);
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)
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
视频说的 ~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