Transcript aug31

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 = 32 + 8 + 4 + 1
Write powers of 2: 25 + 23 + 22 + 20
Write binary number: 101101
Another example
• Let’s convert 61 to binary:
• Go to binary store with $61…
61 = 32 + 16 + 8 + 4 + 1
61 = 25 + 24 + 23 + 22 + 20
Finally: 111101
Octal
• Octal means base 8: each digit is a power
of 8.
• Because 8 = 23, each octal digit corresponds
to 3 bits
• 4618 = 100 110 0012
• 73258 = ?
Hexadecimal (“hex”)
• Base 16: each digit is a power of 16
• Since 16 = 24, 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
• 96416 = 1001 0110 01002
• d12316 = 1101 0001 0010 00112
• Now let’s go the other way:
• 1110002 = ______16
• 100111112 = _______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 = 64 + 4 + 2 + 1
– Binary number is 1000111
– Grouped as octal: 001 000 1112 = 1078
• 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 –2n–1 to 2n – 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.