Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从java文件中获取特定数量的位_Java_Bit Manipulation_Bit - Fatal编程技术网

从java文件中获取特定数量的位

从java文件中获取特定数量的位,java,bit-manipulation,bit,Java,Bit Manipulation,Bit,我正在尝试制作一个从文件中读取17位块的代码,但我不知道如何完成,我的意思是我将有任何文件,需要读取下一个17位以应用crc算法。今天下午我想编程 下面的类bitbreader允许您通过readBits方法一次最多读取8位,或者通过readBits17方法一次读取17位 源代码 public class BitReader { private static final int[] MASK = new int[16]; static { for (int i =

我正在尝试制作一个从文件中读取17位块的代码,但我不知道如何完成,我的意思是我将有任何文件,需要读取下一个17位以应用crc算法。今天下午我想编程

下面的类
bitbreader
允许您通过
readBits
方法一次最多读取8位,或者通过
readBits17
方法一次读取17位

源代码

public class BitReader {
    private static final int[] MASK = new int[16];
    static {
        for (int i = 0; i < 16; i++) {
            MASK[i] = (1 << i) - 1;
        }
    }

    private InputStream in;

    private int bitsLeft;

    private int bitBuffer;

    public BitReader(InputStream in) {
        this.in = in;
    }

    /**
     * Reads at most 8 bits from the InputStream.
     * 
     * @param bits
     *            between 1 and 8 (inclusive)
     */
    public int readBits(int bits) throws IOException {
        if (bits < 1 && bits > 8)
            throw new IllegalArgumentException("bits");
        if (bits > bitsLeft) {
            int r = in.read();
            if (r == -1) {
                throw new EOFException();
            }
            bitsLeft += 8;
            bitBuffer = (bitBuffer << 8) | r;
        }
        int result = bitBuffer >> (bitsLeft - bits);
        bitsLeft -= bits;
        bitBuffer &= MASK[bitsLeft];
        return result;
    }

    public int readBits17() throws IOException {
        return readBits(8) << 9 | readBits(8) << 1 | readBits(1);
    }
}

(版权所有:我在此将此代码放在公共领域,供所有人使用。)

今天下午我想编写一些东西

下面的类
bitbreader
允许您通过
readBits
方法一次最多读取8位,或者通过
readBits17
方法一次读取17位

源代码

public class BitReader {
    private static final int[] MASK = new int[16];
    static {
        for (int i = 0; i < 16; i++) {
            MASK[i] = (1 << i) - 1;
        }
    }

    private InputStream in;

    private int bitsLeft;

    private int bitBuffer;

    public BitReader(InputStream in) {
        this.in = in;
    }

    /**
     * Reads at most 8 bits from the InputStream.
     * 
     * @param bits
     *            between 1 and 8 (inclusive)
     */
    public int readBits(int bits) throws IOException {
        if (bits < 1 && bits > 8)
            throw new IllegalArgumentException("bits");
        if (bits > bitsLeft) {
            int r = in.read();
            if (r == -1) {
                throw new EOFException();
            }
            bitsLeft += 8;
            bitBuffer = (bitBuffer << 8) | r;
        }
        int result = bitBuffer >> (bitsLeft - bits);
        bitsLeft -= bits;
        bitBuffer &= MASK[bitsLeft];
        return result;
    }

    public int readBits17() throws IOException {
        return readBits(8) << 9 | readBits(8) << 1 | readBits(1);
    }
}

(版权所有:我在此将此代码放在公共领域,供所有人使用。)

今天下午我想编写一些东西

下面的类
bitbreader
允许您通过
readBits
方法一次最多读取8位,或者通过
readBits17
方法一次读取17位

源代码

public class BitReader {
    private static final int[] MASK = new int[16];
    static {
        for (int i = 0; i < 16; i++) {
            MASK[i] = (1 << i) - 1;
        }
    }

    private InputStream in;

    private int bitsLeft;

    private int bitBuffer;

    public BitReader(InputStream in) {
        this.in = in;
    }

    /**
     * Reads at most 8 bits from the InputStream.
     * 
     * @param bits
     *            between 1 and 8 (inclusive)
     */
    public int readBits(int bits) throws IOException {
        if (bits < 1 && bits > 8)
            throw new IllegalArgumentException("bits");
        if (bits > bitsLeft) {
            int r = in.read();
            if (r == -1) {
                throw new EOFException();
            }
            bitsLeft += 8;
            bitBuffer = (bitBuffer << 8) | r;
        }
        int result = bitBuffer >> (bitsLeft - bits);
        bitsLeft -= bits;
        bitBuffer &= MASK[bitsLeft];
        return result;
    }

    public int readBits17() throws IOException {
        return readBits(8) << 9 | readBits(8) << 1 | readBits(1);
    }
}

(版权所有:我在此将此代码放在公共领域,供所有人使用。)

今天下午我想编写一些东西

下面的类
bitbreader
允许您通过
readBits
方法一次最多读取8位,或者通过
readBits17
方法一次读取17位

源代码

public class BitReader {
    private static final int[] MASK = new int[16];
    static {
        for (int i = 0; i < 16; i++) {
            MASK[i] = (1 << i) - 1;
        }
    }

    private InputStream in;

    private int bitsLeft;

    private int bitBuffer;

    public BitReader(InputStream in) {
        this.in = in;
    }

    /**
     * Reads at most 8 bits from the InputStream.
     * 
     * @param bits
     *            between 1 and 8 (inclusive)
     */
    public int readBits(int bits) throws IOException {
        if (bits < 1 && bits > 8)
            throw new IllegalArgumentException("bits");
        if (bits > bitsLeft) {
            int r = in.read();
            if (r == -1) {
                throw new EOFException();
            }
            bitsLeft += 8;
            bitBuffer = (bitBuffer << 8) | r;
        }
        int result = bitBuffer >> (bitsLeft - bits);
        bitsLeft -= bits;
        bitBuffer &= MASK[bitsLeft];
        return result;
    }

    public int readBits17() throws IOException {
        return readBits(8) << 9 | readBits(8) << 1 | readBits(1);
    }
}


(版权所有:我在此将此代码放在公共领域,供所有人使用。)

如何表示这17位;在一个
int
或一个
byte[]
中,或者以另一种方式?我尝试使用浮点值,因为它有4个字节,int只有2个字节,我需要17位,但我不知道只选择17位。在Java中,
int
总是有32位,但它是一个有符号的值short有16位。噢,谢谢你的信息!你不能直接这么做。但是你可以读取字节,把你没有用到的位放在int中,这样你下次想读取17位的东西时就可以使用它们;在一个
int
或一个
byte[]
中,或者以另一种方式?我尝试使用浮点值,因为它有4个字节,int只有2个字节,我需要17位,但我不知道只选择17位。在Java中,
int
总是有32位,但它是一个有符号的值short有16位。噢,谢谢你的信息!你不能直接这么做。但是你可以读取字节,把你没有用到的位放在int中,这样你下次想读取17位的东西时就可以使用它们;在一个
int
或一个
byte[]
中,或者以另一种方式?我尝试使用浮点值,因为它有4个字节,int只有2个字节,我需要17位,但我不知道只选择17位。在Java中,
int
总是有32位,但它是一个有符号的值short有16位。噢,谢谢你的信息!你不能直接这么做。但是你可以读取字节,把你没有用到的位放在int中,这样你下次想读取17位的东西时就可以使用它们;在一个
int
或一个
byte[]
中,或者以另一种方式?我尝试使用浮点值,因为它有4个字节,int只有2个字节,我需要17位,但我不知道只选择17位。在Java中,
int
总是有32位,但它是一个有符号的值short有16位。噢,谢谢你的信息!你不能直接这么做。但是你可以读取字节,把你没有使用的位放在int中,这样下次你想读取17位的东西时就可以使用它们了。