Advertisement
VoronVU

Использование побитовых операторов

Nov 11th, 2015
413
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.14 KB | None | 0 0
  1. //Использование побитовых операторов для выполнения операций NOT, AND, OR и XOR с отдельными битами целого числа
  2. #include <iostream>
  3. #include <bitset>
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8.    cout << "Enter a number (0 - 255): ";
  9.    unsigned short InputNum = 0;
  10.    cin >> InputNum;
  11.  
  12.    bitset<8> InputBits (InputNum);
  13.    cout << InputNum << " in binary is " << InputBits << endl;
  14.  
  15.    bitset<8> BitwiseNOT = (~InputNum);
  16.    cout << "Logical NOT |" << endl;
  17.    cout << "~" << InputBits  << " = " << BitwiseNOT << endl;
  18.  
  19.    cout << "Logical AND, & with 00001111" << endl;
  20.    bitset<8> BitwiseAND = (0x0F & InputNum);// 0x0F is hex for 0001111
  21.    cout << "0001111 & " << InputBits  << " = " << BitwiseAND << endl;
  22.  
  23.    cout << "Logical OR, | with 00001111" << endl;
  24.    bitset<8> BitwiseOR = (0x0F | InputNum);
  25.    cout << "00001111 | " << InputBits  << " = " << BitwiseOR << endl;
  26.  
  27.    cout << "Logical XOR, ^ with 00001111" << endl;
  28.    bitset<8> BitwiseXOR = (0x0F ^ InputNum);
  29.    cout << "00001111 ^ " << InputBits  << " = " << BitwiseXOR << endl;
  30.  
  31.    return 0;
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement