haseeb_heaven

Logic_Gates.c

Aug 2nd, 2014
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.46 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<math.h>
  3. #include<stdlib.h>
  4.  
  5. unsigned int NOR(unsigned int,unsigned int);
  6. unsigned int X_NOR(unsigned int,unsigned int);
  7. unsigned int NAND(unsigned int,unsigned int);
  8.  
  9. int main(void)
  10. {
  11.  
  12. unsigned int a,b;
  13. int choice;
  14.  
  15. do{
  16. printf("\nWelcome to Logic Gates...\n");
  17. printf("1)Nor gate\n");
  18. printf("2)X-Nor gate\n");
  19. printf("3)Nand gate\n");
  20. printf("4)Exit\n\n");
  21. printf("Enter a and b (Two Inputs for Logic Gate) \n");
  22. scanf("%u %u",&a,&b);
  23.  
  24. printf("\nNow Choose Any Logic Gate\n");
  25. scanf("%d",&choice);
  26.  
  27.  switch(choice){
  28.   case 1 :
  29.   printf("\nNOR gate :\n %u NOR %u --> %u\n",a,b,NOR(a,b));
  30.   break;
  31.  
  32.   case 2 :
  33.   printf("\nX-NOR gate :\n %u X-NOR %u --> %u\n",a,b,X_NOR(a,b));
  34.   break;
  35.  
  36.   case 3 :
  37.   printf("\nNAND gate :\n %u NAND %u --> %u\n",a,b,NAND(a,b));
  38.   break;
  39.  
  40.   case 4 : exit(0);
  41.   break;
  42.  
  43.   default : fputs("Error Wrong Choice\n",stderr);
  44.  }
  45. }while(choice < 4);
  46.    
  47. return 0;
  48. }
  49.  
  50.  
  51. unsigned int NOR(unsigned int a,unsigned int b)
  52. {
  53. return ( (~(a | b)) & (((int)pow(2,floor(log2( (a > b) ? ((a == 0) ? 1 : a) : ((b == 0) ? 1 : b) )) + 1) )  - 1) );
  54. }
  55.  
  56. unsigned int X_NOR(unsigned int a,unsigned int b)
  57. {
  58. return ( (~(a ^ b)) & (((int)pow(2,floor(log2( (a > b) ? ((a == 0) ? 1 : a) : ((b == 0) ? 1 : b) )) + 1) )  - 1) );
  59. }
  60.  
  61. unsigned int NAND(unsigned int a,unsigned int b)
  62. {
  63. return ( (~(a & b)) & (((int)pow(2,floor(log2( (a > b) ? ((a == 0) ? 1 : a) : ((b == 0) ? 1 : b) )) + 1) )  - 1));
  64. }
Add Comment
Please, Sign In to add comment