Java BitSet Example

I am looking for a good Java BitSet example to work with 0 and 1. I tried to take a look at Javadocs, but I donโ€™t understand how to use this class, just by reading this. For example, how do the and , or and xor methods work on two different BitSet objects?

For example:

  BitSet bits1 = new BitSet(); BitSet bits2 = new BitSet(); bits2.set(1000001); bits1.set(1111111); bits2.and(bits1); System.out.println(bits2); 

If I do this, it returns bits2 as empty, why?

+46
java bitset
Feb 17 '12 at 18:45
source share
6 answers

For the specific problem you were talking about: when you called bits2.set(1000001) , you set the millionth and first bit to true. Then, when you crossed paths with bits1 , in which one million, 111 thousand and 111-bit bits were set, they had no bits.

I think what you wanted to do is

  bits2.set(0); // set the 0th bit bits2.set(6); // set the 6th bit 

Does this help clarify the situation?

+92
Feb 17 '12 at 7:21
source share

If you want to work with bits, you can use int values โ€‹โ€‹in Java 7.

 int bits2 = 0b1000001; int bits1 = 0b1111111; bits2 &= bits1; System.out.println(Integer.toBinaryString(bits2)); 

prints

 1000001 
+50
Feb 17 2018-12-17T00:
source share

BitSet does not have convenient methods for receiving strings of such bits. I cited a few below, and now the example works as you expected. Note that this uses new features in Java 7; It's easy to find implementations of these methods on the Internet if you want to use Java 6.

 import java.util.BitSet; class Scratch { public static void main(String[] args) { BitSet bits1 = fromString("1000001"); BitSet bits2 = fromString("1111111"); System.out.println(toString(bits1)); // prints 1000001 System.out.println(toString(bits2)); // prints 1111111 bits2.and(bits1); System.out.println(toString(bits2)); // prints 1000001 } private static BitSet fromString(final String s) { return BitSet.valueOf(new long[] { Long.parseLong(s, 2) }); } private static String toString(BitSet bs) { return Long.toString(bs.toLongArray()[0], 2); } } 
+39
Feb 17 2018-12-17T00:
source share

Here are some links about bitSet that will help you:

UPDATE:

The docs say:

public void set (int bitIndex)

 Sets the bit at the specified index to true. 

Therefore, when you call bits2.set(10); , it is considered 10 decimal not 1 0 , so you get the following number 1000000000 .

To set it correctly, in this example I want to set the second bit to 1, so I call bits2.set(1); because the index starts at 0 .

In conclusion , for each bit set to 1, you need to call bitSet.Set and provide it with the bit index.

+8
Feb 17 '12 at 18:52
source share

I use my implementation to create a BitSet object using a bit string as input.

 private static BitSet createFromString(String s) { BitSet t = new BitSet(s.length()); int lastBitIndex = s.length() - 1; for (int i = lastBitIndex; i >= 0; i--) { if ( s.charAt(i) == '1'){ t.set(lastBitIndex - i); } } return t; } 

To enter the string "1001"

 BitSet s1 = createFromString("1001"); System.out.println(s1); 

output:

 {0, 3} 
+4
Sep 20 '13 at 20:48
source share

Try the following:

 import java.util.BitSet; public class BitSetExample { public static void main(String args[]){ BitSet bits1 = new BitSet(7); BitSet bits2 = new BitSet(7); // set some bits for(int i = 0; i < 7; i++) { if((i % 2) == 0) bits1.set(i); if((i % 3) != 0) bits2.set(i); } System.out.println("BitSet1: "); for(int i = 0; i < 7; i++) { System.out.print(bits1.get(i)? "1 ": "0 "); } System.out.println("\nBitSet2: "); for(int i = 0; i < 7; i++) { System.out.print(bits2.get(i)? "1 ": "0 "); } System.out.println(); //And bits1.and(bits2); System.out.println("b1 = b1 AND b2\nBitSet1: "); for(int i = 0; i < 7; i++) { System.out.print(bits1.get(i)? "1 ": "0 "); } System.out.println(); System.out.println("BitSet2: "); for(int i = 0; i < 7; i++) { System.out.print(bits2.get(i)? "1 ": "0 "); } System.out.println(); //Or bits1.or(bits2); System.out.println("b1 = b1 OR b2\nBitSet1: "); for(int i = 0; i < 7; i++) { System.out.print(bits1.get(i)? "1 ": "0 "); } System.out.println(); System.out.println("BitSet2: "); for(int i = 0; i < 7; i++) { System.out.print(bits2.get(i)? "1 ": "0 "); } System.out.println(); //Xor bits1.xor(bits2); System.out.println("b1 = b1 XOR b2\nBitSet1: "); for(int i = 0; i < 7; i++) { System.out.print(bits1.get(i)? "1 ": "0 "); } System.out.println(); System.out.println("BitSet2: "); for(int i = 0; i < 7; i++) { System.out.print(bits2.get(i)? "1 ": "0 "); } System.out.println(); //Setting bits to zero and one bits1.set(1); bits2.set(1,false); System.out.println("set bit 1 of BitSet1 to one and set bit 1 of BitSet2 to zero\nBitSet1: "); for(int i = 0; i < 7; i++) { System.out.print(bits1.get(i)? "1 ": "0 "); } System.out.println(); System.out.println("BitSet2: "); for(int i = 0; i < 7; i++) { System.out.print(bits2.get(i)? "1 ": "0 "); } System.out.println(); } } 

Hope this is helpful. For more information, visit: https://github.com/m-vahidalizadeh/foundations/blob/master/src/data_structures/BitSetExample.java .

0
Sep 19 '16 at 20:36
source share



All Articles