如何管理和操作极大的二进制值
我需要读入几个由二进制数字组成的非常大的字符串。这些字符串可能非常大(最多 100,000 位数字),我需要存储它们,能够操作(翻转位)并将其加在一起。我的第一个想法是将字符串拆分为 8 个字符块,将它们转换为字节并将它们存储在数组中。考虑到需要翻转的位的索引,这将使我能够相对轻松地翻转位,但通过这种方法,我不确定如何将两个值的整体添加在一起。
谁能找到一种以内存有效的方式存储这些值的方法,这将使我仍然能够对它们执行计算?
编辑: “加在一起”(连接?算术加法?) - 算术加法
我的问题是,在最困难的情况下,我有两个 100,000 位数字(存储在 12,500 字节的数组中)。存储和手动翻转位不是问题,但我需要两个数字的总和,然后才能找出它的第 x 位是什么。
I need to read in a couple of extremely large strings which are comprised of binary digits. These strings can be extremely large (up to 100,000 digits) which I need to store, be able to manipulate (flip bits) and add together. My first though was to split the string in to 8 character chunks, convert them to bytes and store them in an array. This would allow me to flip bits with relative ease given an index of the bit needed to be flipped, but with this approach I'm unsure how I would go about adding the entirety of the two values together.
Can anyone see a way of storing these values in a memory efficient manner which would allow me to be able to still be able to perform calculations on them?
EDIT:
"add together" (concatenate? arithmetic addition?) - arithmetic addition
My problem is that in the hardest case I have two 100,000 bit numbers (stored in an array of 12,500 bytes). Storing and manually flipping bits isn't an issue, but I need the sum of both numbers and then to be able to find out what the xth bit of this is.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对我来说,“二进制数字字符串”听起来绝对像字节数组。要将两个这样的字节数组“添加”在一起,您只需分配一个足够大以容纳所有内容的新字节数组,并使用 System.arraycopy 复制内容。
但是假设每个“字符串”都是 8 位的倍数。如果要将一个 15 位字符串“添加”到另一个 15 位字符串,则需要进行位移位。这对你来说可能是个问题吗?根据您需要的操作,您甚至可能只想保留一个了解两个字节数组的对象,并且可以在逻辑连接的“字符串”中找到任意位。
无论哪种方式,
byte[]
都将成为前进的方向 - 或者可能BitSet
。"Strings of binary digits" definitely sound like byte arrays to me. To "add" two such byte arrays together, you'd just allocate a new byte array which is big enough to hold everything, and copy the contents using
System.arraycopy
.However that assumes each "string" is a multiple of 8 bits. If you want to "add" a string of 15 bits to another string of 15 bits, you'll need to do bit-shifting. Is that likely to be a problem for you? Depending on what operations you need, you may even want to just keep an object which knows about two byte arrays and can find an arbitrary bit in the logically joined "string".
Either way,
byte[]
is going to be the way forward - or possiblyBitSet
.又怎样呢
?
您正在尝试做这样的事情吗?
What about
?
Is it something like this you are trying to do?