开发者

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. How migh开发者_Python百科t 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. 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; so 0x01 has only the rightmost bit set, 0x08 has only the fourth-from-right bit set. 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. If you want to check different bits, other than the rightmost four, then you'll have to create different bitmasks.


On specification

Others are raising a very valid point: in Java, Byte.SIZE == 8. That is, there are 8 bits in a byte. You need to define how you want to map 8 bits into 4 boolean values; otherwise we can only guess what is it you're trying to do.


On BitSet

Regardless of how you do this mapping, however, it's unlikely that boolean[] really is the best representation. A java.util.BitSet may be better. 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. Note that a (byte) 10 has its bits 1 and 3 set (i.e. 10 = 2^1 + 2^3 where ^ denotes exponentiation).

The example also shows how to perform an or/set union operation on BitSet.


On EnumSet

Possibly another applicable data structure is an EnumSet, which is a Set implementation highly optimized for enum. 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]"
    }
}

See also

  • Effective Java 2nd Edition, Item 32: Use EnumSet instead of bit fields


As an addendum to maerics' answer, this is how you could convert the bool array back into a byte, if needed :

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;
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜