JavaScript Bitwise NOT Operator
心血来潮地跑到Upworks做了个JavaScript Test,结果当然是惨不忍睹,发现自己对不少JavaScript的基础知识的理解是模模糊糊,甚至是错的。
比如这题:
~-(2+"2")
这个表达式的值是21,我脑补了好久也得不到这个答案,这才发现,我完全不理解Bitwise NOT操作符。
不懂就补喽。
这回还真是“补”,回想起大学那会儿,学到“补码”时,脑子里就是一团雾,原来那团迷雾到今天也没散,真他么黏糊。
读到 Why is ~5 === -6 in JavaScript?:
It does indeed perform a bit-wise NOT, the negative number is in two's complement. So the value 1010 is -6.
Two's complement basically works by the very left-most bit signifies a negative number and is taken as a negative value. All other 1 bits are added to this number. For example:
1010 => (-8 +0 +2 +0) => -6
1111 => (-8 +4 +2 +1) => -1
又琢磨了一会儿,才搞明白为啥补码这么反直觉。
“二补码”只能脑补,或者用代码打印脑补的内容。
在JavaScript里,如果用number.toString(2)
,结果是这样:
Decimal: 5 | Binary: 00000000000000000000000000000101
Decimal: 4 | Binary: 00000000000000000000000000000100
Decimal: 3 | Binary: 00000000000000000000000000000011
Decimal: 2 | Binary: 00000000000000000000000000000010
Decimal: 1 | Binary: 00000000000000000000000000000001
Decimal: 0 | Binary: 00000000000000000000000000000000
Decimal: -0 | Binary: 00000000000000000000000000000000
Decimal: -1 | Binary: 000000000000000000000000000000-1
Decimal: -2 | Binary: 00000000000000000000000000000-10
Decimal: -3 | Binary: 00000000000000000000000000000-11
Decimal: -4 | Binary: 0000000000000000000000000000-100
Decimal: -5 | Binary: 0000000000000000000000000000-101
这个结果符合直觉,但加法器的实现只会使用二补码,下面是加法器实际使用的“二补码”:
Decimal: 5 | Binary: 00000000000000000000000000000101
Decimal: 4 | Binary: 00000000000000000000000000000100
Decimal: 3 | Binary: 00000000000000000000000000000011
Decimal: 2 | Binary: 00000000000000000000000000000010
Decimal: 1 | Binary: 00000000000000000000000000000001
Decimal: 0 | Binary: 00000000000000000000000000000000
Decimal: -0 | Binary: 00000000000000000000000000000000
Decimal: -1 | Binary: 11111111111111111111111111111111
Decimal: -2 | Binary: 11111111111111111111111111111110
Decimal: -3 | Binary: 11111111111111111111111111111101
Decimal: -4 | Binary: 11111111111111111111111111111100
Decimal: -5 | Binary: 11111111111111111111111111111011
理解了二补码,再来看-22怎么被补成21的:
Decimal: -22 | Binary: 11111111111111111111111111101010
->
Decimal: 21 | Binary: 00000000000000000000000000010101
Python对此解释得更直接:
~ x
Returns the complement of x - the number you get by switching each 1 for a 0 and each 0 for a 1.
This is the same as -x - 1.
我连补码都没闹明白,竟然过了关还Score top 30%,可见其他人都没作弊,我真是无耻的程序员。
写了半天,一言以蔽之:
~x 相当于调用
function twosComplement(x){return 0 -x - 1;
}
参考链接:
- Why is ~5 === -6 in JavaScript?
- Why does bitwise “not 1” equal -2?
- MDN Bitwise operators
- Python's bitwise operators.
- Two's Complement
JavaScript Bitwise NOT Operator相关推荐
- Some Essential JavaScript Questions And Answers(5)
Some Essential JavaScript Questions And Answers Question 9: Discuss possible ways to write a functio ...
- javascript运算符_JavaScript中的按位运算符
javascript运算符 JavaScript按位运算符 (JavaScript Bitwise Operators) A lot of times you come across some str ...
- javascript operators(操作符)
1.Assignment operators(分配操作) Name Shorthand operator Meaning Assignment x = y x = y Addition assignm ...
- vscode前端环境(html+css+javascript)的配置
现在最新版本的vscode已经不需要额外配置这三个语言本身了.已经默认支持很多功能. IntelliJ IDEA Keybindings快捷键插件 习惯用idea的快捷键,根据需要来.这个插件可以将快 ...
- 在JavaScript中删除对象
本文翻译自:Deleting Objects in JavaScript I'm a bit confused with JavaScript's delete operator. 我对JavaScr ...
- 在JavaScript中使用json.js:Ajax项目之POST请求(异步)
经常在百度搜索框输入一部分关键词后,弹出候选关键热词.现在我们就用Ajax技术来实现这一功能. 一.下载json.js文件 百度搜一下,最好到json官网下载,安全起见. 并与新建的两个文件部署如图 ...
- “bang” in JavaScript
!这个是一个逻辑运算符,用于将值转换为其相反的布尔值.因为JavaScript会强制值,所以它会将值"转换"为true/false形式,并返回相反的布尔值. 当我们使用的时候!对0 ...
- javascript版谷歌身份验证器google authenticator
https://www.cnblogs.com/huangcong/p/11028910.html : https://github.com/wuyanxin/totp.js 使用JS计算Goo ...
- JavaScript入门到精通(十三)
JavaScript基础 120. 修改div移动练习 121. 延时调用 122. 定时器的应用 123. 轮播图 124. 类的操作 125. 二级菜单 126. JSON 本章节通过练习需要 ...
最新文章
- leetcood学习笔记-45-跳跃游戏二
- ERC20 Short Address Attack
- sap 打印预览界面点击打印时记录打印次数_SAP打印机设置
- C#灰度图转伪彩色图
- mysql 解压缩安装_[mysql] MySQL解压缩安装步骤
- Eclipse导入Zookeeper源码Version2017.11.3
- Qt捕捉窗口关闭事件
- 如何开启QtCreator的代码自动补全功能
- iOS import,include和class的区别
- 提权、渗透、经验、技巧总结大全三
- Lattice的构建过程
- linux下安装mysql8.0
- Android:短信的接收
- @所有人:产品汪、运营喵专属台历,你值得拥有!
- 全国中小学教师计算机水平考试,中小学教师计算机水平考试笔试试题
- (6) IFC构件与空间的关系 (Industry Foundation Class)
- 【排序算法】图解直接插入排序(图解堪比Debug显示每次循环结果)
- C语言中strstr函数功能及用法
- 震撼人心的敏涵控股集团———如日方升
- jenkins内部分享ppt