Advertisement
sedran

Bitwise Operations in C++

Dec 26th, 2011
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.37 KB | None | 0 0
  1. char x = 'A';
  2. char x1 = 0;
  3. cout << x << endl;
  4. char bitler[8];
  5. for(int i=0; i<8; i++) {
  6.     bitler[i] = x & 1; // 0101010a & 00000001 = 0000000a for a€{0,1}
  7.     x >>= 1; // (00111110 << 1) returns 00011111
  8. }
  9. for(int i=7; i>=0; i--) {
  10.     x1 <<= 1; // (00111110 << 1) returns 01111100
  11.     x1 |= bitler[i]; // 01010100 | 0000000a = 0101010a for a€{0,1}
  12. }
  13. cout << x1 << endl;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement