从java文件中获取特定数量的位
我正在尝试制作一个从文件中读取17位块的代码,但我不知道如何完成,我的意思是我将有任何文件,需要读取下一个17位以应用crc算法。今天下午我想编程 下面的类从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 =
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位,但它是一个有符号的值int
或一个byte[]
中,或者以另一种方式?我尝试使用浮点值,因为它有4个字节,int只有2个字节,我需要17位,但我不知道只选择17位。在Java中,int
总是有32位,但它是一个有符号的值int
或一个byte[]
中,或者以另一种方式?我尝试使用浮点值,因为它有4个字节,int只有2个字节,我需要17位,但我不知道只选择17位。在Java中,int
总是有32位,但它是一个有符号的值int
或一个byte[]
中,或者以另一种方式?我尝试使用浮点值,因为它有4个字节,int只有2个字节,我需要17位,但我不知道只选择17位。在Java中,int
总是有32位,但它是一个有符号的值