如何64bit的整数拆分两个32位整数 [英] how to split 64bit integer to two 32bit integers

查看:1469
本文介绍了如何64bit的整数拆分两个32位整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要一个64位的整数分成两个32位的整数:

I want to split a 64bit integer into two 32bit integers:

var bigInt = 0xffffff;

var highInt = bigInt >> 8 // get the high bits 0xfff
var lowInt = bigInt // cut of the first part (with &)?

console.log(highInt); // 0xfff
console.log(lowInt); // 0xfff

// set them together again
var reBigInt = (highInt << 8) + lowInt;

不幸的是没有得到highInt也得到lowInt工程...可能有人给我答案,我需要怎么使用位运算符?

Unfortunately neither getting the highInt nor getting the lowInt works... Could somebody give me the answer how I need to use the bitwise operators?

关于

推荐答案

修改 JavaScript的重新presents整数<一个href=\"http://stackoverflow.com/questions/2983206/bitwise-and-in-javascript-with-a-64-bit-integer\">using IEEE双precision格式,所以没有办法,存储任意64位整数而不precision的损失除了通过定制的大整数库。对潜在的裁剪值位运算显然毫无意义。

EDIT JavaScript represents integers using IEEE double precision format, so there is no way to store arbitrary 64 bit integers without loss of precision, except through custom big integer libraries. Bitwise operations on potentially cropped values obviously make no sense.

在一般情况下,对于支持64位整数的语言:

In general, for languages that do support 64 bit integers:

的人的64位模式为 0xffffffffffffffff 。要提取的高32位,则需要32移:&GT;&GT; 32 。要使用32个1提取低32位,公正,它们:&安培;为0xffffffff

A 64-bit pattern of ones is 0xffffffffffffffff. To extract the upper 32 bits, you need to shift by 32: >> 32. To extract the lower 32 bit, just and them with 32 ones: & 0xffffffff.

您得到了正确的原则 - 你有多少位转移或掩盖是错误算法。

You got the principle right - your arithmetic on how many bits to shift or mask is just wrong.

这篇关于如何64bit的整数拆分两个32位整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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