Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computer Structures Lecture 2: Working with 0’s and 1’s.

Similar presentations


Presentation on theme: "Computer Structures Lecture 2: Working with 0’s and 1’s."— Presentation transcript:

1 Computer Structures Lecture 2: Working with 0’s and 1’s

2 Computer Structures Continuous and Discrete, Analogue and Digital First of all remember that even numbers can be continuous or discrete. We all know about integers, e.g. -10, - 9, …,-2, -1, 0, 1, 2, 3, …9, 10 etc. These are discrete, which means separate from each other, so that there is a gap between one integer and the next and they are in order. They are ordinal. We can label them and count our way through them. We normally label them with decimal digits, but we could equally well count through the list with binary digits or hexadecimal digits or with other labels. They are just like pebbles on the beach, separate and countable. Mathematicians, who work in the abstract, are free to imagine integers as big (+ or -) as they like, without limit. Binary numbers are just integers which we count through using 0’s and 1’s (bits). Computers deal with binary numbers. Computers handle binary numbers as ‘words’ made of a fixed number of bits (usually 8, 16, 32, maybe 64). Because computers always have limited amounts of memory, there is a limit to how many bits can be used to represent any number. So unlike the world of mathematicians, in the world of computers, the range of integers (or binary numbers) they can handle is limited. Mathematicians also define a continuum of real numbers, which go from –infinity through zero to + infinity continuously, i.e. with no steps between one number and the next. So, for example 3.4 doesn’t have a next number, not even 3.40000000000000000000000000000000000000000001, because we could also have 3.40000000000000000000000000000000000000000000000000000000000000001 and so on. So real numbers can’t be counted. We say they are not ordinal (there is no ‘next one’) Real numbers are analogue, in electrical engineering speak. In computers, real numbers have to be approximated somehow as countable binary numbers, so there will be (albeit very small) steps between one real number and the next. So the accuracy of real numbers in computers is always limited. So numbers, data, information and signals can be continuous or discrete. The sound from a trumpet is a continuous, analogue signal, i.e. it doesn’t have any breaks in time in it, as in the next slide:

3 Computer Structures Analogue to Digital conversion Continuous data has to be ‘chopped up’ into discrete binary numbers before it can be handled by a computer. This is called analogue to digital conversion, done by clever electronic circuits which you will study later in your course. Because of sampling and quantisation limitations, the original analogue signal is not exactly represented by the numbers 1, 5, 8, 9, 9, 9, 8, 6, etc. All digitised data suffers from this lack of ‘resolution’, but more samples and more accurate samples improve things greatly (e.g. the move to more and more Megabits per image in digital cameras). time 8 6 4 2 8 5 1 6 8 9 9 amplitude Sampling issues Quantisation issues 9

4 Computer Structures Binary basics: 1 Computers use binary numbers with a fixed numbers of bits, so we must know how many. Don’t just think of any old binary number, but an n-bit binary number (maybe 4 or 8, 16, 32 etc). Also called an n-bit word. The number of bits fixes the number of different possible patterns in the word (and so the range of integers the word can represent, or the number of codes for things) There are 2 n patterns for a word of n bits Powers of 2 to know by heart are at least: For example, a computer using a 16 bit word (65536 patterns) could handle integers between - 37768 and + 37765 (remember one code is needed for zero) 2020 21212 2323 2424 2525 2626 2727 2828 2929 2 10 2 16 1248163264128256512102465536

5 Computer Structures Binary basics: 2 Some other numbers. A word of 8 bits has a special name – a Byte. Usually, this is the smallest bundle of bits which computer hardware deals with – single bits are not moved around or stored individually by your hardware – look at all those busses (parallel circuit board lands or ribbon cables inside the box). Things like Kilobyte or Megabyte don’t exactly mean one thousand or one million bytes – they are rough shorthand for a nearby power of 2. The Googles of the world really do deal in Terabytes Kilobyte (KB) 1024 or 2 10 bytes 1,024 bytes Thousands of bytes Megabyte (MB) 1024 2 or 2 20 bytes 1,048,578 bytes Millions of bytes Gigabyte (GB) 1024 3 or 2 30 bytes 1,073,741,824 bytes Billions of bytes Terabyte (TB) 1024 4 or 2 40 bytes 1,099,511,627,776 bytes Trillions of bytes

6 Computer Structures Binary basics: 3 In a decimal (base 10) number, each digit represents a power of 10. In a binary number, like the byte below, each digit represents a power of 2. Get used to drawing boxes round binary numbers, as above – it helps not to lose track of where the powers of 2 are. By drawing boxes like this, it easy to convert any binary number to decimal. Weight 27272727 26262626 25252525 24242424 23232323 22222222 21212121 20202020 Value1286432168421 Binary Value 10100101 Binary Notation Decimal Value 1010 0101 = 1x128 + 0x64 + 1x32 + 0x16 + 0x8 + 1x4 + 0x2 + 1x1 = 165

7 Computer Structures Binary basics: 4 converting from decimal to binary Just a bit more tricky. You have to break up the decimal number into parts which are all powers of two. Do it by successive division by 2, like the example for decimal 55 below: So 55 as a binary number is 110111 Odd numbers will always have a 1 as the lsb! Notice that this process generates the binary number lsb first, msb last. It is easy to convert any decimal number to binary – you could write a C++ program to do this!. stepremainderBinary 55/2=27r11 (bit 0)27 lots of 2 and one unit (2 0 ) 27/2=13r1113 lots of 4 and one 2 (2 1 ) 13/2=6r116 lots of 8 and one 4 (2 2 ) 6/2=3r003 lots of 16 and no 8’s (2 3 ) 3/2=1r111 lot of 32 and one 16 (2 4 ) 1/2=0 (so now we stop) r11 (bit 5)0 lots of 64 and one 32 (2 5 )

8 Computer Structures Hexadecimal Notation Hexadecimal notation is just a convenient way of writing down binary numbers without getting a 0’s and 1’s headache! Copy this into your logbook and learn it if possible!

9 Computer Structures Addition and Subtraction by hand Addition follows exactly the same method you learnt at school using decimal numbers. –With several binary numbers to add together, there might be a big carry – so there could be carries into columns further to the left than just the next column. This would be a bit unusual for decimal addition. To avoid getting things mixed up, always draw boxes so that the various rows and columns involved are very clear – even use a separate row for the carries. Neatly write out the example from the lecture: 9 + 13 + 7 + 14 = 43 to convince yourself that you can do it, and then do some more examples, e.g. 21+34+1, 157+63, etc. Subtraction by hand is more of a headache – keeping track of ‘borrows’ is harder than keeping track of ‘carries’. But the principle is exactly as you learnt at school. Again good organisation is essential. Copy this into your logbook and learn it if possible!

10 Computer Structures Representing negative integers There are two common ways to do this for binary numbers. Sign Magnitude representation is the most obvious, because it is what we learnt to use at school for decimal numbers. –We put a + or a – symbol at the front of the decimal digits telling us how big the number is. So the number is two parts, a sign symbol and a magnitude. –Everything in a binary number must be a 0 or 1 (called a bit), so we have to use one bit (the left-most one) for the sign and the rest for the magnitude. 1 means negative and 0 means positive. In sign magnitude therefore, the byte (8 bit word) below –1 1 0 1 1 0 0 1 (D9 in Hex) means – 89, and –0 1 0 1 1 0 0 1 (59 in Hex) means + 89 –The range of sign magnitude integers in an 8 bit word (byte) is therefore: 1 1 1 1 1 1 1 1- 127, all the way down to 1 0 0 0 0 0 0 1- 1 1 0 0 0 0 0 0 0- 0 0 0 0 0 0 0 0 0+ 0 (notice there are two zeros, which is unnecessary! 0 0 0 0 0 0 0 1+ 1, all the way up to 0 1 1 1 1 1 1 1+ 127

11 Computer Structures Problems with Sign-magnitude numbers, Two’s Complements In a CPU, numbers have to be handled by electronic circuits, which, for sign-magnitude numbers leads to unwanted complications: –Checking all the time for both of the two bit patterns for zero (wastes time) –In addition or subtraction, checking the sign of the operands separately, the type of operation (i.e. addition or subtraction) and then deciding how to do it – plus adding on the right sign to the result afterwards. (+13) + (+7), (-13) + (+7), (-13) – (-7) etc, all the different possibilities. –All these lead to complex circuits –Binary subtraction circuits are much more complex than binary add circuits, so Can we subtract without a subtraction circuit? Answer YES. –Remember (+13) - (+7) is the same as – (+13) + (-7) and – (+13) - (-7) is the same as – (+13) + (+7) –So the answer is to find a way to subtract by adding an appropriate complement of the number we are trying to subtract (we can even do this with decimal numbers) For binary numbers, the trick is to use what are called two’s complements. –The two’s complement of +n represents –n and vice versa –All the operations above then work correctly using the same binary adder circuits!!


Download ppt "Computer Structures Lecture 2: Working with 0’s and 1’s."

Similar presentations


Ads by Google