Upgrade to Pro — share decks privately, control downloads, hide ads and more …

03-NumberSystems.pdf

 03-NumberSystems.pdf

Avatar for William Albritton

William Albritton

September 04, 2015
Tweet

More Decks by William Albritton

Other Decks in Programming

Transcript

  1. Memory Allocation  Numeric Systems  Decimal, Binary, Hexadecimal 

    Converting  Integer Representation  Character Representation
  2. Decimal (base 10)  Uses positional representation  Each digit

    corresponds to a power of 10 based on its position in the number  The powers of 10 increment from 0, 1, 2, etc. as you move right to left  1,479 = 1 * 103 + 4 * 102 + 7 * 101 + 9 * 100
  3. Binary (base 2)  Two digits: 0, 1  To

    make the binary numbers more readable, the digits are often put in groups of 4  1010 = 1 * 23 + 0 * 22 + 1 * 21 + 0 * 20 = 8 + 2 = 10 decimal  1100 1001 = 1 * 27 + 1 * 26 + 1 * 23 + 1 * 20 = 128 + 64 + 8 + 1 = 201 decimal
  4.  Shorter and easier to read than binary  16

    digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F  “0x” often precedes hexadecimal numbers  0x123 = 1 * 162 + 2 * 161 + 3 * 160 = 1 * 256 + 2 * 16 + 3 * 1 = 256 + 32 + 3 = 291 decimal Hexadecimal (base 16)
  5. Hexadecimal (base 16)  Another example  0xABC = A

    * 162 + B * 161 + C * 160 = 10 * 256 + 11 * 16 + 12 * 1 = 2560 + 176 + 12 = 2748 decimal
  6. Binary Decimal Hexadecimal 0000 0 0 0001 1 1 0010

    2 2 0011 3 3 0100 4 4 0101 5 5 0110 6 6 0111 7 7 Binary Decimal Hexadecimal
  7. Binary Decimal Hexadecimal 1000 8 8 1001 9 9 1010

    10 A 1011 11 B 1100 12 C 1101 13 D 1110 14 E 1111 15 F 10000 16 10 Binary Decimal Hexadecimal
  8. Converting  From binary or hex to decimal  Use

    positional representation as shown previously  From decimal to binary (or hex)  Keep dividing by 2 (or 16)  Remainders give the digits, starting from lowest power
  9. Converting  From binary to hex (or vice versa) 

    Replace each set of four binary digits by the corresponding hexadecimal digit (or vice versa)
  10. Convert Decimal to Binary  22 decimal = 10110 binary

     Calculations: 2 | 22 r. 0 2 | 11 r. 1 2 | 5 r. 1 2 | 2 r. 0 2 | 1 r. 1 0
  11. Convert Decimal to Binary  22 decimal = 10110 binary

     Calculations:  Write down powers of 2  Subtract the largest power of 2 that is less than the number  Add a 1 if can subtract, and a 0 if cannot 22 6 2 32 16 8 4 2 1 0 1 0 1 1 0
  12. Other Bases  Base 8 (octal number system)  123

    = 1 * 82 + 2 * 81 + 3 * 80 = 1 * 64 + 2 * 8 + 3 * 1 = 64 + 16 + 3 = 83 decimal
  13. Other Bases  Base 20 (vigesimal number system)  Mayans,

    Aztecs, Tlingit (Pacific Northwest Coast), Inuit, Yoruba language (Africa), Ainu language (Japan), Dzongkha language (Bhutan)  Gettysburg Address "Four score and seven years ago…“ = 87 years ago
  14. Unsigned Integers  Represents positive integers only  Not necessary

    to indicate a sign, so all bits can be used for the magnitude:  1 byte = 8 bits = 28 = 256 (0 to 255)  255 decimal = 1111 1111 binary  2 bytes = 16 bits = 216 = 65,536 (0 to 65,535)  4 bytes = 32 bits = 232 = 4,294,967,296 (0 to 4,294,967,295)
  15. Signed Integers  Represents positive and negative integers  MSB

    (Most Significant Bit – leftmost bit) used to indicate the sign  0 = positive  1 = negative  Trivia question: What is a TLA?
  16. Signed Integers: 1 byte  One less bit is used

    for the magnitude, with one extra negative value  1 byte = 8-1 bits = 27 (-128 to +127)  -128 decimal = 1000 0000 binary  -8 decimal = 1111 1000 binary  -1 decimal = 1111 1111 binary  +8 decimal = 0000 1000 binary  +127 decimal = 0111 1111 binary
  17. Signed Integers: 2 bytes  2 bytes = 16-1 bits

    for magnitude = 215 (-32,768 to +32,767)  -32,768 decimal = 0x8000 = 1000 0000 0000 0000 binary  -1 decimal = 0xFFFF = 1111 1111 1111 1111 binary  +32,767 decimal = 0x7FFF = 0111 1111 1111 1111 binary
  18. Signed Integers: 4 bytes  4 bytes = 32-1 bits

    for magnitude = 231 (-2,147,483,648 to +2,147,483,647)  -2,147,483,648 decimal = 0x80000000  -1 decimal = 0xFFFFFFFF  +1 decimal = 0x00000001  +2,147,483,647 decimal = 0x7FFFFFFF
  19. One’s Complement Form  Formed by complementing (flipping) each bit

     Change all 1s to 0s, and all 0s to 1s  1 byte example:  0000 1001 binary (9 decimal)  1111 0110 is 1's complement
  20. One’s Complement Form  2 byte example:  0000 1111

    0101 1001 binary  1111 0000 1010 0110 is one's complement
  21. Two’s Complement Form  Formed by adding 1 to one's

    complement  Negative numbers are stored this way  Additive inverse of a number  Computer never has to subtract  A – B = A + (-B)
  22. Binary to Decimal Conversion  Unsigned Integers  Convert binary

    number directly to decimal number by using positional representation as shown previously
  23. Binary to Decimal Conversion  Signed Integers  If MSB

    = 0 (postive number)  Convert binary number directly to decimal number by using positional representation as shown previously
  24. Binary to Decimal Conversion  Signed Integers  If MSB

    = 1 (negative number)  Convert to 1’s complement form (flip the bits)  Convert to 2's complement form (add 1)  Convert binary number to decimal number using positional representation as shown previously  Put negative sign (-) in front of decimal number
  25. Convert Signed Negative Integers  Convert the signed 8-bit binary

    number to decimal: 1 1 1 1 0 1 1 1 binary  Step #1:  Convert to 1’s complement form  1111 0111 binary (original number)  0000 1000 binary (flipped bits)
  26. Convert Signed Negative Integers  Convert the signed 8-bit binary

    number to decimal: 1 1 1 1 0 1 1 1 binary  Step #2:  Convert to 2's complement form  0000 1000 binary (flipped bits)  +1 binary (add 1)  0000 1001 binary
  27. Convert Signed Negative Integers  Convert the signed 8-bit binary

    number to decimal: 1 1 1 1 0 1 1 1 binary  Step #3:  Convert binary number to decimal number using positional representation  0000 1001 binary = 9 decimal (unsigned)
  28. Convert Signed Negative Integers  Convert the signed 8-bit binary

    number to decimal: 1 1 1 1 0 1 1 1 binary  Step #4:  Put a negative sign (-) in front of decimal number  1111 0111 binary = -9 decimal (signed)
  29. Decimal to Binary Conversion  Convert a positive decimal number

    to signed binary number  Keep dividing by 2, where remainders give the digits, starting from lowest power, as shown previously
  30. Decimal to Binary Conversion  Convert a negative decimal number

    to signed binary number  Step #1: Convert the magnitude (number without the sign) from decimal to binary: keep dividing by 2, where remainders give the digits, starting from lowest power, as shown previously  Step #2: Convert to 1’s complement form (flip the bits)  Step #3: Convert to 2's complement form (add 1)
  31. Convert Signed Negative Integers  Convert the decimal integer to

    signed 8-bit binary number: -9  Step #1: Convert the magnitude (number without the sign) from decimal to binary 9 decimal = 0000 1001 binary (unsigned)
  32. Convert Signed Negative Integers  Convert the decimal integer to

    signed 8-bit binary number: -9  Step #2: Convert to 1’s complement form 0000 1001 binary (unsigned number) 1111 0110 binary (flipped bits)  Step #3: Convert to 2's complement form 1111 0110 binary (flipped bits) +1 binary (add 1) 1111 0111 binary = -9 decimal
  33. Character Representation  ASCII (See link to ASCII Codes) 

    American Standard Code for Information Interchange  Standard encoding scheme used to represent characters in binary format on computers  7-bit encoding, so 128 characters can be represented
  34. ASCII Character Codes  0 to 31 (& 127) are

    "control characters" (cannot print)  Ctrl-A or ^A is 1, ^B is 2, etc.  Used for screen formatting and data communication  32 to 126 are printable characters
  35. ASCII Character Codes  '0' 48 dec 0x30 0011 0000

     '9' 57 dec 0x39 0011 1001  'A' 65 dec 0x41 0100 0001  'Z' 90 dec 0x5A 0101 1010  'a‘ 97 dec 0x61 0110 0001  'z‘ 122 dec 0x7A 0111 1010
  36. Binary Data  Decimal, hexadecimal, and character representations are easier

    for humans to understand; however…  All the data in the computer is binary
  37. Binary Data  An int is typically 32 binary digits

     int y = 5; (y = 0x00000005;)  In computer, y = 00000000 00000000 00000000 00000101  int z = -5; (y = 0xFFFFFFFB;)  In computer, z = 11111111 11111111 11111111 11111011
  38. Binary Data  A char is typically 8 binary digits

     char x = '5'; (or char x = 53, or char x = 0x35)  In computer, x = 00110101  char x = 5; (or char x = 0x05;)  In computer, x = 00000101
  39. Binary Data  Note that the ASCII character '5' has

    a different binary value than the decimal integer 5  Also note that 1 ASCII character = 2 hexadecimal numbers
  40. Memory Management  Numeric Systems  Decimal, Binary, Hexadecimal 

    Converting  Integer Representation  Character Representation