js 位运算符 以及 心算十进制二进制相互转换

位运算符

|(或) 运算

有1即为1,双0才为0

 

1 31| 2 = 11111 | 10 = 11111 = 3
2 3 | 4 = 11 | 100 = 111 = 7
3 6 | 7 = 110 | 111 = 111 = 7
4 10 | 9 = 1010 | 1001 = 1011 = 11

 

&(与) 运算

有0即为0,双1才为1

1 31 & 2 = 11111 & 10 = 00010 = 2
2 3 & 4 = 11 & 100 = 000 = 0
3 6 & 7 = 110 & 111 = 110 = 6
4 10 & 9 = 1010 & 1001 = 1000 = 8

 

^(异或) 运算

不等为即为1,相等为0

1 31 ^ 2 = 11111 & 10 = 11101 = 29
2 3 & 4 = 11 & 100 = 111 = 7
3 6 & 7 = 110 & 111 = 001 = 1
4 10 & 9 = 1010 & 1001 = 0011 = 3

 

<<(左移位) 运算

移几位,右侧补几个0

1 31 << 2 = 11111 + 00 = 1111100 = 124
2 3 << 4 = 11 + 0000 = 110000 = 48
3 6 << 3 = 110 + 000 = 110000 = 48
4 10 << 4 = 1010 + 0000 = 10100000 = 160

 

>>(右移位) 运算

移几位,左侧补几个0,右侧删除相应位数

1 31 >> 2 = 00 + 11111 = 00111 = 7
2 60 >> 4 = 0000 + 111100 = 000011 = 3
3 27 >> 3 = 000 + 11011 = 00011 = 3
4 100 >> 4 = 0000 + 1100100 = 0000110 = 6

 

 

心算十进制二进制相互转换

例如想要知道十进制108在二进制下是多少

老方法:

新方法:

  Code回忆录原创技术文章,转载请注明出处:https://www.cnblogs.com/bensun/p/10214997.html

posted @ 2019-01-03 17:53  Bensun  阅读(667)  评论(0编辑  收藏  举报