繁体   English   中英

在不依赖parseInt的情况下,使用Java以任意位数将一个二进制输入拼接为另一个二进制输入的有效方法是什么?

[英]What is an efficient way to splice one binary input into another at an arbitrary bit number using Java without relying on parseInt?

我正在尝试在Java中创建一个函数,以使用Java在位级别而不是字节级别将一个二进制输入拼接为另一个二进制输入。 例如,将“ 00000000”拼接到“ 11111111”的位置3将产生“ 1110000000011111”。

我曾尝试研究用于处理二进制数据的JBBP库 ,但似乎好像有一种特殊的编码语言(除Java以外)似乎是Java特定的,您可以编写该语言然后将其作为Java中的字符串传递给Java。 JBBPParser.prepare()函数。 我尝试在Java文档中查找该库,但它们仅描述该函数的功能,而不是您可以作为字符串传递的命令,这些命令的功能或这些命令的正确语法。

谁能为您提供命令的文档链接,您可以将这些命令作为字符串传递给JBBPParser函数,或者提供一种替代方法,以任意位将二进制数据拼接在一起,而不必依赖二进制字符串和parseInt,因为它们效率低下?

阅读有关Endianness的信息 Java是big-endian。 这意味着您的示例是错误的。 您的示例演示了在位置6之后或位置5之前插入零字符串,而不是在目标字节中的位置3之后插入零字符串(00000000)。

您要查找的操作称为“左移”。 这是讨论按位运算符和移位寄存器的链接。

这是您要使用的策略:

  1. 确定从目标移到目标的位数。 我将其称为initialShiftCount 在您的示例中,initialShiftCount为3位。
  2. 将initialShiftCount位数左移到目标中。
  3. 将所有8位从插入字符串向左移到目标位置。
  4. 将剩余的位从目标左移到目标。

术语: 目的地 -您要将插入字符串中的位组合与目标一起的位置。 在您的示例中,该变量最终包含字符串1110000000011111'.<br/> *insert string* - the bits you want to insert into the target bits. In your example, the insert string is 1110000000011111'.<br/> *insert string* - the bits you want to insert into the target bits. In your example, the insert string is 00000000。 .<br/> *target* - the bits into which you want to insert the insert string. In your example, the target is .<br/> *target* - the bits into which you want to insert the insert string. In your example, the target is 11111111`。

位编号在大端系统中,位的编号如下:87654321

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM