如何按位运算应用到实际的IEEE 754重新$ P $ JS数的psentation? [英] How to apply bitwise operations to the actual IEEE 754 representation of JS Numbers?

查看:131
本文介绍了如何按位运算应用到实际的IEEE 754重新$ P $ JS数的psentation?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在JavaScript中,当你执行按位运算,如 X<< 2 ,64位浮点再presentation被移位实际发生之前转换为32位无符号整型。我insterested在应用转移到实际的,不变的IEEE 754位元重新presentation。

In JavaScript, whenever you perform a bitwise operation such as x << 2, the 64-bit float representation gets converted to a 32-bit unsigned int before the shifting actually occurs. I am insterested in applying the shift to the actual, unaltered IEEE 754 bitwise representation.

这怎么可能?

推荐答案

您可以尝试先转换为JSNumber字节/整数,结果转移自己。

You might try converting the JSNumber to bytes/integers first and shifting the result yourself.

使用TypedArray东西可以在最新版本的主流浏览器:

Using TypedArray stuff available in recent versions of major browsers:

var f = new Float64Array( 1 );    // creating typed array to contain single 64-bit IEEE754
f.set( [ 1.0 ], 0 );              // transferring JSNumber for untyped array to first element of typed one
var d = new DataView( f.buffer ); // creating raw view on content in typed array
var w1 = d.getUint32( 0 );        // accessing bytes 0 to 3 of typed array
var w2 = d.getUint32( 4 );        // accessing bytes 4 to 7 of typed array

之后,你可以在 W1 转向32位字和 W2 单独在传输高2位低位字,以降低字上自己2位。

After that you could shift the 32-bit-words in w1 and w2 individually transferring upper 2 bits in lower word to lower 2 bits of upper word yourself.

字节序可能会使用第二个参数进行控制,以 d.getUint32()

Endianess might be controlled on using second argument to d.getUint32().

了解更多: https://developer.mozilla.org/en -US /文档/网络/的JavaScript / Typed_arrays

只是为了确保,BERGI的注释正确识别。我所有的code可能会降低到单行这样的:

Just to ensure, comment of Bergi is recognized properly. All my code might be reduced to single line like that:

var d = new Uint32Array( new Float64Array( [1.0] ).buffer );

D [0] D [1] 适用于访问包含32位字,然后。

d[0] and d[1] are suitable for accessing contained 32-bit words, then.

这篇关于如何按位运算应用到实际的IEEE 754重新$ P $ JS数的psentation?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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