mvaganov

simple C program learning about binary

Nov 19th, 2014
165
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2.  
  3. int main()
  4. {
  5.     int number = 0;
  6.     // %d is a base 10 number (Decimal)
  7.     // %c is a character
  8.     // %x is a base 16 number (heXadecimal)
  9.  
  10.     // -1 is the same as 255 as a character      End Of File
  11.     // 0  \0   null-terminator
  12.     // 7  \a   alarm
  13.     // 8  \b   backspace
  14.     // 9  \t   tab
  15.     // 10 \n   new line (line feed)
  16.     // 13 \r   carriage return
  17.  
  18.     while(number < 256)
  19.     {
  20.         printf("%d: 0x%x (%c)\n", number, number, number);
  21.         number++;
  22.     }
  23.     // 8 bit binary
  24.     //   0   0   0   0   0   1   0   1      5
  25.     // ___ ___ ___ ___ ___ ___ ___ ___
  26.     // 128  64  32  16   8   4   2   1
  27.     // << left shift
  28.     // >> right shift
  29.     printf("%d\n", 1 << 0); // 1  <-- 2^0
  30.     printf("%d\n", 1 << 1); // 2  <-- 2^1
  31.     printf("%d\n", 1 << 2); // 4  <-- 2^2
  32.     printf("%d\n", 1 << 3); // 8  <-- 2^3
  33.     printf("%d\n", 3 << 1); // 6
  34.     printf("%d\n", 5 << 2); // 20
  35.     printf("%d\n", 5 >> 1); // 2 (1 bit falls off of the register)
  36.     printf("%d\n", 1 << 30); // ~1billion <-- 2^30
  37.     printf("%d\n", 1 << 31); // ~-2billion <-- 2^31 <-- the sign bit
  38.                             // most significant place reserved for negatives
  39.     printf("%d\n", 1 << 32); // 0 <-- moved all the way off of the register
  40.     printf("%d\n", 1 << 33); // 0
  41.     return 0; // signals the main function to stop.
  42. }
RAW Paste Data