Java Program to Implement Bit Set

«
»
This is a Java Program to implement Bit Set. Bit Set is an array data structure used to implement a simple set data structure.

Here is the source code of the Java Program to implement Bit Set. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

  1. /**
  2.  **  Java Program to implement Bit Set
  3.  **/
  4.  
  5. import java.util.Scanner;
  6.  
  7. /** class bit set */
  8. class BitSet
  9. {
  10.     private byte[] bits;
  11.     private int bitSetSize;
  12.  
  13.     /** constructor **/
  14.     public BitSet(int size)
  15.     {
  16.         bitSetSize = size;
  17.         bits = new byte[1 + size/8];
  18.     } 
  19.  
  20.     /** constructor **/
  21.     public BitSet(BitSet bs)
  22.     {
  23.         bitSetSize = bs.bitSetSize;
  24.         bits = new byte[bs.bits.length];
  25.         System.arraycopy(bs.bits, 0, bits, 0, bs.bits.length);
  26.     }
  27.  
  28.     /** function to clear all bits **/
  29.     public void clear()
  30.     {
  31.         bits = new byte[bits.length];
  32.     }
  33.  
  34.     /** function setBit - bit to be set **/
  35.     public void setBit(int n)
  36.     {
  37.         bits[n / 8] |= (1 << (n % 8));
  38.     }
  39.  
  40.     /** function getBit **/
  41.     public boolean getBit(int n)
  42.     {
  43.         return ((bits[n / 8] & (1 << (n % 8))) != 0);
  44.     }
  45.  
  46.     /** function clearBit **/
  47.     public void clearBit(int n)
  48.     {
  49.         bits[n / 8] &= ((1 << (n % 8)) ^ ((1 << 8) - 1));
  50.     }
  51.  
  52.     /** function for or **/
  53.     public void or(BitSet set)
  54.     {
  55.         for (int i = 0; i < bits.length; i++)
  56.         {
  57.             if (i < set.bits.length)
  58.                 bits[i] |= set.bits[i];
  59.             else
  60.                 break;
  61.         }
  62.     }
  63.  
  64.     /** function for  and **/
  65.     public void and(BitSet set)
  66.     {
  67.         for (int i = 0; i < bits.length; i++)
  68.         {
  69.             if (i < set.bits.length)
  70.                 bits[i] &= set.bits[i];
  71.             else
  72.                 bits[i] = 0;
  73.         }
  74.     }
  75.  
  76.     /** function to display bitset */
  77.     public void display()
  78.     {
  79.         System.out.print("\nBit Set : ");
  80.         for (int i = 0; i < bitSetSize; i++)
  81.             if (getBit(i))
  82.                 System.out.print(i +" ");
  83.         System.out.println();
  84.     }    
  85. }
  86.  
  87. /** Class BitSetTest **/
  88. public class BitSetTest
  89. {
  90.     public static void main(String[] args)
  91.     {
  92.         Scanner scan = new Scanner(System.in);
  93.         System.out.println("Bit Set Test\n");   
  94.  
  95.         System.out.println("Enter max size of Bit Set 1");
  96.         BitSet bs1 = new BitSet(scan.nextInt() );
  97.  
  98.         char ch;
  99.         /*  Perform bitset operations  */
  100.         do    
  101.         {
  102.             System.out.println("\nBit Set Operations\n");
  103.             System.out.println("1. set bit");
  104.             System.out.println("2. get bit");
  105.             System.out.println("3. clear bit");  
  106.             System.out.println("4. clear");           
  107.  
  108.             int choice = scan.nextInt();            
  109.             switch (choice) 
  110.             {
  111.             case 1 : 
  112.                 System.out.println("Enter integer element to set");
  113.                 bs1.setBit(scan.nextInt() );                     
  114.                 break;                           
  115.             case 2 : 
  116.                 System.out.println("Enter integer element to get status");
  117.                 System.out.println("Bit Status : "+ bs1.getBit(scan.nextInt() ));         
  118.                 break;                                          
  119.             case 3 : 
  120.                 System.out.println("Enter integer element to clear");
  121.                 bs1.clearBit(scan.nextInt() );   
  122.                 break;   
  123.             case 4 : 
  124.                 System.out.println("Bit Set Cleared");
  125.                 bs1.clear();   
  126.                 break;            
  127.             default : 
  128.                 System.out.println("Wrong Entry \n ");
  129.                 break;   
  130.             }    
  131.             bs1.display();
  132.  
  133.             System.out.println("\nDo you want to continue (Type y or n) \n");
  134.             ch = scan.next().charAt(0);                        
  135.         } while (ch == 'Y'|| ch == 'y');    
  136.  
  137.  
  138.         System.out.println("\n\nEnter max size of Bit Set 2");
  139.         BitSet bs2 = new BitSet(scan.nextInt() );
  140.  
  141.         /*  Perform bitset operations  */
  142.         do    
  143.         {
  144.             System.out.println("\nBit Set Operations\n");
  145.             System.out.println("1. set bit");
  146.             System.out.println("2. get bit");
  147.             System.out.println("3. clear bit");  
  148.             System.out.println("4. clear");           
  149.  
  150.             int choice = scan.nextInt();            
  151.             switch (choice) 
  152.             {
  153.             case 1 : 
  154.                 System.out.println("Enter integer element to set");
  155.                 bs2.setBit(scan.nextInt() );                     
  156.                 break;                          
  157.             case 2 : 
  158.                 System.out.println("Enter integer element to get status");
  159.                 System.out.println("Bit Status : "+ bs2.getBit(scan.nextInt() ));         
  160.                 break;                                          
  161.             case 3 : 
  162.                 System.out.println("Enter integer element to clear");
  163.                 bs2.clearBit(scan.nextInt() );   
  164.                 break;   
  165.             case 4 : 
  166.                 System.out.println("Bit Set Cleared");
  167.                 bs2.clear();   
  168.                 break;            
  169.             default : 
  170.                 System.out.println("Wrong Entry \n ");
  171.                 break;   
  172.             }    
  173.             bs2.display();
  174.  
  175.             System.out.println("\nDo you want to continue (Type y or n) \n");
  176.             ch = scan.next().charAt(0);                        
  177.         } while (ch == 'Y'|| ch == 'y');    
  178.  
  179.  
  180.         BitSet temp = new BitSet(bs1);
  181.         temp.or(bs2);
  182.         System.out.println("\n\nBitSet1 OR BitSet2");
  183.         temp.display();
  184.  
  185.         temp = new BitSet(bs1);
  186.         temp.and(bs2);
  187.         System.out.println("\nBitSet1 AND BitSet2");
  188.         temp.display();       
  189.  
  190.     }
  191. }

advertisement
Bit Set Test
 
Enter max size of Bit Set 1
50
 
Bit Set Operations
 
1. set bit
2. get bit
3. clear bit
4. clear
1
Enter integer element to set
24
 
Bit Set : 24
 
Do you want to continue (Type y or n)
 
y
 
Bit Set Operations
 
1. set bit
2. get bit
3. clear bit
4. clear
1
Enter integer element to set
6
 
Bit Set : 6 24
 
Do you want to continue (Type y or n)
 
y
 
Bit Set Operations
 
1. set bit
2. get bit
3. clear bit
4. clear
1
Enter integer element to set
28
 
Bit Set : 6 24 28
 
Do you want to continue (Type y or n)
 
y
 
Bit Set Operations
 
1. set bit
2. get bit
3. clear bit
4. clear
1
Enter integer element to set
5
 
Bit Set : 5 6 24 28
 
Do you want to continue (Type y or n)
 
y
 
Bit Set Operations
 
1. set bit
2. get bit
3. clear bit
4. clear
1
Enter integer element to set
14
 
Bit Set : 5 6 14 24 28
 
Do you want to continue (Type y or n)
 
y
 
Bit Set Operations
 
1. set bit
2. get bit
3. clear bit
4. clear
2
Enter integer element to get status
14
Bit Status : true
 
Bit Set : 5 6 14 24 28
 
Do you want to continue (Type y or n)
 
y
 
Bit Set Operations
 
1. set bit
2. get bit
3. clear bit
4. clear
3
Enter integer element to clear
14
 
Bit Set : 5 6 24 28
 
Do you want to continue (Type y or n)
 
n
 
 
Enter max size of Bit Set 2
50
 
Bit Set Operations
 
1. set bit
2. get bit
3. clear bit
4. clear
1
Enter integer element to set
14
 
Bit Set : 14
 
Do you want to continue (Type y or n)
 
y
 
Bit Set Operations
 
1. set bit
2. get bit
3. clear bit
4. clear
1
Enter integer element to set
7
 
Bit Set : 7 14
 
Do you want to continue (Type y or n)
 
y
 
Bit Set Operations
 
1. set bit
2. get bit
3. clear bit
4. clear
1
Enter integer element to set
1
 
Bit Set : 1 7 14
 
Do you want to continue (Type y or n)
 
y
 
Bit Set Operations
 
1. set bit
2. get bit
3. clear bit
4. clear
1
Enter integer element to set
28
 
Bit Set : 1 7 14 28
 
Do you want to continue (Type y or n)
 
y
 
Bit Set Operations
 
1. set bit
2. get bit
3. clear bit
4. clear
1
Enter integer element to set
5
 
Bit Set : 1 5 7 14 28
 
Do you want to continue (Type y or n)
 
n
 
 
BitSet1 OR BitSet2
 
Bit Set : 1 5 6 7 14 24 28
 
BitSet1 AND BitSet2
 
Bit Set : 5 28

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

Note: Join free Sanfoundry classes at Telegram or Youtube
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.