Bit-wise Operations using C++
#include #include #include void dectobi(int p) { int b,i,x,d; for(i=31;i>=0;i--) { d=1<<i; b=p&d; if(b==0) printf("0"); else printf("1"); } } void main() { int i,j,k,ch,n,m,a; char cho; clrscr(); do { printf("enter a integer\n"); scanf("%d",&n); dectobi(n); printf("\n 1 for 1`s compliment"); printf("\n 2 for AND opertion"); printf("\n 3 for OR opertion"); printf("\n 4 for EX-OR opertion"); printf("\n 5 for LEFT SHIFT opertion"); printf("\n 6 for RIGHT SHIFT opertion"); printf("\n 7 for EXIT\n"); scanf("%d",&ch); switch(ch) { case 1: printf(" \n1`s compliment\n"); a=~n; dectobi(a); break; case 2: printf("enter intger\n"); scanf("%d",&m); dectobi(m); printf("after and\n"); a=n&m; dectobi(a); break; case 3: printf("enter intger\n"); scanf("%d",&m); dectobi(m); printf("after OR\n"); a=n|m; dectobi(a); break; case 4: printf("enter intger\n"); scanf("%d",&m); dectobi(m); printf("after EX-OR\n"); a=n^m ; dectobi(a); break; case 5: printf("enter the no by u want to LEFT SHIFT"); scanf("%d",&m); a=n<<m; printf("after LEFT SHIFT\n"); a=n<<1; dectobi(a); break; case 6: printf("enter the no by u want to RIGHT SHIFT"); scanf("%d",&m); a=n>>m; printf("after RIGHT SHIFT\n"); dectobi(a); break; case 7: exit(0); } flushall(); printf("\nwant to continue (y/n)"); scanf("%c",&cho); } while(cho!='n'); }
Leave a Reply