Java Program to Implement Bit Matrix

«
»
This is a Java Program to implement Bit Matrix. Here bit matrix is implemented as an array of bit sets.

Here is the source code of the Java Program to implement Bit Matrix. 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 Matrix
  3.  **/
  4.  
  5. import java.util.Scanner;
  6. import java.util.BitSet;
  7.  
  8. /** class bit Matrix */
  9. class BitMatrix
  10. {
  11.     private BitSet[] bitArr;
  12.  
  13.     /** constructor **/
  14.     public BitMatrix(int rows, int cols)
  15.     {
  16.         bitArr = new BitSet[rows];
  17.         for (int i = 0; i < rows; i++)
  18.             bitArr[i] = new BitSet(cols);
  19.     } 
  20.     /** function to clear **/
  21.     public void clear()
  22.     {
  23.         int rows = bitArr.length;
  24.         int cols = bitArr[0].size();
  25.         bitArr = new BitSet[rows];
  26.         for (int i = 0; i < rows; i++)
  27.             bitArr[i] = new BitSet(cols);       
  28.     }
  29.     /** function to clear a particular row **/
  30.     public void clear(int r)
  31.     {
  32.         bitArr[r].clear();
  33.     }
  34.     /** function to clear a particular bit **/
  35.     public void clear(int r, int c)
  36.     {
  37.         bitArr[r].clear(c);
  38.     }    
  39.     /** function to get a particular bit **/
  40.     public boolean get(int r, int c)
  41.     {
  42.         return bitArr[r].get(c);
  43.     }    
  44.     /** function to set a particular bit **/
  45.     public void set(int r, int c)
  46.     {
  47.         bitArr[r].set(c);
  48.     }
  49.     /** function to OR two rows **/
  50.     public void or(int r1, int r2)
  51.     {
  52.         bitArr[r1].or(bitArr[r2]);
  53.     }    
  54.     /** function to And two rows **/
  55.     public void and(int r1, int r2)
  56.     {
  57.         bitArr[r1].and(bitArr[r2]);
  58.     }    
  59.     /** function to XOR two rows **/
  60.     public void xor(int r1, int r2)
  61.     {
  62.         bitArr[r1].xor(bitArr[r2]);
  63.     }       
  64.     /** function to display bitset */
  65.     public void display()
  66.     {
  67.         System.out.println("\nBit Matrix : ");
  68.         for (BitSet bs : bitArr)
  69.                System.out.println(bs);
  70.         System.out.println();
  71.     }    
  72. }
  73.  
  74. /** BitMatrixTest **/
  75. public class BitMatrixTest
  76. {
  77.     public static void main(String[] args)
  78.     {
  79.         Scanner scan = new Scanner(System.in);
  80.         System.out.println("Bit Matrix Test\n");   
  81.  
  82.         System.out.println("Enter row and column dimensions");
  83.         BitMatrix bm = new BitMatrix(scan.nextInt(), scan.nextInt() );
  84.  
  85.         char ch;
  86.         /*  Perform Bit Matrix operations */
  87.         do    
  88.         {
  89.             System.out.println("\nBit Matrix Operations\n");
  90.             System.out.println("1. or ");
  91.             System.out.println("2. and");
  92.             System.out.println("3. xor");
  93.             System.out.println("4. clear");
  94.             System.out.println("5. set");
  95.             System.out.println("6. get");            
  96.  
  97.             int choice = scan.nextInt();            
  98.             switch (choice) 
  99.             {
  100.             case 1 : 
  101.                 System.out.println("Enter row1 and row2 to OR");
  102.                 bm.or(scan.nextInt(), scan.nextInt() );                     
  103.                 break;                          
  104.             case 2 : 
  105.                 System.out.println("Enter row1 and row2 to AND");
  106.                 bm.and(scan.nextInt(), scan.nextInt() );      
  107.                 break;                                          
  108.             case 3 : 
  109.                 System.out.println("Enter row1 and row2 to XOR");
  110.                 bm.xor(scan.nextInt(), scan.nextInt() );      
  111.                 break;
  112.             case 4 : 
  113.                 System.out.println("\nBit matrix Cleared");
  114.                 bm.clear();
  115.                 break;    
  116.             case 5 : 
  117.                 System.out.println("Enter row and column to set bit");
  118.                 bm.set(scan.nextInt(), scan.nextInt() );
  119.                 break;         
  120.             case 6 : 
  121.                 System.out.println("Enter row and column to get bit status");
  122.                 System.out.println("\nStatus : "+ bm.get(scan.nextInt(), scan.nextInt()));
  123.                 break;
  124.             default : 
  125.                 System.out.println("Wrong Entry \n ");
  126.                 break;   
  127.             }    
  128.             bm.display();
  129.  
  130.             System.out.println("\nDo you want to continue (Type y or n) \n");
  131.             ch = scan.next().charAt(0);                        
  132.         } while (ch == 'Y'|| ch == 'y');         
  133.     }
  134. }

Bit Matrix Test
 
Enter row and column dimensions
4 4
 
Bit Matrix Operations
 
1. or
2. and
3. xor
4. clear
5. set
6. get
5
Enter row and column to set bit
0 24
 
Bit Matrix :
{24}
{}
{}
{}
 
 
Do you want to continue (Type y or n)
 
y
 
Bit Matrix Operations
 
1. or
2. and
3. xor
4. clear
5. set
6. get
5
Enter row and column to set bit
0 6
 
Bit Matrix :
{6, 24}
{}
{}
{}
 
 
Do you want to continue (Type y or n)
 
y
 
Bit Matrix Operations
 
1. or
2. and
3. xor
4. clear
5. set
6. get
5
Enter row and column to set bit
1 28
 
Bit Matrix :
{6, 24}
{28}
{}
{}
 
 
Do you want to continue (Type y or n)
 
y
 
Bit Matrix Operations
 
1. or
2. and
3. xor
4. clear
5. set
6. get
5
Enter row and column to set bit
1 5
 
Bit Matrix :
{6, 24}
{5, 28}
{}
{}
 
 
Do you want to continue (Type y or n)
 
y
 
Bit Matrix Operations
 
1. or
2. and
3. xor
4. clear
5. set
6. get
1
Enter row1 and row2 to OR
2 0
 
Bit Matrix :
{6, 24}
{5, 28}
{6, 24}
{}
 
 
Do you want to continue (Type y or n)
 
y
 
Bit Matrix Operations
 
1. or
2. and
3. xor
4. clear
5. set
6. get
2
Enter row1 and row2 to AND
0 3
 
Bit Matrix :
{}
{5, 28}
{6, 24}
{}
 
 
Do you want to continue (Type y or n)
 
y
 
Bit Matrix Operations
 
1. or
2. and
3. xor
4. clear
5. set
6. get
3
Enter row1 and row2 to XOR
3 2
 
Bit Matrix :
{}
{5, 28}
{6, 24}
{6, 24}
 
 
Do you want to continue (Type y or n)
 
y
 
Bit Matrix Operations
 
1. or
2. and
3. xor
4. clear
5. set
6. get
3
Enter row1 and row2 to XOR
3 1
 
Bit Matrix :
{}
{5, 28}
{6, 24}
{5, 6, 24, 28}
 
 
Do you want to continue (Type y or n)
 
y
 
Bit Matrix Operations
 
1. or
2. and
3. xor
4. clear
5. set
6. get
6
Enter row and column to get bit status
0 3
 
Status : false
 
Bit Matrix :
{}
{5, 28}
{6, 24}
{5, 6, 24, 28}
 
 
Do you want to continue (Type y or n)
 
y
 
Bit Matrix Operations
 
1. or
2. and
3. xor
4. clear
5. set
6. get
6
Enter row and column to get bit status
1 28
 
Status : true
 
Bit Matrix :
{}
{5, 28}
{6, 24}
{5, 6, 24, 28}
 
 
Do you want to continue (Type y or n)
 
y
 
Bit Matrix Operations
 
1. or
2. and
3. xor
4. clear
5. set
6. get
4
 
Bit matrix Cleared
 
Bit Matrix :
{}
{}
{}
{}
 
 
Do you want to continue (Type y or n)
 
n

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.