〜操作员 [英] ~ operator
问题描述
这两行应该是一样的。但是在IE5中,它们会显示两个不同的数字。
为什么?
document.write((0xfffffff0).toString(10)+"< br> ;");
document.write((~0xf).toString(10)+"< br>");
谢谢你。
Hi,
These 2 lines should be the same. But in IE5, they display 2 different numbers.
Why?
document.write((0xfffffff0).toString(10) + "<br>");
document.write((~0xf).toString(10) + "<br>");
Thank you.
推荐答案
toString()方法不参数
~不是数字符号的一部分
你确定你正在学习JavaScript而不是其他东西吗?
如果你不仅仅是在开玩笑,你可能想看看像
这样的东西 http://www.devguru.com/Technologies/ .. .ript_intro.htm
l
chirs< ym*@kicon.com>在消息中写道
news:4c ************************** @ posting.google.c om ...
toString() method doesn''t take arguments
~ is not a part of number notation
Are you sure you are studying JavaScript and not something else?
If you are not just joking, you may want to look on something like
http://www.devguru.com/Technologies/...ript_intro.htm
l
chirs <ym*@kicon.com> wrote in message
news:4c**************************@posting.google.c om...
这两行应该是一样的。但在IE5中,它们显示2个不同的
数字。为什么?
document.write((0xfffffff0).toString(10)+"< br>");
document.write((~0xf).toString(10) +"< br>");
谢谢。
Hi,
These 2 lines should be the same. But in IE5, they display 2 different numbers. Why?
document.write((0xfffffff0).toString(10) + "<br>");
document.write((~0xf).toString(10) + "<br>");
Thank you.
chirs说:
这2行应该是一样的。但是在IE5中,它们会显示两个不同的数字。
为什么?
document.write((0xfffffff0).toString(10)+"< br>");
document.write((~0xf).toString(10)+"< br>");
Hi,
These 2 lines should be the same. But in IE5, they display 2 different numbers.
Why?
document.write((0xfffffff0).toString(10) + "<br>");
document.write((~0xf).toString(10) + "<br>");
因为数字不是存储为32位整数。
Because numbers are not stored as 32 bit integers.
ym*@kicon.com (chirs)写道:
ym*@kicon.com (chirs) writes:
这两行应该是相同的。
说谁? :)
但在IE5中,它们显示2个不同的数字。为什么?
因为
0xfffffff0!= ~0xf
0xfffffff0的值是4294967280.
值〜0xf = -16。
对不相等的Javascript数字进行比较。
Javascript整数不是32位数,它们是实际上IEEE
双精度浮点数。位级操作
首先将数字转换为32位整数(跳过
小数并在整数部分取模2 ^ 32)。结果是
视为*带* 32位二进制补码。
这就是为什么~0xf = -16。前15个转换为32位数。然后
其位被反转,给出位模式0xfffffff0。这个,* b $ b被解释为* * * 32位数字是-16。
由于你没有对0xfffffff0进行位操作,所以它不被解释为
a签署了32位值,就像一些正数一样。
如果你把〜改为0xfffffff0,这两行就会给出
相同(15)。如果您将0xfffffff0解释为32位有符号数,
它也会给出相同的(~~ 0xfffffff0 == ~0xf)。
document.write((0xfffffff0 ).toString(10)+"< br>");
document.write((~0xf).toString(10)+"< br>");
These 2 lines should be the same.
Says who? :)
But in IE5, they display 2 different numbers. Why?
Because
0xfffffff0 != ~0xf
The value of 0xfffffff0 is 4294967280.
The value of ~0xf = -16.
The comparison is made on Javascript numbers which are not equal.
Javascript integers are not 32 bit numbers, they are actually IEEE
double-precission floating point numbers. The bit-level operations
work by first converting the number to 32-bit integers (skipping
decimals and taking modulo 2^32 on the integer part). The result is then
treated as a *signed* 32-bit two''s complement number.
That is why ~0xf = -16. First 15 is converted to a 32-bit number. Then
its bits are inverted, giving the bit-pattern 0xfffffff0. This,
interpreted as a *signed* 32-bit number is -16.
Since you do no bit operations on 0xfffffff0, it is not interpreted as
a signed 32-bit value, just as some positive number.
If you put the ~ on 0xfffffff0 instead, the two lines would give the
same (15). If you interpreted 0xfffffff0 as a 32-bit signed number,
it would also give the same (~~0xfffffff0 == ~0xf).
document.write((0xfffffff0).toString(10) + "<br>");
document.write((~0xf).toString(10) + "<br>");
(和〜是Javascript中的一个补码(按位否定),比如大多数基于C语法的语言中的
和Number.prototype。 toString在Javascript 1.1 +,JScript 2+和ECMAScript中使用
参数
/ L
-
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D''HTML:< ; URL:http://www.infimum.dk/HTML/randomArtSplit.html>
''没有判断的信仰只会降低精神神圣。''
(And ~ is the one''s complement (bitwise negation) in Javascript, like
in most C-syntax based languages, and Number.prototype.toString takes
an argument in Javascript 1.1+, JScript 2+, and ECMAScript)
/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D''HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
''Faith without judgement merely degrades the spirit divine.''
这篇关于〜操作员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!