720 likes | 824 Views
Binary Numbers. Why Binary?. Maximal distinction among values m inimal corruption from noise Imagine taking the same physical attribute of a circuit, e.g. a voltage lying between 0 and 5 volts, to represent a number The overall range can be divided into any number of regions.
E N D
Why Binary? • Maximal distinction among values minimal corruption from noise • Imagine taking the same physical attribute of a circuit, e.g. a voltage lying between 0 and 5 volts, to represent a number • The overall range can be divided into any number of regions
Don’t sweat the small stuff • For decimal numbers, fluctuations must be less than 0.25 volts • For binary numbers, fluctuations must be less than 1.25 volts 5 volts 0 volts Binary Decimal
Range actually split in three High Forbidden range Low
It doesn’t matter …. • Some of the standard voltages coming from a computer’s power are ideally supposed to be 3.30 volts, 5.00 volts and 12.00 volts • Typically they are 3.28 volts, 5.14 volts or 12.22 volts or some such value • So what, who cares
How to represent big integers • Use positional weighting, same as with decimal numbers • 205 = 2102 + 0101 + 5100 • Decimal – powers of ten • 11001101 = 127 + 126 + 025 + 024 + 123 + 122 + 021 + 120 = 128 + 64 + 8 + 4 + 1 = 205 • Binary – powers of two
Converting 205 to Binary • 205/2 = 102 with a remainder of 1, place the 1 in the least significant digit position • Repeat 102/2 = 51, remainder 0
Iterate • 51/2 = 25, remainder 1 • 25/2 = 12, remainder 1 • 12/2 = 6, remainder 0
Iterate • 6/2 = 3, remainder 0 • 3/2 = 1, remainder 1 • 1/2 = 0, remainder 1
Recap 127 + 126 + 025 + 024 + 123 + 122 + 021 + 120 205
Finite representation • Typically we just think computers do binary math. • But an important distinction between binary math in the abstract and what computers do is that computers are finite. • There are only so many flip-flops or logic gates in the computer. • When we declare a variable, we set aside a certain number of flip-flops (bits of memory) to hold the value of the variable. And this limits the values the variable can have.
Same number, different representation • 5 using 8 bits • 0000 0101 • 5 using 16 bits • 0000 0000 0000 0101 • 5 using 32 bits • 0000 0000 0000 0000 0000 0000 0000 0101
Adding Binary Numbers • Same as decimal; if the sum of digits in a given position exceeds the base (10 for decimal, 2 for binary) then there is a carry into the next higher position
Adding Binary Numbers carries
Uh oh, overflow* • What if you use a byte (8 bits) to represent an integer • A byte may not be enough to represent the sum of two such numbers. *The End of the World as We Know It
Biggest unsigned integers • 4 bit: 1111 15 = 24 - 1 • 8 bit: 11111111 255 = 28 – 1 • 16 bit: 1111111111111111 65535= 216 – 1 • 32 bit: 11111111111111111111111111111111 4294967295= 232 – 1 • Etc.
Bigger Numbers • You can represent larger numbers by using more words • You just have to keep track of the overflows to know how the lower numbers (less significant words) are affecting the larger numbers (more significant words)
Negative numbers • Negative x is the number that when added to x gives zero • Ignoring overflow the two eight-bit numbers above sum to zero
Two’s Complement • Step 1: exchange 1’s and 0’s • Step 2: add 1 (to the lowest bit only)
Sign bit • With the two’s complement approach, all positive numbers start with a 0 in the left-most, most-significant bit and all negative numbers start with 1. • So the first bit is called the sign bit. • But note you have to work harder than just strip away the first bit. • 10000001 IS NOT the 8-bit version of –1
Add 1’s to the left to get the same negative number using more bits • -5 using 8 bits • 11111011 • -5 using 16 bits • 1111111111111011 • -5 using 32 bits • 11111111111111111111111111111011 • When the numbers represented are whole numbers (positive or negative), they are called integers.
Biggest signed integers • 4 bit: 0111 7 = 23 - 1 • 8 bit: 01111111 127 = 27 – 1 • 16 bit: 0111111111111111 32767= 215 – 1 • 32 bit: 01111111111111111111111111111111 2147483647= 231 – 1 • Etc.
Most negative signed integers • 4 bit: 1000 -8 = - 23 • 8 bit: 10000000 - 128 = - 27 • 16 bit: 1000000000000000 -32768= - 215 • 32 bit: 10000000000000000000000000000000 -2147483648= - 231 • Etc.
Riddle • Is it 214? • Or is it – 42? • Or is it Ö? • Or is it …? • It’s a matter of interpretation • How was it declared?
3-bit unsigned and signed Think of an odometer reading 999999 and the car travels one more mile.
Fractions • Similar to what we’re used to with decimal numbers
Places • 11.001001 Two’s place One’s place Half’s place Fourth’s place Eighth’s place Sixteenth’s place
Decimal to binary • 98.61 • Integer part • 98 / 2 = 49 remainder 0 • 49 / 2 = 24 remainder 1 • 24 / 2 = 12 remainder 0 • 12 / 2 = 6 remainder 0 • 6 / 2 = 3 remainder 0 • 3 / 2 = 1 remainder 1 • 1 / 2 = 0 remainder 1 • 1100010
Decimal to binary • 98.61 • Fractional part • 0.61 2 = 1.22 • 0.22 2 = 0.44 • 0.44 2 = 0.88 • 0.88 2 = 1.76 • 0.76 2 = 1.52 • 0.52 2 = 1.04 • .100111
Decimal to binary • Put together the integral and fractional parts • 98.61 1100010.100111
Another Example (Whole number part) • 123.456 • Integer part • 123 / 2 = 61 remainder 1 • 61 / 2 = 30 remainder 1 • 30 / 2 = 15 remainder 0 • 15 / 2 = 7 remainder 1 • 7 / 2 = 3 remainder 1 • 3 / 2 = 1 remainder 1 • 1 / 2 = 0 remainder 1 • 1111011
Another Example (fractional part) • 123.456 • Fractional part • 0.456 2 = 0.912 • 0.912 2 = 1.824 • 0.824 2 = 1.648 • 0.648 2 = 1.296 • 0.296 2 = 0.592 • 0.592 2 = 1.184 • 0.184 2 = 0.368 • … • .0111010…
Checking fractional part: Enter digits found in binary mode Note that the leading zero does not display.
Divide by 2 raised to the number of digits (in this case 7, including leading zero) 1 2 3 4
Finally hit the equal sign. In most cases it will not be exact
Other way around • Multiply fraction by 2 raised to the desired number of digits in the fractional part. For example • .456 27 = 58.368 • Throw away the fractional part and represent the whole number • 58 111010 • But note that we specified 7 digits and the result above uses only 6. Therefore we need to put in the leading 0. (Also the fraction is less than .5 so there’s a zero in the ½’s place.) • 0111010
Limits of the fixed point approach • Suppose you use 4 bits for the whole number part and 4 bits for the fractional part (ignoring sign for now). • The largest number would be 1111.1111 = 15.9375 • The smallest, non-zero number would be 0000.0001 = .0625
Floating point representation • Floating point representation allows one to represent a wider range of numbers using the same number of bits. • It is like scientific notation.
Scientific notation • Used to represent very large and very small numbers • Ex. Avogadro’s number • 6.0221367 1023 particles • 602213670000000000000000 • Ex. Fundamental charge e • 1.60217733 10-19 C • 0.000000000000000000160217733 C
Scientific notation: all of these are the same number • 12345.6789 = 1234.56789 100 • 1234.56789 10 = 1234.56789 101 • 123.456789 100 =123.456789 102 • 12.3456789 103 • 1.23456789 104 • Rule: Shift the point to the left and increment the power of ten.
Small numbers • 0.000001234 • 0.00001234 10-1 • 0.0001234 10-2 • 0.001234 10-3 • 0.01234 10-4 • 0.1234 10-5 • 1.234 10-6 • Rule: shift point to the right and decrement the power.
Floating Point Rules • Starting with the fixed point binary representation, shift the point and increase the power (of 2 now that we’re in binary) • Shift so that the number has no whole number part and also so that the first fractional bit (the half’s place) has a 1.
Floats • SHIFT expression so it is just under 1 and keep track of the number of shifts • 1100010.1001100110011001 • .11000101001100110011001 27 • Express the number of shifts in binary • .11000101001100110011001 200000111 7 shifts
Mantissa and Exponent • .11000101001100110011001 200000111 • Mantissa • .11000101001100110011001 200000111 • Exponent • (Actually the representation of a float is just a little more complicated, but we will return to that another time.)
Hexadecimal Numbers • Even moderately sized decimal numbers end up as long strings in binary • Hexadecimal numbers (base 16) are often used because the strings are shorter and the conversion to binary is easier • There are 16 digits: 0-9 and A-F