All bitwise operation in C | My CS Tutorial - My CS Tutorial

Breaking

Programming languages full tutorial and programs, exam papers, visual basic( Vb.net ), information of new technologies and more....

Tuesday, March 3, 2020

All bitwise operation in C | My CS Tutorial


C PROGRAM CODE




#include<stdio.h>
#include<string.h>
main()
{
 int a,b,c;
 char choice[2];

 printf("\n Enter your Choice\n & for AND\n | for OR\n ^ for XOR\n ~ for Compliment\n << for Left Shift\n >> for Right Shift\n ");
 scanf("%s",choice);

 printf("\n Enter the value of a and b:\n ");
 scanf("%d%d",&a,&b);

 if(strcmp(choice,"&")==0)
 {
  c=a&b;
  printf("\n %d & %d=%d\n",a,b,c);
 }
 else if(strcmp(choice,"|")==0)
 {
  c=a|b;
  printf("\n %d | %d=%d\n",a,b,c);
 }
 else if(strcmp(choice,"^")==0)
 {
  c=a^b;
  printf("\n %d ^ %d=%d\n",a,b,c);
 }
 else if(strcmp(choice,"~")==0)
 {
  printf("\n ~ %d=%d\n",a,~a);
  printf("\n ~ %d=%d\n",b,~b);
 }
 else if(strcmp(choice,"<<")==0)
 {
  c=a<<b;
  printf("\n %d << %d=%d\n",a,b,c);
 }
 else if(strcmp(choice,">>")==0)
 {
  c=a>>b;
  printf("\n %d >> %d=%d\n",a,b,c);
 }
 else
 {
  printf("\n Invalid Choice\n" );
 }
}


OUTPUT







No comments:

Post a Comment