[英]Convert a byte into a boolean array of length 4 in Java
I need to convert a byte into an array of 4 booleans in Java.我需要在 Java 中将一个字节转换为一个包含 4 个布尔值的数组。 How might I go about this?我该怎么办?
Per Michael Petrotta's comment to your question, you need to decide which bits in the 8-bit byte should be tested for the resulting boolean array.根据 Michael Petrotta 对您的问题的评论,您需要决定应针对生成的布尔数组测试 8 位字节中的哪些位。 For demonstration purposes, let's assume you want the four rightmost bits, then something like this should work:出于演示目的,假设您想要最右边的四个位,那么这样的事情应该可以工作:
public static boolean[] booleanArrayFromByte(byte x) {
boolean bs[] = new boolean[4];
bs[0] = ((x & 0x01) != 0);
bs[1] = ((x & 0x02) != 0);
bs[2] = ((x & 0x04) != 0);
bs[3] = ((x & 0x08) != 0);
return bs;
}
The hexadecimal values ( 0x01
, 0x02
, etc.) in this example are special bit masks that have only a single bit set at the desired location;此示例中的十六进制值( 0x01
、 0x02
等)是特殊位掩码,在所需位置仅设置了一个位; so 0x01 has only the rightmost bit set, 0x08 has only the fourth-from-right bit set.所以 0x01 只设置了最右边的位,0x08 只设置了从右数第四个位。 By testing the given byte against these values with the bitwise AND operator ( &
) you will get that value back if the bit is set, or zero if not.通过使用按位 AND 运算符 ( &
) 针对这些值测试给定的字节,如果设置了该位,您将获得该值,否则将获得零。 If you want to check different bits, other than the rightmost four, then you'll have to create different bitmasks.如果你想检查不同的位,而不是最右边的四个,那么你必须创建不同的位掩码。
Others are raising a very valid point: in Java, Byte.SIZE == 8
.其他人提出了一个非常有效的观点:在 Java 中, Byte.SIZE == 8
。 That is, there are 8 bits in a byte
.也就是说,一个byte
有 8 位。 You need to define how you want to map 8 bits into 4 boolean
values;您需要定义如何将 8 位映射到 4 个boolean
值; otherwise we can only guess what is it you're trying to do.否则我们只能猜测你想做什么。
BitSet
BitSet
Regardless of how you do this mapping, however, it's unlikely that boolean[]
really is the best representation.然而,无论您如何进行此映射, boolean[]
不太可能真的是最好的表示。 A java.util.BitSet
may be better. java.util.BitSet
可能更好。 Here's an example:下面是一个例子:
import java.util.*;
public class BitSetExample {
static BitSet toBitSet(byte b) {
BitSet bs = new BitSet(Byte.SIZE);
for (int i = 0; i < Byte.SIZE; i++) {
if (((b >> i) & 1) == 1) {
bs.set(i);
}
}
return bs;
}
public static void main(String[] args) {
BitSet bs = toBitSet((byte) 10);
System.out.println(bs); // prints "{1, 3}"
System.out.println(bs.get(3)); // prints "true"
System.out.println(bs.get(2)); // prints "false"
byte b = 25;
System.out.println(toBitSet(b)); // prints "{0, 3, 4}"
bs.or(toBitSet(b));
System.out.println(bs); // prints "{0, 1, 3, 4}"
}
}
The above code uses the standard bit probing technique to convert a byte
to a BitSet
.上面的代码使用标准位探测技术将byte
转换为BitSet
。 Note that a (byte) 10
has its bits 1 and 3 set (ie 10 = 2^1 + 2^3
where ^
denotes exponentiation).请注意, (byte) 10
第 1 位和第 3 位已设置(即10 = 2^1 + 2^3
,其中^
表示求幂)。
The example also shows how to perform an or
/set union operation on BitSet
.该示例还显示了如何对BitSet
执行or
/set union 操作。
EnumSet
在EnumSet
Possibly another applicable data structure is an EnumSet
, which is a Set
implementation highly optimized for enum
.可能另一个适用的数据结构是EnumSet
,它是为enum
高度优化的Set
实现。 Here's an example:下面是一个例子:
import java.util.*;
public class EnumSetExample {
enum Style {
BOLD, ITALIC, UNDERLINE, BLINKING;
}
public static void main(String[] args) {
EnumSet<Style> myStyle = EnumSet.of(Style.BOLD, Style.UNDERLINE);
System.out.println(myStyle);
// prints "[BOLD, UNDERLINE]"
System.out.println(myStyle.contains(Style.UNDERLINE));
// prints "true"
System.out.println(myStyle.contains(Style.BLINKING));
// prints "false" (thank goodness!)
myStyle.add(Style.ITALIC);
System.out.println(myStyle);
// prints "[BOLD, ITALIC, UNDERLINE]"
}
}
EnumSet
instead of bit fields Effective Java 2nd Edition,Item 32:使用EnumSet
而不是位域As an addendum to maerics' answer, this is how you could convert the bool array back into a byte, if needed :作为 maerics 答案的附录,如果需要,这是将 bool 数组转换回字节的方法:
public static byte byteFromBooleanArray(bool[] _boolArray)
{
byte x = 0;
x += _boolArray[0] ? (byte)1 : (byte)0;
x += _boolArray[1] ? (byte)2 : (byte)0;
x += _boolArray[2] ? (byte)4 : (byte)0;
x += _boolArray[3] ? (byte)8 : (byte)0;
return x;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.