Java Program to Implement Bit Array

«
»
This Java program is to Implement Bit Array. A bit array (also known as bitmap, bitset, bit string, or bit vector) is an array data structure that compactly stores bits. It can be used to implement a simple set data structure. A bit array is effective at exploiting bit-level parallelism in hardware to perform operations quickly.

Here is the source code of the Java program to implement bit array. The Java program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. import java.util.BitSet;
  2.  
  3. public class BitArray
  4. {
  5.     private BitSet bits;
  6.  
  7.     public BitArray(String bits)
  8.     {
  9.         this.bits = fromString(bits);
  10.     }
  11.  
  12.     private BitSet getBitSet()
  13.     {
  14.         return bits;
  15.     }
  16.  
  17.     private void setBitSet(BitSet bitSet )
  18.     {
  19.         bits = bitSet;
  20.     }
  21.  
  22.     public BitArray and(BitArray bitarray)
  23.     {
  24.         BitSet bits1 = this.getBitSet();
  25.         BitSet bits2 = bitarray.getBitSet();
  26.  
  27.         bits1.and(bits2);
  28.         this.setBitSet(bits1);
  29.         return this;
  30.     }
  31.  
  32.     public BitArray or(BitArray bitarray)
  33.     {
  34.         BitSet bits1 = this.getBitSet();
  35.         BitSet bits2 = bitarray.getBitSet();
  36.         bits1.or(bits2);
  37.         this.setBitSet(bits1);
  38.         return this;
  39.     }
  40.  
  41.     private BitSet fromString(String bit)
  42.     {
  43.         return BitSet.valueOf(new long[] { Long.parseLong(bit, 2) });
  44.     }
  45.  
  46.     public String toString()
  47.     {
  48.         return Long.toString(bits.toLongArray()[0], 2);
  49.     }
  50.  
  51.     public static void main (String...arg)
  52.     {
  53.         BitArray array1 = new BitArray("1010");
  54.         BitArray array2 = new BitArray("1001");
  55.         BitArray array3 = new BitArray("1100");
  56.  
  57.         System.out.println("The BitArray Are");
  58.         System.out.println("First :" + array1);
  59.         System.out.println("Second :" +array2);
  60.         System.out.println("Third : " + array3);
  61.  
  62.         System.out.println("First AND Second");
  63.         System.out.println(array1.and(array2));
  64.  
  65.         System.out.println("Second OR Third");
  66.         System.out.println(array2.or(array3));
  67.     }	
  68. }


advertisement
$javac BitArray.java
$java BitArray
 
The BitArray Are
First :1010
Second :1001
Third : 1100
First AND Second
1000
Second OR Third
1101

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
advertisement
advertisement
If you wish to look at all Java Programming examples, go to Java Programs.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.