开发者

Bitwise operation substring in Java

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 d开发者_如何转开发o this for numeric types?


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)


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 :)


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.


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);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜