Sunday 31 May 2015

Printing the truth tables of Binary Operators : AND, OR, NOR, XOR, NAND, NXOR, using simple java code :)

Hello friends,

I want to discuss the program to print the Truth Table of the binary Operators. Binary operator works on two bits and give result in binary. Also do note how I have used an abstract method in an enum that I have defined in my program and also have overridden the same :)


 
 public class TruthTableOfBinaryOperators {  
      public enum Operator {  
           AND {  
                @Override  
                public String getOperatorName() {  
                     return "AND";  
                }  
           },  
           OR {  
                @Override  
                public String getOperatorName() {  
                     return "OR";  
                }  
           },  
           NOR {  
                @Override  
                public String getOperatorName() {  
                     return "NOR";  
                }  
           },  
           XOR {  
                @Override  
                public String getOperatorName() {  
                     return "XOR";  
                }  
           },  
           NXOR {  
                @Override  
                public String getOperatorName() {  
                     return "NXOR";  
                }  
           },  
           NAND {  
                @Override  
                public String getOperatorName() {  
                     return "NAND";  
                }  
           };  
           abstract public String getOperatorName();  
      };  
      public static void printTruthTable(int N, Operator op) {  
           int totalSubSets = (1 << N);  
           for (int i = 0; i < totalSubSets ; i++) {  
                int result = -1;  
                for (int j = N - 1; j >= 0; j--) {  
                     int currentBit;  
                     if ((i & (1 << j)) != 0) {  
                          currentBit = 1;  
                     } else {  
                          currentBit = 0;  
                     }  
                     if (result == -1) {  
                          result = currentBit;  
                     } else {  
                          switch(op){  
                          case AND:  
                               if(result != 0){  
                                    result = currentBit & result;  
                               }  
                               break;  
                          case NAND:  
                               if((currentBit & result)==0){  
                                    result = 0;  
                               }else{  
                                    result = 1;  
                               }  
                               break;  
                          case OR:  
                               if(result != 1){  
                                    result = currentBit | result;  
                               }  
                               break;  
                          case NOR:  
                               if((currentBit | result)==0){  
                                    result = 0;  
                               }else{  
                                    result = 1;  
                               }  
                               break;  
                          case XOR:  
                                    result = currentBit ^ result;  
                               break;  
                          case NXOR:  
                               if((currentBit ^ result) == 0){  
                                    result = 0;  
                               }else{  
                                    result = 1;  
                               }  
                               break;  
                          }  
                     }  
                     System.out.print(currentBit+" ");  
                }  
                if(result != -1){  
                     System.out.println(" "+op.getOperatorName()+" -> "+result);  
                }  
           }  
      }  
      public static void main(String[] args) {  
           printTruthTable(2, Operator.XOR);  
      }  
 }  

No comments:

Post a Comment

Hi Friends, Please leave your comments as they shall be greatest motivation , shall help me make corrections to my existing posts and will enable me to create better posts. :)