Explore the significance of binary numbers in computer data representation, from voltage ranges to integer conversions, with a focus on finite representation and handling overflow. Learn about bigger numbers, two’s complement, and signed integers in this insightful guide adapted from Nicholas Carter’s Computer Architecture Chapter 2.
An Image/Link below is provided (as is) to download presentation
Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.Content is provided to you AS IS for your information and personal use only.Download presentation by click this link.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.During download, if you can't get a presentation, the file might be deleted by the publisher.
E N D
More Related Content
Presentation Transcript
Binary Numbers Material on Data Representation can be found in Chapter 2 of Computer Architecture (Nicholas Carter)
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 Decimal Binary
It doesn’t matter …. • Two of the standard voltages coming from a computer’s power supply are ideally supposed to be 5.00 volts and 12.00 volts • Measurements often reveal values that are slightly off – e.g. 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 • 11001101 = 127 + 126 + 025 + 024 + 123 + 122 + 021 + 120 = 128 + 64 + 8 + 4 + 1 = 205
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
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
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. 170 204 118???
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. *If one uses all of the bits available to represent only positive counting numbers, one is said to be working with unsigned integers.
Bigger Numbers • High-level languages often offer a hierarchy of types that differ in the number of bits used. • You can represent larger numbers than allowed by the highest type in the hierarchy 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 add up to zero x -x
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 just integers or signed.
3-bit signed and unsigned Think of driving a brand new car in reverse. What would happen to the odometer?
Riddle • Is it 214? • Or is it – 42? • Or is it Ö? • Or is it …? • It’s a matter of interpretation • How was it declared?
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.
Numbers from Logic • All of the numerical operations we have talked about are really just combinations of logical operations. • E.g. the adding operation is just a particular combination of logic operations • Possibilities for adding two bits • 0+0=0 (with no carry) • 0+1=1 (with no carry) • 1+0=1 (with no carry) • 1+1=0 (with a carry)
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 • 0111010
Fixed point • If one has a set number of bits reserved for representing the whole number part and another set number of bits reserved for representing the fractional part of a number, then one is said to be using fixed point representation. • The point dividing whole number from fraction has an unchanging (fixed) place in the number.
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