如何64bit的整数拆分两个32位整数

编程入门 行业动态 更新时间:2024-10-24 22:30:23
本文介绍了如何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整数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位整数

本文发布于:2023-11-30 01:34:43,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1648280.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:整数   两个   bit

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!