Java中子串的按位运算

发布于 2024-11-11 12:18:46 字数 500 浏览 5 评论 0原文

想象一下您有一个数字的二进制或十六进制表示形式。让我们考虑一下:

int number  = 0xffffff;
// this would recover the third f, as a stand-alone value, value of third_f would be just f
int third_f = byteSubstring(number,2,1);
// third and fourth f, value of tf would be ff
int tf = byteSubstring(number,2,2);
// all except first, value of except_first would be fffff
int except_first = byteSubstring(number,1,5);

使​​用单独的按位运算、笔和笔。纸,我知道如何提取所有这些,但将它们组合在一个通用函数中......:)。 JDK 中是否已经有可用的东西可以对数字类型执行此操作?

Imagine you'd have the binary or hex representation of a number. Let's take this:

int number  = 0xffffff;
// this would recover the third f, as a stand-alone value, value of third_f would be just f
int third_f = byteSubstring(number,2,1);
// third and fourth f, value of tf would be ff
int tf = byteSubstring(number,2,2);
// all except first, value of except_first would be fffff
int except_first = byteSubstring(number,1,5);

Using separate bitwise operations, pen & paper, I know how to extract all of them, but combining them in one generic function ... :). Is there something already available in the JDK that can do this for numeric types?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

檐上三寸雪 2024-11-18 12:18:47

您有一个以位为单位指定的大小偏移。传统上,这些位是从 LSB 开始编号的。

您可以通过右移来处理偏移

result = x >>> offset

您可以通过屏蔽来处理大小; (1 << size) - 1 是掩码

result = result & ((1 << size) - 1)

You have a size and offset specified in bits. Traditionally the bits are numbered starting at the LSB.

You handle the offset by shifting right

result = x >>> offset

You handle the size by masking; (1 << size) - 1 is the mask

result = result & ((1 << size) - 1)
三生池水覆流年 2024-11-18 12:18:47

Java 有常见的 按位运算符,因此您可以构建一个掩码以及您的数据。

编辑

也许一些示例代码会更有用:

// be aware - this actually substrings a hex substring, using
// bit ops
int byteSubString(int number, 
                  int firstPos /* 0-indexed */, 
                  int length) {
        // tricky naming as hex = 2 byte per char!
        int mask = 0;
        switch (length) { //lookup table/array might be best here
        case 0: mask = 0; break;
        case 1: mask = 0xf; break;
        case 2: mask = 0xff; break;
        case 3: mask = 0xfff; break;
        case 4: mask = 0xffff; break;
        case 5: mask = 0xfffff; break;
        default: throw new IllegalArgumentException(
                 "Length of " + length + " not supported");
        }
        int tmp = (number >> (4*firstPos));
        tmp = (tmp & mask);
        System.out.println("Byte substring on " +
                       Integer.toHexString(number) + 
                       " starting at pos " + firstPos + 
                       " with length " + length + 
                       " uses mask " + Integer.toHexString(mask) + 
                       " results in " + Integer.toHexString(tmp));
        return tmp;
    }

当然,您也可以将字符串呈现为十六进制表示形式和子字符串。可能会更快、更易读:)

Java has the usual bitwise operators so you could just build a mask and and your data.

EDIT

Perhaps some example code will be a bit more usefull:

// be aware - this actually substrings a hex substring, using
// bit ops
int byteSubString(int number, 
                  int firstPos /* 0-indexed */, 
                  int length) {
        // tricky naming as hex = 2 byte per char!
        int mask = 0;
        switch (length) { //lookup table/array might be best here
        case 0: mask = 0; break;
        case 1: mask = 0xf; break;
        case 2: mask = 0xff; break;
        case 3: mask = 0xfff; break;
        case 4: mask = 0xffff; break;
        case 5: mask = 0xfffff; break;
        default: throw new IllegalArgumentException(
                 "Length of " + length + " not supported");
        }
        int tmp = (number >> (4*firstPos));
        tmp = (tmp & mask);
        System.out.println("Byte substring on " +
                       Integer.toHexString(number) + 
                       " starting at pos " + firstPos + 
                       " with length " + length + 
                       " uses mask " + Integer.toHexString(mask) + 
                       " results in " + Integer.toHexString(tmp));
        return tmp;
    }

Of course you could also just render the String to a hex representation and substring that. Might be faster and more readable :)

腻橙味 2024-11-18 12:18:47

请参阅 Formatter 中的“format”语法与String.format("%x", hexa)结合。例如,String.format("%x", 0xffffff) 返回String“ffffff”。然后您可以编写您想要的方法作为此方法和 String.substring 的包装器。

然而,它不能处理二进制,但手动编码更容易。

编辑:实际上 Integer.toBinaryString 也存在。

See the "format" syntax in Formatter and combine with String.format("%x", hexa). For example String.format("%x", 0xffffff) returns the String "ffffff". Then you can just write the methods you want as wrapper over this and String.substring.

However, it cannot deal with binary, but that's easier to code by hand.

Edit: actually Integer.toBinaryString also exists.

飘过的浮云 2024-11-18 12:18:47

我不知道标准库中有任何函数可以做到这一点。
类似的东西在 Integer 类中,有一些函数对位进行一些组合操作。

你可以自己编码:

// offset and len are bits
// (so you multiply them by 4 if you want to get a hex representation)
int substring(int value, int offset, int len) {
  value >>>= (Integer.SIZE - Integer.numberofLeadingZeros(value)) - (offset + len);
  return value & ((1 << len) - 1);
}

I don't know of any functions in the standard library that does that.
Things that are alike are in Integer class, there are some functions that do some composed operations on bits.

You can code it yourself:

// offset and len are bits
// (so you multiply them by 4 if you want to get a hex representation)
int substring(int value, int offset, int len) {
  value >>>= (Integer.SIZE - Integer.numberofLeadingZeros(value)) - (offset + len);
  return value & ((1 << len) - 1);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文