Guest User

C++ Arithmetics

a guest
Sep 3rd, 2012
61
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <cstdio>
  2. #include <cstdlib>
  3.  
  4. int main(int argc, char* argv[]) {
  5.   printf("Simple integral arithmetics:\n");
  6.   printf("1 + 1 = %d\n", 1 + 1);
  7.   printf("3 * 3 = %d\n", 3 * 3);
  8.   printf("5 - 8 = %d\n", 5 - 8);
  9.   printf("18 / 5 = %d\n", 18 / 5);
  10.   printf("18 %% 5 = %d\n", 18 % 5);
  11.   printf("-18 / 5 = %d\n", -18 / 5);
  12.   printf("-18 %% 5 = %d\n", -18 % 5);
  13.  
  14.   printf("\n\n");
  15.   printf("Bit operations:\n");
  16.   printf("1 | 5 = %d\n", 1 | 5);
  17.   printf("3 & 7 = %d\n", 3 & 7);
  18.   printf("5 ^ 8 = %d\n", 5 ^ 8);
  19.   printf("~18 = %d\n", ~18);
  20.   printf("18 >> 2 = %d\n", 18 >> 2);
  21.   printf("5 << 3 = %d\n", 5 << 3);
  22.  
  23.   printf("\n\n");
  24.   printf("Increments and so on:\n");
  25.  
  26.   int i = 10;
  27.   ++i;
  28.   printf("int i = 10; ++i;\n");
  29.   printf("i = %d\n", i);
  30.   printf("i++ = %d\n", i++);
  31.   printf("--i = %d\n", --i);
  32.   printf("(i += 10) = %d\n", i += 10);
  33.  
  34.   printf("\n\n");
  35.   printf("Type cast:\n");
  36.   printf("3 / 2 = %d\n", 3 / 2);
  37.   printf("(double)(3) / 2 = %lf\n", (double)(3) / 2);
  38.  
  39.   printf("\n\n");
  40.   printf("sizeof():\n");
  41.   printf("sizeof(3 / 2) = %u\n", sizeof(3 / 2));
  42.   printf("sizeof(3.0 / 2.0) = %u\n", sizeof(3.0 / 2.0));
  43.  
  44.   /*Output:
  45.   Simple integral arithmetics:
  46.   1 + 1 = 2
  47.   3 * 3 = 9
  48.   5 - 8 = -3
  49.   18 / 5 = 3
  50.   18 % 5 = 3
  51.   -18 / 5 = -3
  52.   -18 % 5 = -3
  53.  
  54.  
  55.   Bit operations:
  56.   1 | 5 = 5
  57.   3 & 7 = 3
  58.   5 ^ 8 = 13
  59.   ~18 = -19
  60.   18 >> 2 = 4
  61.   5 << 3 = 40
  62.  
  63.  
  64.   Increments and so on:
  65.   int i = 10; ++i;
  66.   i = 11
  67.   i++ = 11
  68.   --i = 11
  69.   (i += 10) = 21
  70.  
  71.  
  72.   Type cast:
  73.   3 / 2 = 1
  74.   (double)(3) / 2 = 1.500000
  75.  
  76.  
  77.   sizeof():
  78.   sizeof(3 / 2) = 4
  79.   sizeof(3.0 / 2.0) = 8
  80.   */
  81.  
  82.  
  83.  
  84.   /* Operators priority
  85.   -a +a ~a (type) sizeof() ++ --
  86.   * / %
  87.   + -
  88.   << >>
  89.   &
  90.   ^
  91.   |
  92.   = += -= etc
  93.  
  94.   */
  95.  
  96.  
  97.   system("pause");
  98. }
RAW Paste Data