如何存储在Ruby的两个32位整数的64位整数 [英] How to store a 64 bit integer in two 32 bit integers in Ruby

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

问题描述

正如标题所说,我有点失去了如何在Ruby中做到这一点...有主题对如何做到这一点在C或C ++数。任何Ruby专家,有可以附和这个?

As the title says, I'm a little lost on how to accomplish this in Ruby...there are number of topics on how to do this in C or C++. Any ruby experts out there that can chime in on this?

推荐答案

你会用C使用相同的语法作品红宝石,只是下降的类型转换:

The same syntax you'd use in C works in ruby, just drop the typecasts:

n = 0xFFFFFFFFEEEEEEEE
x = (n & 0xFFFFFFFF00000000) >> 32
y =  n & 0xFFFFFFFF
puts x.to_s(16)
# => "ffffffff"
puts y.to_s(16)
# => "eeeeeeee"
v = x << 32 | y
puts v.to_s(16)
# => "ffffffffeeeeeeee"

如果您需要的值是正好32位的块(即你需要发言的二进制一些外部数据文件或程序),那么你需要使用的阵列#包并的字符串#解压,以得到正确的位。

If you need the values to be in chunks of exactly 32 bits (i.e. you need to speak binary to some external data file or program), then you'll want to use Array#pack and String#unpack to get the right bits.

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

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