了解JavaScript的按位NOT运算符 [英] Understanding JavaScript bitwise NOT operator

查看:112
本文介绍了了解JavaScript的按位NOT运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

感谢大家提前 -

alert((~1).toString(2));

输出: -10

但在PHP / Java中输出11111111111111111111111111111110

But in PHP/Java it outputs 11111111111111111111111111111110

我缺少的东西,为什么使用Javascript添加一个 - ?到输出

Am I missing something, why does Javascript add a "-" to the output?

THX,
山姆

Thx, Sam

推荐答案

我知道Java使用补重新present负数,而在二进制的 11111111111111111111111111111110 的,这是什么的 〜1 的给予,再presents -2。或者,再$ P $二进制psented负号,-10,这是你得到了什么。

I know Java uses two's complement to represent negative numbers, and 11111111111111111111111111111110 in binary, which is what ~1 gives, represents -2. Or, represented in binary with a negative sign, -10, which is what you got.

在计算10(基数2)使用补消极的方式是,首先反转所有位,给你:

The way you calculate the negative of 10 (in base 2) using two's complement is that you first invert all of the bits, giving you:

11111111111111111111111111111101

然后你加1,给你:

11111111111111111111111111111110

我想同样是在Javascript中发生的事情。

I guess the same is happening in Javascript.

这篇关于了解JavaScript的按位NOT运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆