Convert a byte into a boolean array of length 4 in Java - java

I need to convert a byte into an array of 4 booleans in Java. 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. 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;
}

Related

Does BitSet in java stores bits or integers?

I came across many coding sites about bitset. But i cant understand whether it stores bits or integers.
BitSet creates an array of bits represented by boolean values.
import java.util.*;
public class GFG
{
public static void main(String[] args)
{
BitSet bs1 = new BitSet();
BitSet bs2 = new BitSet(6);
bs1.set(0);
bs1.set(1);
bs1.set(2);
bs1.set(4);
bs2.set(4);
bs2.set(6);
bs2.set(5);
bs2.set(1);
bs2.set(2);
bs2.set(3);
System.out.println("bs1 : " + bs1);
System.out.println("bs2 : " + bs2);
}
}
Output:
bs1 : {0, 1, 2, 4}
bs2 : {1, 2, 3, 4, 5, 6}
BitSet stores bits or integers?
How does it stores that in memory?
How the values change when any manipulation is done?
Typically BitSet would be implemented using a long[]. Each long stores 64 consecutive possible bit positions. The array needs a size equal to the highest set bit index minus one (to allow for index 0), divided by 64 (rounded down). Set bits are represented as a binary 1 and bits present in the array but not set as a binary 0.
So the internal representation of your examples would be something like:
bs1 = new long[] { 0b00010111L }; // 23
bs2 = new long[] { 0b01111110L }; // 126
// bit indexes: 76543210
(Bits 8-63 elided from constants - add all the zeros if your want.)
The BitSet stores bits using an array of longs:
private long[] bits;
Manipulating this means you manipulate bits of those longs using bitwise operations
and shifts
public void set(int pos)
{
int offset = pos >> 6; // divide by 2^6 = 64
ensure(offset); // if needed extend array
// ArrayIndexOutOfBoundsException subclasses IndexOutOfBoundsException,
// so we'll just let that be our exception.
bits[offset] |= 1L << pos; // set bit using OR and a shift
}
Some illustration of whats going on for 6 bits (index 0-5):
init 000000
set 3:
000000
OR 001000 = 1 << 3
= 001000
set 5:
001000
OR 100000 = 1 << 5
= 101000
This means you take all bits of the current bitmask and the newly set bit of the desired offset to calculate the new bitmask.
Source Code

Difference between output of bitwise operator

For some reason, the following two bitwise operations provide different results yet it seems intuitive that they should provide the same result since the masks used should be the same. What am I missing here? Why would the results of using the two masks vary?
public class BitShiftTest {
private long bitString = -8784238533840732024L ;
private final int MAX_BITS_POSITION = 63 ;
public static void main(String[] args) {
BitShiftTest bst = new BitShiftTest() ;
System.out.printf("Before applying mask: %s\n", Long.toBinaryString(bst.bitString));
System.out.printf("Using Mask 1: %s\n", Long.toBinaryString(bst.clearBitMask(60)));
System.out.printf("Using Mask 2: %s\n", Long.toBinaryString(bst.clearBitMaskAlternative(60)));
}
public long clearBitMask(int position) {
return bitString & ~(1 << position) ;
}
public long clearBitMaskAlternative(int position) {
return bitString & (0x8000000000000000L >>> MAX_BITS_POSITION - position) ;
}
}
The results produced are
Before applying mask: 1000011000011000000111011001100000101000001000000000000010001000
Using Mask 1: 1000011000011000000111011001100000101000001000000000000010001000
Using Mask 2: 0
You are assuming that ~(1<<position) and 0x8000000000000000L >>> MAX_BITS_POSITION - position are equal, but that's not true.
Basically, you're missing a ~ in the alternative case - otherwise it looks as if you're just trying to extract one bit, not just clear it. It should be:
~(0x8000000000000000L >>> MAX_BITS_POSITION - position)
But also, as noted by #RolandIllig 1 << position is actually done in int arithmetic. You're not seeing this in your output because neither the 60th nor 28th bits happen to be set in the long you are masking. 1L << position fixes this.
1 << 63 is of type int and is therefore equal to Integer.MIN_VALUE, since the 1 is shifted out of the number. (In other languages the result would be undefined since you shift by more bits than an int has.
To fix this, use 1L << 63, which operates on long.

How can I get the position of bits

I have a decimal number which I need to convert to binary and then find the position of one's in that binary representation.
Input is 5 whose binary is 101 and Output should be
1
3
Below is my code which only provides output as 2 instead I want to provide the position of one's in binary representation. How can I also get position of set bits starting from 1?
public static void main(String args[]) throws Exception {
System.out.println(countBits(5));
}
private static int countBits(int number) {
boolean flag = false;
if (number < 0) {
flag = true;
number = ~number;
}
int result = 0;
while (number != 0) {
result += number & 1;
number = number >> 1;
}
return flag ? (32 - result) : result;
}
Your idea of having countBits return the result, instead of putting a System.out.println inside the method, is generally the best approach. If you want it to return a list of bit positions, the analogue would be to have your method return an array or some kind of List, like:
private static List<Integer> bitPositions(int number) {
As I mentioned in my comments, you will make life a lot easier for yourself if you use >>> and get rid of the special code to check for negatives. Doing this, and adapting the code you already have, gives you something like
private static List<Integer> bitPositions(int number) {
List<Integer> positions = new ArrayList<>();
int position = 1;
while (number != 0) {
if (number & 1 != 0) {
positions.add(position);
}
position++;
number = number >>> 1;
}
return positions;
}
Now the caller can do what it wants to print the positions out. If you use System.out.println on it, the output will be [1, 3]. If you want each output on a separate line:
for (Integer position : bitPositions(5)) {
System.out.println(position);
}
In any case, the decision about how to print the positions (or whatever else you want to do with them) is kept separate from the logic that computes the positions, because the method returns the whole list and doesn't have its own println.
(By the way, as Alex said, it's most common to think of the lower-order bit as "bit 0" instead of "bit 1", although I've seen hardware manuals that call the low-order bit "bit 31" and the high-order bit "bit 0". The advantage of calling it "bit 0" is that a 1 bit in position N represents the value 2N, making things simple. My code example calls it "bit 1" as you requested in your question; but if you want to change it to 0, just change the initial value of position.)
Binary representation: Your number, like anything on a modern day (non-quantum) computer, is already a binary representation in memory, as a sequence of bits of a given size.
Bit operations
You can use bit shifting, bit masking, 'AND', 'OR', 'NOT' and 'XOR' bitwise operations to manipulate them and get information about them on the level of individual bits.
Your example
For your example number of 5 (101) you mentioned that your expected output would be 1, 3. This is a bit odd, because generally speaking one would start counting at 0, e.g. for 5 as a byte (8 bit number):
76543210 <-- bit index
5 00000101
So I would expect the output to be 0 and 2 because the bits at those bit indexes are set (1).
Your sample implementation shows the code for the function
private static int countBits(int number)
Its name and signature imply the following behavior for any implementation:
It takes an integer value number and returns a single output value.
It is intended to count how many bits are set in the input number.
I.e. it does not match at all with what you described as your intended functionality.
A solution
You can solve your problem using a combination of a 'bit shift' (>>) and an AND (&) operation.
int index = 0; // start at bit index 0
while (inputNumber != 0) { // If the number is 0, no bits are set
// check if the bit at the current index 0 is set
if ((inputNumber & 1) == 1)
System.out.println(index); // it is, print its bit index.
// advance to the next bit position to check
inputNumber = inputNumber >> 1; // shift all bits one position to the right
index = index + 1; // so we are now looking at the next index.
}
If we were to run this for your example input number '5', we would see the following:
iteration input 76543210 index result
1 5 00000101 0 1 => bit set.
2 2 00000010 1 0 => bit not set.
3 1 00000001 2 1 => bit set.
4 0 00000000 3 Stop, because inputNumber is 0
You'll need to keep track of what position you're on, and when number & 1 results in 1, print out that position. It look something like:
...
int position = 1;
while (number != 0) {
if((number & 1)==1)
System.out.println(position);
result += number & 1;
position += 1;
number = number >> 1;
}
...
There is a way around working with bit-wise operations to solve your problem.
Integer.toBinaryString(int number) converts an integer to a String composed of zeros and ones. This is handy in your case because you could instead have:
public static void main(String args[]) throws Exception {
countBits(5);
}
public static void countBits(int x) {
String binaryStr = Integer.toBinaryString(x);
int length = binaryStr.length();
for(int i=0; i<length; i++) {
if(binaryStr.charAt(i)=='1')
System.out.println(length-1);
}
}
It bypasses what you might be trying to do (learn bitwise operations in Java), but makes the code look cleaner in my opinion.
The combination of Integer.lowestOneBit and Integer.numberOfTrailingZeros instantly gives the position of the lowest 1-Bit, and returns 32 iff the number is 0.
Therefore, the following code returns the positions of 1-Bits of the number number in ascending order:
public static List<Integer> BitOccurencesAscending(int number)
{
LinkedList<Integer> out = new LinkedList<>();
int x = number;
while(number>0)
{
x = Integer.lowestOneBit(number);
number -= x;
x = Integer.numberOfTrailingZeros(x);
out.add(x);
}
return out;
}

What is the fastest way to associate a boolean flag to every possible integer value?

If I had a byte instead of an integer, I could easily create a boolean array with 256 positions and check:
boolean[] allBytes = new boolean[256];
if (allBytes[value & 0xFF] == true) {
// ...
}
Because I have an integer, I can't have an array with size 2 billion. What is the fastest way to check if an integer is true or false? A set of Integers? A hashtable?
EDIT1: I want to associate for every possible integer (2 billion) a true or false flag.
EDIT2: I have ID X (integer) and I need a quick way to know if ID X is ON or OFF.
A BitSet can't handle negative numbers. But there's a simple way around:
class BigBitSet {
private final BitSet[] bitSets = new BitSet[] {new BitSet(), new BitSet()};
public boolean get(int bitIndex) {
return bitIndex < 0 ? bitSets[1].get(~bitIndex)
: bitSets[0].get(bitIndex);
}
...
}
The second BitSet is for negative numbers, which get translated via the '~' operator (that's better than simply negating as it works for Integer.MIN_VALUE, too).
The memory consumption may get up to 4 Gib, i.e., about 524 MB.
I feel stupid for even elaborating on this.
The smallest unit of information your computer can store is a bit, right? A bit has two states, you want two states, so lets just say bit=0 is false and bit=1 is true.
So you need as many bits as there are possible int's, 2^32 = 4,294,967,296. You can fit 8 bits into a byte, so you need only 2^32 / 8 = 536,870,912 bytes.
From that easily follows code to address each of these bits in the bytes...
byte[] store = new byte[1 << 29]; // 2^29 bytes provide 2^32 bits
void setBit(int i) {
int byteIndex = i >>> 3;
int bitMask = 1 << (i & 7);
store[byteIndex] |= bitMask;
}
boolean testBit(int i) {
int byteIndex = i >>> 3;
int bitMask = 1 << (i & 7);
return (store[byteIndex] & bitMask) != 0;
}
java.util.BitSet provides practically the same premade in a nice class, only you can use it to store a maximum of 2^31 bits since it does not work with negative bit indices.
Since you're using Java, use BitSet. It's fast and easy. If you prefer, you could also use an array of primitive longs or BigInteger, but this is really what BitSet is for.
http://docs.oracle.com/javase/7/docs/api/java/util/BitSet.html

How to convert a byte into bits?

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?
Thank you.
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.
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);
}
You might find something along the lines of what you're looking in the Guava Primitives package.
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).
BitSet.valueOf(byte[] bytes)
You may have to take a look at the source code how it's implemented if you are not using java 7
Java has bitwise operators. See a tutorial example.
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".
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 b = 3;
if((b & 0x1) == 0x1) {
// LSB is on
} else {
// LSB is off
}
byte ar[] ;
byte b = ar[0];//you have 8 bits value here,if I understood your Question correctly :)
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;
}
Here is a sample, I hope it is useful for you!
DatagramSocket socket = new DatagramSocket(6160, InetAddress.getByName("0.0.0.0"));
socket.setBroadcast(true);
while (true) {
byte[] recvBuf = new byte[26];
DatagramPacket packet = new DatagramPacket(recvBuf, recvBuf.length);
socket.receive(packet);
String bitArray = toBitArray(recvBuf);
System.out.println(Integer.parseInt(bitArray.substring(0, 8), 2)); // convert first byte binary to decimal
System.out.println(Integer.parseInt(bitArray.substring(8, 16), 2)); // convert second byte binary to decimal
}
public static String toBitArray(byte[] byteArray) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < byteArray.length; i++) {
sb.append(String.format("%8s", Integer.toBinaryString(byteArray[i] & 0xFF)).replace(' ', '0'));
}
return sb.toString();
}

Categories

Resources