Bitwise Operators
2002-02-09 23:36:11
Category: cpp:math
Description: Example of the different bitwise operators (or, and, xor, shift left, shift right)
Author: detour
Viewed: 2687
Rating: (17 votes)


/* bitwise.cpp written by detour@metalshell.com 
 *
 * Bitwise Operators
 * x & y = And X and Y ( 0001 & 0011 = 0001 )
 * x | y = Or X and Y ( 0101 | 1100 = 1101 )
 * x ^ y = Xor X and Y ( 0110 ^ 1100 = 1000 )
 * ~x = Not X ( 1111 = 0000 )
 * x << n = Shift x to the left n times ( multply by n^2 if n>1 )
 * x >> n = Shift x to the right n times ( divide by n^2 if n>1 )
 *
 * http://www.metalshell.com/
 *
 */
 
#include <stdio.h>
 
int main() {
  int x = 11; // 1011
  int y = 2;  // 0010
 
  printf("x = 1011\ny = 0010\n\n");
  printf("x | y = %d\n", x|y);
  printf("x & y = %d\n", x&y);
  printf("x ^ y = %d\n", x^y);
  printf("x << 3 = %d\n", x << 3);
  printf("y >> 1 = %d\n", y >> 1);
 
}