关于javascript号码的另一个问题! [英] Another problem about javascript number!

查看:48
本文介绍了关于javascript号码的另一个问题!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

code1:var x = 0xf0000000;

alert(x);


输出:4026531840


code2:var b = 0xf<< 28;

alert(b);


输出:-268435456


我有上面这样的代码,code2如何输出和code1相同?!

code1:var x=0xf0000000;
alert(x);

output:4026531840

code2:var b=0xf<<28;
alert(b);

output:-268435456

I have such above codes , how can code2 output the same as code1?!

推荐答案



chanma写道:

chanma wrote:
code1:var x = 0xf0000000;
alert(x);

输出:4026531840

code2:var b = 0xf<< 28;
alert(b);

输出:-268435456
我有上面这样的代码,code2输出怎么样code1?!
code1:var x=0xf0000000;
alert(x);

output:4026531840

code2:var b=0xf<<28;
alert(b);

output:-268435456

I have such above codes , how can code2 output the same as code1?!




你不能(通过正式的官方手段)。那将是一个人之后

我可能会布置所有细节,你只记得你的范围

这样的操作是

从-2147483648到2147483647(对不起,但请尽量满意吗

你有:-)



You can''t (by regular official means). That''s going to be someone after
me probably to layout all details, you just remember that your range
for such operations is
from -2147483648 to 2147483647 (sorry, but try to be happy with that
you have :-)


> code1 :var x = 0xf0000000;
>code1:var x=0xf0000000;
alert(x);
输出:4026531840
alert(x); output:4026531840




但这些代码实际输出4026531840;

这证明JS引擎有时会将一个数字解释为

未签名的,但有时作为我的code2,将其解释为签名

one。似乎没有办法告诉JS引擎何时以及如何

解释代码?



But these code actually output 4026531840;
this prove that the JS engine sometimes interprets one num to an
unsigned one,but sometimes as my code2,interprets it to an signed
one.Doesn''t it seem there be some way to tell JS engine when and how
to interpret the code?




chanma写道:

chanma wrote:
code1:var x = 0xf0000000;
alert(x);
code1:var x=0xf0000000;
alert(x);


输出:4026531840
output:4026531840



但这些代码实际上输出了4026531840;
这证明JS引擎有时会将一个数字解释为一个无符号的数字,但有时作为我的代码2,将其解释为已签名
one.难道似乎没有办法告诉JS引擎何时以及如何解释代码?



But these code actually output 4026531840;
this prove that the JS engine sometimes interprets one num to an
unsigned one,but sometimes as my code2,interprets it to an signed
one.Doesn''t it seem there be some way to tell JS engine when and how
to interpret the code?




不要混合两个非常分离电子邮件:

1. JavaScript引擎可以处理的最大数量:

alert(Number.MAX_VALUE)可以看到
;

2. 32位有符号整数的最低/最大值你可以按位运算处理
:这个范围在我之前的答案中给出。

如果绝对有必要为

支持范围内的整数实现它,则必须使用

按位操作实现自己的BigMath库。底部和字符串连接/解析

顶部。

任何BigMath在32位机器上的资源非常昂贵,这就是为什么

例如Mozilla.org每个64位的数字导致了一个

的讨论我们真的需要它吗?或者我们可以在某种程度上保持在/ b
32bit之内。但是根据你的实际任务,减速可能不是那么重要。



Do not mix two very separate things:
1. The biggest number JavaScript engine can handle: that one can be
seen by
alert(Number.MAX_VALUE);
2. The lowest / biggest values of 32bit signed integer you can handle
in bitwise operations: this range is given in my previous answer.

If it is absolutely necessary to implement it for integers out of the
supported range, you have to implement your own BigMath library with
bitwise operations at the bottom and string concatenation / parsing at
the top.
Any BigMath is very resource expensive on 32bit machines, this is why
for example in Mozilla.org each and every 64bit number leads to a
discussion "do we really need it here or can we stay somehow within
32bit". But depending on your actual task the slow down may not be so
important.


这篇关于关于javascript号码的另一个问题!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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