CS 101 – Aug. 31 Interpreting binary decimal √ Decimal binary Shorthand ways of expressing binary –Octal –“Hex” Negative numbers
Number of Possible Values 1 bit: 0 and 1 2 bits: 00, 01, 10, 11 3 bits: 000, 001, …? For a given number of bits, how many patterns?
decimal binary You already know binary decimal But given a decimal number like 45, what is binary form? Binary numbers are longer than decimal. –(Ex. 5 digit number may require 15 bits) My technique is the “binary store”
Binary store At the Binary Store, price is a power of 2: $1, $2, $4, $8, $16… If you had $45, what could you buy? 45 = Write powers of 2: Write binary number:
Another example Let’s convert 61 to binary: Go to binary store with $61… 61 = = Finally:
Octal Octal means base 8: each digit is a power of 8. Because 8 = 2 3, each octal digit corresponds to 3 bits = = ?
Hexadecimal (“hex”) Base 16: each digit is a power of 16 Since 16 = 2 4, each hex digit corresponds to 4 bits. Hex also means we have 16 different digits. a = 10, b = 11, c = 12, d = 13, e = 14, f = 15
Hex examples = d = Now let’s go the other way: = ______ = _______ 16
Decimal octal, hex Often the best way to come up with octal or hex is to go thru binary first. Ex. What is 71 in octal? –Binary store gives: 71 = –Binary number is –Grouped as octal: = We can check answer.
Why couldn’t the computer scientist tell the difference between Halloween (Oct 31) and Christmas (Dec 25) ?
Negatives? We can used a “signed” representation. We want half the rep’ns to be negative. Ex. 5 bits 32 numbers. –16 numbers are negative –Thus, range is –16 to +15. For n bits: range is –2 n–1 to 2 n – 1 – 1
How to represent negatives In ordinary (unsigned) binary, this was impossible! In signed: 3 steps to represent –x: 1.Find rep’n of +x. 2.Invert the bits. 3.Add 1. Try some examples.