logo

UTK Notes


Postional Number Systems

Topics

What is a Positional Number System?

  • A way to represent a number and its magnitude using the “position” a digit is placed
  • Base determines the power base
    • Decimal 10 p
    • Binary 2 p
    • Hexadecimal 16 p
    • Octal 8 p
  • Places starts with power 0 (just like array index)
  • Base has digits 0 - (base - 1)
    • Decimal 0 - 9
    • Binary 0 - 1
    • Hexadecimal 0 - F
    • Octal 0 - 7

Decimal (Base 10)

  • The weight of each digit of a decimal number depends on its relative position within the numer
  • ex. One thousand, two hundred twenty-four

  • Decimal
    • 1234:
      • 1(1000) + 2(100) + 3(10) + 4(1)
    • Decimal consists of powers of 10
      • 1000 (103), 100 (102), 10 (101), 1 (100)
    • For each ascending place, its magnitude grows 10x

Binary (Base 2)

  • A digit in binary (0 or 1) is also called a bit. Only these two digits are used in binary.
  • Like a decimal number, the weight of each binary bit of a binary number depends on its relative position within the number
    • For each ascending place, its magnitdue grows 2x
  • Binary (base 2)
    • 1101
      • 1(8) + 1(4) + 0(2) + 1(1)
    • Binary consists of powers of 2
      • 8 (23), 4 (22), 2 (21), 1(20)
    • For each ascending place, its magnitude grows 2x

Distinguishing Base 2

  • Is 1101
    • One thousand, one hunderd, one?
      • 1(1000) + 1(100) + 0(10) + 1(1)
    • Thirteen?
      • 1(8) + 1(4) + 0(2) + 1(1)
    • Distinguish from base 10 by prefix or suffix
      • 0b1101 = Binary 1101 (0b means binary)
        • This is how you represent binary in C++ and assembly
      • 11012 = Bianry 1101 (subscript 2 means binary)

Why Binary

  • Binary represents on (1) or off (0), true or false, yes or no.
    • 1 = voltage on (typically)
    • 0 = voltage off
  • Computers use a group of 8 binary digits (bits), which is called a “byte”.
    • 0b0101_0011 = 1 byte

Hexadeimal Digits

  • Hexadecimal number system has a base of 16 … so how do we represent any digit bigger than 9? 10 can’t work for ten - it’s two digits, and we need a single digit.
  • Digits > 9 are represented by a letter A-F
    • A = 10
    • B = 11
    • C = 12
    • D = 13
    • E = 14
    • F = 15

NOTE: Case does not typically matter and in programming, we use lower-case letters.

  • Hexadecimal
    • 19AC
      • 1(4096) + 9(256) + A(16) + C(1)
    • Hexadecimal consists of powers of 16
      • 4096 (163), 256 (162), 16 (161), 1(160)
    • For each place, its magnitude grows 16x

Distinguishing Base 16

  • Is 16
    • Sixteen?                   1(10) + 6(1)
    • Twenty-two?            1(16) + 6(1)
  • Distinguish from base 10 by prefix or suffix
    • 0x16 = Hex 16 (0x means hex)
      • This is how you represent hex in C++ and assembly
    • 16 16 = Hex 16 (subscript 16 means hex)

Number System Comparison

Number system suffix example subscript example
decimal 0d 0d1023 10 102310
binary 0b 0b1101 2 11012
hexadecimal 0x 0x12F 16 12F16
Decimal Binary Hexadeciaml
0 0000 0
1 0001 1
2 0010 2
3 0011 3
4 0100 4
5 0101 5
6 0110 6
7 0111 7
8 1000 8
9 1001 9
10 1010 A
11 1011 B
12 1100 C
13 1101 D
14 1101 E
15 1111 F

Practice

  • 0xA5
    • A(16) + 5(1)
      • 10(16) + 5 = 160 + 5 = 165
  • 0x1C
    • 1(16) + C(1)
      • 16 + 12 = 28
  • 0b0001_1001
    • 1(16) + 1(8) + 0(4) + 0(2) + 1(1)
      • 16 + 8 + 1 = 24 + 1 = 25
  • 0b0000_1111
    • 1(8) + 1(4) + 1(2) + 1(1)
      • 8 + 4 + 2 + 1 = 15

Converting To Base 2

  • 84
    • Need to find powers of two that add up to 84
  • Find largest power of 2 that comes closest to 84, but not over. Your first 1 wil go here. Subtract 84 by the power, repeat.
    • 27 = 128 (too large) Index 7 gets 0
    • 26 = 64 (closest to 84) Index 6 gets 1
      • 84 - 64 = 20 (still need to represent 20)
    • 25 = 32 (bigger than 20) Index 5 gets 0
    • 24 = 16 (closest to 20) Index 4 gets 1
      • 20 - 16 = 4 (still need to represent 4)
    • 23 = 8 (too large) Index 3 gets 0
    • 22 = 4 (closest to 4) Index 2 gets 1
      • 4 - 4 = 0
    • 21 = 2 (too large) Index 1 gets 0
    • 20 - 1 (too large) Index 0 gets 0

84 = 0101 0100

Easy Converting To Base 16

  • 124
    • First, convert to binary
      • 0b0111_1100
    • Each 1 hex digit is 4 binary digits
      • Takes groups of four at a time
        • 0111 = 7 = 0x7
        • 1100 = 12 = 0xc

124 = 0x7c

Powerpoint