Download Lecture 2: Working with 0’s and 1’s

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Theoretical computer science wikipedia , lookup

Hardware random number generator wikipedia , lookup

Binary search algorithm wikipedia , lookup

Transcript
Lecture 2: Working with 0’s and 1’s
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:
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.
amplitude
9
8
9
8
9
8
6
5
6
Quantisation
issues
4
2
time
1
Sampling issues
•
•
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).
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 2n patterns for a word of n bits
Powers of 2 to know by heart are at least:
20
21
22
23
24
25
26
27
28
29
210
216
1
2
4
8
16
32
64
128
256
512
1024
65536
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)
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).
Kilobyte
(KB)
1024 or
210 bytes
1,024 bytes
Thousands of
bytes
Megabyte
(MB)
10242 or
220 bytes
1,048,578 bytes
Millions of bytes
Gigabyte
(GB)
10243 or
230 bytes
1,073,741,824 bytes
Billions of bytes
Terabyte
(TB)
10244 or
240 bytes
1,099,511,627,776 bytes
Trillions of
bytes
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
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.
27
26
25
24
23
22
21
20
Value
128
64
32
16
8
4
2
1
Binary
Value
1
0
1
0
0
1
0
1
Weight
•
•
Binary
Notation
1010 0101 =
1x128 + 0x64 + 1x32 + 0x16 + 0x8 + 1x4 + 0x2 + 1x1 =
Decimal
Value
165
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.
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:
step
•
•
•
•
remainder
Binary
55/2
= 27
r
1
1 (bit 0)
27 lots of 2 and one unit (20)
27/2
= 13
r
1
1
13 lots of 4 and one 2 (21)
13/2
= 6
r
1
1
6 lots of 8 and one 4 (22)
6/2
= 3
r
0
0
3 lots of 16 and no 8’s (23)
3/2
= 1
r
1
1
1 lot of 32 and one 16 (24)
1/2
= 0 (so now
we stop)
r
1
1 (bit 5)
0 lots of 64 and one 32 (25)
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!.
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!
Decimal
0
1
2
3
4
5
6
7
•
Binary
0
1
10
11
100
101
110
111
Hexadecimal
0
1
2
3
4
5
6
7
Decimal
8
9
10
11
12
13
14
15
Copy this into your logbook and learn it if possible!
Computer Structures
Binary
1000
1001
1010
1011
1100
1101
1110
1111
Hexadecimal
8
9
A
B
C
D
E
F
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.
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:
11111111
10000001
10000000
00000000
00000001
01111111
- 127, all the way down to
-1
-0
+ 0 (notice there are two zeros, which is unnecessary!
+ 1, all the way up to
+ 127
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)
(+13)
(+13)
(+13)
–
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)
+
+
(+7) is the same as
(-7) and
(-7) is the same as
(+7)
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!!
Computer Structures