Pabon_SEC

Bitwise Operation

Jun 1st, 2016
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.47 KB | None | 0 0
  1. /***
  2.  
  3. Bismillahir Rahmanir Rahim
  4.  
  5.  _______
  6. |   __  \           _____
  7. |  |__|  )_______   |   /     ____    ______
  8. |   ____/ \__    \  |  |__   /    \  |      \
  9. |  (        / __  \ |  __ \ /  __  \ |   /\  \
  10. |  |       (____  / |     / \      / |__/  \  )
  11. |__|            \/  |____/   \____/         \/
  12.  
  13.  
  14. ***/
  15.  
  16. #include<bits/stdc++.h>
  17.  
  18. using namespace std;
  19.  
  20. bool check(int num,int pos)
  21. {
  22.     return num & (1<<pos);
  23. }
  24.  
  25. int biton(int num,int pos)
  26. {
  27.     return num|=(1<<pos);
  28. }
  29.  
  30. int bitoff(int num,int pos)
  31. {
  32.     return num &= ~(1<<pos);
  33. }
  34.  
  35. int main()
  36. {
  37.     int number,pos;
  38.  
  39.     cout<<"Enter a Number please : ";
  40.  
  41.     while(cin>>number)
  42.     {
  43.         cout<<"\nPosition is : ";
  44.  
  45.         cin>>pos;
  46.  
  47.         cout<<"\nThe "<<pos<<"'th bit is : "<<check(number,pos)<<"\n\n";
  48.  
  49.         if(check(number,pos))
  50.         {
  51.             cout<<"\nThe "<<pos<<"'th bit is already On\n\n";
  52.         }
  53.         else
  54.         {
  55.             cout<<"\nNow the "<<pos<<"'th bit is On";
  56.  
  57.             cout<<" and Changed Number is : "<<biton(number,pos)<<"\n\n";
  58.         }
  59.  
  60.         if(check(number,pos))
  61.         {
  62.             cout<<"\nNow the "<<pos<<"'th bit is Off";
  63.  
  64.             cout<<" and Changed Number is : "<<bitoff(number,pos)<<"\n\n";
  65.         }
  66.         else
  67.         {
  68.             cout<<"\nThe "<<pos<<"'th bit is already Off\n\n";
  69.         }
  70.  
  71.         cout<<"\n______________\n";
  72.  
  73.         cout<<"\nEnter a Number please : ";
  74.     }
  75.  
  76.     return 0;
  77. }
Advertisement
Add Comment
Please, Sign In to add comment