gpt4 book ai didi

javascript ->>> (零填充右移)当第二个操作数为零时

转载 作者:行者123 更新时间:2023-11-29 19:03:10 25 4
gpt4 key购买 nike

我理解什么是零填充右移,当第二个操作数不为零时,它产生的结果非常有意义:

-7 >>> 1
2147483644

比较

-7 >> 1
-4

但是当第二个操作数为零时:

-7 >> 0
-7 // Looks right!
-7 >>> 0
4294967289 // What? Why?

如果我移动零位,是否意味着我根本没有移动?那样的话,是不是应该把原来的号码还给我?我希望 -7 >>> 0 === -7

还有

-7 >>> 32
4294967289

同样,根据定义,我希望 -7 >>> n === 0 where n >= 32 因为所有数字都变为零!

最佳答案

specs 中找到了它的内部工作原理.

x >>> 0 将执行 ToUint32(x)

7.1.6 ToUint32 ( argument )

The abstract operation ToUint32 converts argument to one of 232 integer values in the range 0 through 232−1, inclusive. This abstract operation functions as follows: Let number be ToNumber(argument). ReturnIfAbrupt(number). If number is NaN, +0, −0, +∞, or −∞, return +0. Let int be the mathematical value that is the same sign as number and whose magnitude is floor(abs(number)). Let int32bit be int modulo 232. Return int32bit.

注意令int32bit为int模,并且

The notation “x modulo y” (y must be finite and nonzero) computes a value k of the same sign as y (or zero) such that abs(k) < abs(y) and x−k = q × y for some integer q.

所以根据上面的约定,-7 mod 2^32 === 2^32 - 7 === 4294967289

关于javascript ->>> (零填充右移)当第二个操作数为零时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45320261/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com