-5>如何2 === -2?

发布于 2025-01-26 04:18:25 字数 757 浏览 3 评论 0 原文

在MDN文档中,下面有此示例在右移动运算符下。

const a = 5;          //  00000000000000000000000000000101
const b = 2;          //  00000000000000000000000000000010
const c = -5;         // -00000000000000000000000000000101

console.log(a >> b);  //  00000000000000000000000000000001
// expected output: 1

console.log(c >> b);  // -00000000000000000000000000000010
// expected output: -2

5>> 2 是有道理的,因为您将数字向右移动2个空间。在 -5>>的第二个示例中2 ,为什么即使它是>> 2

MDN文档:

In the MDN docs, there is this example below under right shift operator.

const a = 5;          //  00000000000000000000000000000101
const b = 2;          //  00000000000000000000000000000010
const c = -5;         // -00000000000000000000000000000101

console.log(a >> b);  //  00000000000000000000000000000001
// expected output: 1

console.log(c >> b);  // -00000000000000000000000000000010
// expected output: -2

5 >> 2 makes sense because you shift the digits right 2 spaces. In the second example of -5 >> 2, why does it only seemingly shift to the right one space even though it's >> 2?

MDN Docs: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Right_shift

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤者何惧 2025-02-02 04:18:25

就像约翰尼(Johnny)的一面一样,您可能需要检查两者的补充规则。最左侧的数字可能是 0 的内部<代码> 1 。您可能需要用 -9 检查示例此示例将帮助您更好地理解。

Just like Johnny side, you might want to check the Two's complement rule. The number in left most might be 1 inside of 0. You might want to check the example with -9 this example will help you understand better.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文