Beruflich Dokumente
Kultur Dokumente
Humans use Decimal Number System Computers use Binary Number System Important to understand Decimal system before looking at binary system Decimal Numbers - Base 10
10 digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 Positional number system: the position of a digit in a number determines its value Take the number 1649
The 1 is worth 1000 The 9 is worth 9 units
Formally, the digits in a decimal number are weighted by increasing powers of 10 i.e. they use the base 10. We can write 1649 in the following form:
Distinguishing Decimal from other number systems such as Binary, Hexadecimal (base 16) and Octal (base 8)
How do we know whether the number 111 is decimal or binary One convention is to use subscripts Decimal: 11110 Binary:1112
Difficult to write use keyboard
Hex: 11116
Octal: 1118
Formally, the digits in a binary number are weighted by increasing powers of 2 They operate as decimal numbers do in all other respects Consider the binary number 0101
Weight 27 bits 0 26 1 25 0 24 1
1100
23 1 22 1 21 0 20 0
01011100 = 0*27 + 1*26 + 0*25 + 1*24 + 1*23 + 1*22 + 0*21 + 0*20 = 0 + 6410 + 0 + 1610 + 810 + 410 + 0 + 0 = 9210
Representing Numbers: Binary Numbers Leftmost bit is the most significant bit (MSB).
The leftmost bits in a binary number are referred to as the high-order bits.
= 1000112.
7
Shortcuts
To convert any decimal number which is a power of 2, to binary, simply write 1 followed by the number of zeros given by the power of 2. For example, 32 is 25, so we write it as 1 followed by 5 zeros, i.e. 10000; 128 is 27 so we write it as 1 followed by 7 zeros, i.e. 100 0000. Remember that the largest binary number that can be stored in a given number of bits is made up of n 1s. An easy way to convert this to decimal, is to note that this is the same as 2n - 1. For example, if we are using 4-bit numbers, the largest value we can represent is 1111 which is 24-1, i.e. 15
Review
Decimal Number System - Base 10 Significant Digits Binary Number System - Base 2 Notation (B,D,H,O) Binary to Decimal Decimal to Binary Shortcuts and Common Binary Numbers
Review Questions
What is a positional number system ? What is the MSB and the LSB. Give an example of each one. Show how the weights of the bits in an
8-bit binary number 16-bit binary numbe
What is the weight of the MSB in (a) 8-bit number (b) 16-bit number (c) 32-bit number Convert 48D, 65D, 31D, 15D to binary Convert 1111 0111B, 1010 1010B and 1110 0111B to decimal
Comp 1001: IT & Architecture - Joe Carthy
10
= = = =
+ + +
11
12
= = = =
+ + +
65D = 41H
Exercise: Convert 97D, 48D, 255D to Hexadecimal
13
Hexadecimal to Binary
Convert each Hex digit to a 4-bit binary number
14
Decimal Hex
111100111011B = F3BH Exercise: Convert 11 1111 1010B; 111 1101 1111B and 1111 1111 1111 1111B to Hexadecimal
15
Signed Numbers
How do we represent negative numbers ? Humans use a symbol to indicate number sign: - or + In computer we only have binary: 1s and 0s . Two common methods for representing signed numbers Signed Magnitude Twos Complement (2s Complement)
In the following assume we are working with 8-bit numbers
Signed Magnitude We designate the leftmost bit i.e the MSB as a sign bit The sign bit indicates whether a number is positive or negative 0 sign bit => positive number 1 sign bit => negative number The remaining bits give the magnitude of the number
Comp 1001: IT & Architecture - Joe Carthy
16
Signed Numbers
Example +15 and -15 as 8-bit numbers +15 => 0 000 1111B -15 => 1 000 1111B MSB = 0 => + MSB = 1 => -
Note the magnitude is comprised of 7 bits Largest positive number is 0111 1111 => +127
17
In the case of negative numbers, the sign bit is 1 but the bits to the right of the sign bit do not directly indicate the magnitude of the number.
Comp 1001: IT & Architecture - Joe Carthy
18
Bits 1 0 0 0 0 Weights -128 +64 +32 +16 +8 Value = -128 + 2*1 + 1*1 = -128 +3 = -125D So 1000 0011B = -125D
0 +4
1 +2
1 +1
Exercise: Convert the 2s complement numbers 1000 0111B and 1000 1111B to decimal.
19
In 2s complement each number has a unique representation i.e. the negative representation of a number uses a completely different bit pattern than its positive counterpart. In any number system: +x - x = 0
20
2s Complement Arithmetic
In 2s Complement, we do not need subtraction to compute x - y We simply add -y to x to get the result. This makes it easier to design the hardware to implement 2s arithmetic It much more complicated with signed magnitude e.g to compute +2 - 6 we must always subtract the smaller number from the larger one and then take the sign of the larger number. Quick Conversion to/from 2s Complement Use the rule: Flip the bits and Add 1
21
0000 0000B + 1B => 0000 0001B => 1D Remember the sign bit was 1 => negative So 1111 1111B => -1D
Comp 1001: IT & Architecture - Joe Carthy
22
1111 1110B + 1B => 1111 1111B => -1D => 1111 1111B Exercise: Convert the following 2s complement numbers to decimal: 1111 1110; 1000 0000; 100 0001; 1111 0000 Decimal to 2s complement: -128 ; -65; -2
Comp 1001: IT & Architecture - Joe Carthy
23
1000 0000B (i.e. -128) is the largest negative 8-bit 2s complement number 0111 1111 (127) is the largest positive 8-bit 2s complement number 256 numbers can be represented using 8-bit twos complement numbers from -128 to 127. There is only one representation for zero. The table below lists the decimal equivalents of some 8-bit 2s complement and unsigned binary numbers.
Bit Pattern 0000 0000 0000 0001 0000 0010 0111 1110 0111 1111 1000 0000 1000 0001 1111 1110 1111 1111 Unsigned 0 1 2 126 127 128 129 254 255 2's Complement 0 1 2 126 127 -128 -127 -2 -1
24
Exercise: What is the number range of 4-bit, 10-bit, 20-bit, 30-bit and 32-bit numbers ?
25
Most PCs now use 32-bit addresses. Original PC (1981) used 20-bit addresses. Early Macintoshs used 24-bit addresses.
Comp 1001: IT & Architecture - Joe Carthy
27
Review
Hexadecimal: 16 digits: 0 to 9 and A to F. Easy to convert to binary Signed Numbers: Signed Magnitude and 2s Complement Sign bit: MSB 1 => negative 2 Complement: Flip the bits and add 1. Number range is important Given an n-bit number, we can represent unsigned numbers in the range 0 to 2n -1 and twos complement numbers in the range -2n-1 to 2n-1 -1.
A 20-bit address allows up to 220 (1Mb) memory locations to be accessed A 30-bit address allows up to 1Gb (230 bytes) of RAM to be accessed A 32-bit address allows up to 1Gb (230 bytes) of RAM to be accessed 28
Overflow
What happens if we attempt to store a larger unsigned value than 255 in 8-bits? For example, if we attempt the calculation 70 + 75 using 8-bit twos complement numbers, the result of 145 (1001 0001B) is a negative number in 2's complement! This situation, when it arises is called overflow. It occurs when we attempt to represent a number outside the range of numbers that can be stored in a number of bits. Overflow is detected by the hardware of the CPU. This allows the programmer to test for this condition in an assembly language program and deal with it appropriately.
29
Overflow
The figure below illustrates the relationship between number range and overflow for 8-bit twos complement numbers.
Overflow Region
Number Range
Overflow Region
-128
127
30
Exercises
Convert the following twos complement numbers to decimal: FFFFh ; F000h; 1000h; 1001h What is the range of unsigned numbers that can be represented by 20-bit, 24-bit and 32-bit numbers? What is the range of numbers that can be represented using 32-bit twos complement numbers? What problem arises in representing zero in signed magnitude? What is overflow and how might it occur?
31
33
34
35
1 bit
8 bits
23 bits
S Exponent
sign exponent
Mantissa
mantissa
36
Integers
byte short int long
Real numbers
float double
Other types
char boolean
37
Integers
byte short int -128 to 127 -32,768 to 32,767 -2,147,483,648 to 2,147,483,467 -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 -(27) to 27-1 -(215) to 215-1 -(232) to 232-1 -(264) to 264-1
long
Real numbers float +/- 1.40239846 * 2-45 to +/- 3.40282347 * 238 double +/- 4.94065645841246544 * 2-324 to +/- 1.79769313486231570 * 2308
38
39
Review
Floating point numbers are represented in scientific notation In binary: m x 2exp There are different standards for representing floating point numbers There are different precisions: single and double Languages like Java and C allow you specify the type of number Micro-controllers are embedded processors with some RAM Code for micro-controller needs to be designed to use as little RAM as possible.
40