Does BitSet in java stores bits or integers? - java

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

Related

Translating this java implementation of Sieve of Eratosthenes?

This is a program in Java which implements the Sieve or Eratosthenes by storing the array of booleans as an array of bits. I have never coded in Java before, but the general idea is easy to understand. However, I cannot understand how the getBit and setBit functions work? I am guessing that the getBit function creates a bitmask with the bit i set to 1 and does bitwise AND between the mask and the array? However, I'm not really understanding the details (eg. why i is right shifted by 4 before being passed as index to array, and why MEMORY_SIZE is equal to MAX right shifted by 4). Please explain the each step of getBit and setBit in words, and if possible an equivalent implementation in Python?
private static final long MAX = 1000000000L;
private static final long SQRT_MAX = (long) Math.sqrt(MAX) + 1;
private static final int MEMORY_SIZE = (int) (MAX >> 4);
private static byte[] array = new byte[MEMORY_SIZE];
//--//
for (long i = 3; i < SQRT_MAX; i += 2) {
if (!getBit(i)) {
long j = (i * i);
while (j < MAX) {
setBit(j);
j += (2 * i);
}
}
}
//--//
public static boolean getBit(long i) {
byte block = array[(int) (i >> 4)];
byte mask = (byte) (1 << ((i >> 1) & 7));
return ((block & mask) != 0);
}
public static void setBit(long i) {
int index = (int) (i >> 4);
byte block = array[index];
byte mask = (byte) (1 << ((i >> 1) & 7));
array[index] = (byte) (block | mask);
}
Some notes in advance:
(i >> 4) divides i by 16, which is the index of the block (of 8 bits) in array that contains the i-th bit
(i >> 1) divides i by 2
7 in binary code is 111
((i >> 1) & 7) means "the three rightmost bits of i / 2", which is a number between 0 and 7 (inclusive)
(1 << ((i >> 1) & 7)) is a bit shifted to the left between 0 and 7 times (00000001, 00000010, ..., 10000000). This is the bit mask to set/get the bit of interest from the selected block.
getBit(i) explained
First line selects the 8-bit-block (i.e. a byte) in which the bit of interest is located.
Second line calculates a bit mask with exactly one bit set. The position of the set bit is the same as the one of the bit of interest within the 8-bit-block.
Third line extracts the bit of interest using an bitwise AND, returning true if this bit is 1.
setBit(i) explained
Calculation of the 8-bit-block and the bit mask is equivalent to getBit
The difference is that a bitwise OR is used to set the bit of interest.
Edit
To your first question:
It almost makes sense now, can you please explain why we are able to find the position of the bit cooresponding to the number i by shifting a bit left ((i >> 1) & 7) times? In other words, i understand what the operation is doing, but why does this give us the correct bit position?
I think this is because of the optimized nature of the algorithm. Since i is incremented in steps of 2, it is sufficient to use half of the bits (since the others would be set anyway). Thus, i can be divided by 2 to calculate the number of necessary bit shifts.
Regarding your second question:
Also, just to clarify, the reason we increment j by 2*i after each call to setBit is because we only need to set the bits cooresponding to odd multiples of i, right?
Yes, because according to https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes:
Another refinement is to initially list odd numbers only, (3, 5, ..., n), and count in increments of 2p in step 3, thus marking only odd multiples of p.
Your algorithm starts with 3, increments i by 2, and counts in increments of 2*i.
I hope this helps!

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 add padding on to a byte array?

I have this 40 bit key in a byteArray of size 8, and I want to add 0 padding to it until it becomes 56 bit.
byte[] aKey = new byte [8]; // How I instantiated my byte array
Any ideas how?
An 8 byte array is of 64 bits. If you initialize the array as
byte[] aKey = new byte [8]
all bytes are initialized with 0's. If you set the first 40 bits, that is 5 bytes, then your other 3 bytes, i.e, from 41 to 64 bits are still set to 0. So, you have by default from 41st bit to 56th bit set to 0 and you don't have to reset them.
However, if your array is already initialized with some values and you want to clear the bits from 41 to 56, there are a few ways to do that.
First:
you can just set aKey[5] = 0 and aKey[6] = 0 This will set the 6th bye and the 7th byte, which make up from 41st to 56th bit, to 0
Second: If you are dealing with bits, you can also use BitSet. However, in your case, I see first approach much easier, especially, if you are pre Java 7, some of the below methods do not exist and you have to write your own methods to convert from byte array to bit set and vice-versa.
byte[] b = new byte[8];
BitSet bitSet = BitSet.valueOf(b);
bitSet.clear(41, 56); //This will clear 41st to 56th Bit
b = bitSet.toByteArray();
Note: BitSet.valueOf(byte[]) and BitSet.toByteArray() exists only from Java 7.
Use System.arraycopy() to insert two bytes (56-40 = 16 bit) at the start of your array.
static final int PADDING_SIZE = 2;
public static void main(String[] args) {
byte[] aKey = {1, 2, 3, 4, 5, 6, 7, 8}; // your array of size 8
System.out.println(Arrays.toString(aKey));
byte[] newKey = new byte[8];
System.arraycopy(aKey, 0, newKey, PADDING_SIZE, aKey.length - PADDING_SIZE); // right shift
System.out.println(Arrays.toString(newKey));
}
Guava's com.google.common.primitives.Bytes.ensureCapacity:
aKey = Bytes.ensureCapacity(aKey , 56/8, 0);
or since JDK6 using Java native tools:
aKey = java.util.Arrays.copyOf(aKey , 56/8);

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

Categories

Resources