Java 如何将字节转换为位?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4095463/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to convert a byte into bits?
提问by Supereme
I have one array of bytes. I want to access each of the bytes and want its equivalent binary value(of 8 bits) so as to carry out next operations on it. I've heard of BitSet but is there any other way of dealing with this?
我有一个字节数组。我想访问每个字节并想要其等效的二进制值(8 位),以便对其进行下一步操作。我听说过 BitSet 但还有其他方法可以解决这个问题吗?
Thank you.
谢谢你。
采纳答案by Jigar Joshi
byte ar[] ;
byte b = ar[0];//you have 8 bits value here,if I understood your Question correctly :)
回答by MAK
If you just need the String representation of it in binary you can simply use Integer.toString()
with the optional second parameter set to 2 for binary.
如果您只需要二进制的字符串表示,您可以简单地使用Integer.toString()
可选的第二个参数设置为 2 来表示二进制。
To perform general bit twiddling on any integral type, you have to use logical and bitshift operators.
要对任何整数类型执行一般的位处理,您必须使用逻辑和位移运算符。
// tests if bit is set in value
boolean isSet(byte value, int bit){
return (value&(1<<bit))!=0;
}
// returns a byte with the required bit set
byte set(byte value, int bit){
return value|(1<<bit);
}
回答by gimel
Java has bitwise operators
. See a tutorial example.
Java 有bitwise operators
. 请参阅教程示例。
The Java programming language also provides operators that perform bitwise and bit shift operations on integral types. The operators discussed in this section are less commonly used. Therefore, their coverage is brief; the intent is to simply make you aware that these operators exist.
The unary bitwise complement operator "~" inverts a bit pattern; it can be applied to any of the integral types, making every "0" a "1" and every "1" a "0". For example, a byte contains 8 bits; applying this operator to a value whose bit pattern is "00000000" would change its pattern to "11111111".
Java 编程语言还提供了对整数类型执行按位和位移操作的运算符。本节中讨论的运算符不太常用。因此,它们的覆盖面很简短;目的是简单地让您意识到这些运算符的存在。
一元按位补码运算符“~”反转位模式;它可以应用于任何整数类型,使每个“0”成为“1”,每个“1”成为“0”。例如,一个字节包含 8 位;将此运算符应用于位模式为“00000000”的值会将其模式更改为“11111111”。
A byte
value IS integral, you can check bit state using masking operations.
The least significant bit corresponds to the mask 1
or 0x1
, the next bit correspond to 0x2
, etc.
一个byte
价值是不可或缺,你可以使用屏蔽操作校验位的状态。最低有效位对应于掩码1
或0x1
,下一位对应于0x2
等。
byte b = 3;
if((b & 0x1) == 0x1) {
// LSB is on
} else {
// LSB is off
}
回答by Carl
You might find something along the lines of what you're looking in the Guava Primitivespackage.
您可能会在Guava Primitives包中找到与您正在查找的内容类似的内容。
Alternatively, you might want to write something like
或者,您可能想写一些类似的东西
public boolean[] convert(byte...bs) {
boolean[] result = new boolean[Byte.SIZE*bs.length];
int offset = 0;
for (byte b : bs) {
for (int i=0; i<Byte.SIZE; i++) result[i+offset] = (b >> i & 0x1) != 0x0;
offset+=Byte.SIZE;
}
return result;
}
That's not tested, but the idea is there. There are also easy modifications to the loops/assignment to return an array of something else (say, int
or long
).
这没有经过测试,但想法就在那里。还可以轻松修改循环/分配以返回其他内容的数组(例如,int
或long
)。
回答by codeplay
You may have to take a look at the source code how it's implemented if you are not using java 7
如果您不使用 java 7,您可能需要查看源代码是如何实现的
回答by Pizzamonkey
Well I think I get what you mean. Now a rather substantial error with this is that it doesn't work on negative numbers. However assuming you're not using it to read file inputs, you might still be able to use it.
好吧,我想我明白你的意思。现在一个相当大的错误是它不适用于负数。但是,假设您没有使用它来读取文件输入,您仍然可以使用它。
public static ArrayList<Boolean> toBitArr(byte[] bytearr){
ArrayList<Boolean> bitarr = new ArrayList<Boolean>();
ArrayList<Boolean> temp = new ArrayList<Boolean>();
int i = 0;
for(byte b: bytearr){
while(Math.abs(b) > 0){
temp.add((b % 2) == 1);
b = (byte) (b >> 1);
}
Collections.reverse(temp);
bitarr.addAll(temp);
temp.clear();
}
return bitarr;
}