Advertisement
45b94806

C Bitwise Operators

May 11th, 2024
716
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.65 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. int main()
  4. {
  5.    // BITWISE OPERATORS = special operators used in bit level programming
  6.    //                                          (knowing binary is important for this topic)
  7.  
  8.    // & = AND
  9.    // | = OR
  10.    // ^ = XOR
  11.    // <<  left shift
  12.    // >>  right shift
  13.  
  14.    int x = 6;  //    6 = 00000110
  15.    int y = 12; // 12 = 00001100
  16.    int z = 0;  //    0 = 00000000
  17.  
  18.    z = x & y;
  19.    printf("AND = %d\n", z);
  20.  
  21.    z = x | y;
  22.    printf("OR = %d\n", z);
  23.  
  24.    z = x ^ y;
  25.    printf("XOR = %d\n", z);
  26.  
  27.    z = x << 2;
  28.    printf("SHIFT LEFT = %d\n", z);
  29.  
  30.    z = x >> 2;
  31.    printf("SHIFT RIGHT = %d\n", z);
  32.  
  33.    return 0;
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement