Java优化无符号N位整数对象

时间:2013-11-21 04:22:54

标签: java optimization wrapper bit-manipulation unsigned-integer

我正在编写扩展Number的自己的Integer Wrapper类。我相信我已经实现了大部分功能。目前我正在使用位级操作。

问题

  1. 我是否正在做任何可能导致问题或与Number班级发生冲突的事情?

  2. 我注意到Number在封面下创建了一个不可变的整数,但我希望能够操纵我的底层整数。这是个坏主意吗?

  3. 我应该如何处理负数整数?我需要这是一个无符号整数,所以我只能可靠地存储多达31位。我可以存储所有32个并将其视为未签名的,还是我必须使用long作为我的value

  4. 如果用户将负数传递给构造函数,或者我应该将该值设置为零,是否应该抛出错误?

    this.value = value > 0 ? extractLowerBits(value) : 0;

  5. 这是我的整数包装器:

    NBitUInteger.java

    public final class NBitUInteger extends Number implements
            Comparable<NBitUInteger> {
    
        private static final long serialVersionUID = 1L;
        private int value;
        private final int bits;
        private final int bitMask;
    
        public static final int MIN_VALUE = 0x0;
        public static final int MAX_VALUE = 0x7fffffff;
    
        public NBitUInteger() {
            this(0);
        }
    
        public NBitUInteger(int value) {
            this(value, 32);
        }
    
        public NBitUInteger(int value, int bits) {
            this.bits = bits;
            this.bitMask = bitMask(bits);
            this.value = extractLowerBits(value);
        }
    
        private final int extractLowerBits(final int value) {
            return value & bitMask;
        }
    
        public int bitMask() {
            return this.bitMask;
        }
    
        public int bits() {
            return this.bits;
        }
    
        @Override
        public int intValue() {
            return this.value;
        }
    
        @Override
        public long longValue() {
            return (long) this.value;
        }
    
        @Override
        public float floatValue() {
            return (float) this.value;
        }
    
        @Override
        public double doubleValue() {
            return (double) this.value;
        }
    
        @Override
        public int compareTo(NBitUInteger other) {
            return compare(this.value, other.value);
        }
    
        public static final int compare(final int x, final int y) {
            return (x < y) ? -1 : ((x == y) ? 0 : 1);
        }
    
        public final NBitUInteger shiftRight(final int amount) {
            this.value = this.value >> amount;
    
            return this;
        }
    
        public final NBitUInteger shiftRight() {
            return shiftRight(1);
        }
    
        public final NBitUInteger shiftRightPrependBit(final int bit) {
            this.value = (bit << (bits - 1)) | shiftRight().intValue();
    
            return this;
        }
    
        public final NBitUInteger shiftLeft(final int amount) {
            this.value = (this.value << amount) & bitMask;
    
            return this;
        }
    
        public final NBitUInteger shiftLeft() {
            return shiftLeft(1);
        }
    
        public final NBitUInteger shiftLeftAppendBit(final int bit) {
            this.value = shiftLeft().intValue() | bit;
    
            return this;
        }
    
        public final String toBinaryString() {
            return toBinaryString(this.value, this.bits);
        }
    
        public static final String toBinaryString(int value, int bits) {
            StringBuilder binStr = new StringBuilder(Integer.toBinaryString(value));
    
            while (binStr.length() < bits)
                binStr.insert(0, '0');
    
            return binStr.toString();
        }
    
        public static final int bitMask(final int bits) {
            return (1 << bits) - 1;
        }
    }
    

    测试

    public class NBitUIntegerTest {
        public static void main(String[] args) {
            NBitUInteger x = new NBitUInteger(13, 4);
            printResult(x, 13);
    
            x.shiftRight().shiftRight().shiftRight();
            printResult(x, 1);
    
            x.shiftLeft(2);
            printResult(x, 4);
    
            x.shiftLeftAppendBit(1).shiftLeftAppendBit(1).shiftLeftAppendBit(1);
            printResult(x, 7);
    
            x.shiftRightPrependBit(1);
            printResult(x, 11);
        }
    
        public static void printResult(NBitUInteger value, int expected) {
            System.out.printf("%s == %s => %b\n",
                value.toBinaryString(),
                NBitUInteger.toBinaryString(expected, value.bits()),
                (value.intValue() == expected));
        }
    }
    

    输出

    1101 == 1101 => true
    0001 == 0001 => true
    0100 == 0100 => true
    0111 == 0111 => true
    1011 == 1011 => true
    

0 个答案:

没有答案