Title: Level 3 Machine Information Representation
1Level 3 - MachineInformation Representation
- Chapter 3
- CIS-2261
- Foundations in Computer Science
2Homework Chapter 3
Exercises 2, 4, 6ab, 9, 16, 18a-c, 19adf, 23,
25ab, 26bc, 28a, 35, 36
3OVERVIEW
- Unsigned Binary Representation
- 2s Compliment Binary Representation
- Operations in Binary
- Hexadecimal and Character Representation
4Why Binary
- What is special about binary?
- The basic component of a computer system is a
transistor a two state device which switches
between logical 1 on and 0 off (actually
represented as voltages on the range 5V to 0V) - Octal and hexadecimal are bases in powers of 2,
and are used as a shorthand way of writing binary - Get comfortable converting among decimal, binary,
octal, hexadecimal
5Binary Representation
- Numbers must be stored as electrical charges
- Basic unit of storage is called the bit and can
be either a 1 or a 0 (on or off) - Bits are often grouped together into cells
- an 8-bit computer would store its internal
information in groups of 8-bits which are called
BYTES - modern computers store several bytes together in
what is called a WORD - the representation scheme used to store
information is called a code
6Integers
- Numbers must be represented in binary form to be
stored in memory - Code depends on whether the number has a
fractional part or is in integer - Code depends on whether the number is always
positive or whether it can be negative too - Unsigned Binary representation is for integers
that are always positive
7Numbering Systems
- Positional Number Systems position of character
in string indicates a power of the base r
(radix). Common bases 2, 8, 10, 16. - Base 10 (decimal) digits 0, 1, 2, 3, 4, 5, 6, 7,
8, 9 form the alphabet of the decimal system. - e.g. 31610 3102 1101 6100
- Base 8 (octal) digits 0, 1, 2, 3, 4, 5, 6, 7
form the alphabet of the octal system. - e.g. 4748 482 781 480
8Numbering Systems
- Base 16 (hexadecimal) digits 0-9 and A-F.
- e.g. 13C16 1162 3161 C160
- Base 2 (binary) digits 0, 1 form the alphabet.
- e.g. 100110 125 122 121
- In general, radix r numbers use the first r chars
in 09, A...Z to form the alphabet. - e.g. dn-1dn-2d1d0 dn-1rn-1 dn-2rn-2
d0r0
9Base Conversions
- Convert from Base r to Base 10 by expansion of
powers of base r and summing terms - e.g. 101102 ( ???? )10
1 0 1 1 0
1s place 2s place 4s place 8s
place 16s place
1s place 0 2s place 2 4s
place 4 8s place 0 16s place
16
22 (dec)
10Base Conversions
- Method for converting a number from Base 10 to
Base r - Step 1 Divide decimal by r to obtain
Quotient and Remainder - Step 2 The remainder is added to the Base r
result starting at the LSD and increasing in
place value - Step 3 Continue doing steps 1 2 until the
quotient is 0
11Base Conversions
- Convert from Base 10 by repeated division
- e.g. 96410 ( ???? )8
Result
12Base Conversions
- Convert from Base 10 by repeated division
- e.g. 4510 ( ???? )2
Result
13Converting Fractions
- Converting a decimal fraction from Base 10 to
Base r is similar method as used for decimal
integers except multiply the fraction by r
instead of dividing - Step 1 Multiply decimal by r to obtain
Product and Integer part - Step 2 The Integer part is added to the Base r
Result starting at the MSD and decreasing in
place value - Step 3 Continue doing steps 1 2 until the
fractional part of the Product is 0 - NOTE
Sometimes this process does not conclude
14Converting Fractions
- Convert from Base 10 by repeated multiplication
- e.g. .664062510 ( ???? )8
Result
15Converting Fractions
- Convert from Base 10 by repeated multiplication
- e.g. .62510 ( ???? )2
Result
16Arbitrary Base Conversion
- Converting Base x to Base y convert Base x to
Base 10 then convert Base 10 to Base y - Special case converting among binary, octal, and
hexadecimal is easier - Go through the binary representation, grouping in
sets of 3 or 4. - e.g. 110110012 11 011 001 3318
- 110110012 1101 1001 D916
- e.g. C3B16 ( ???? )8
- C3B16 1100 0011 10112 110 000 111 0112
- C3B16 60738
Regroup
17Range for Unsigned Integers
- There is a finite range for representing integers
within a computer - finite number of bits within a cell
- For a seven bit cell
- maximum range of numbers is 000 0000 to 111
1111 (0 to 127 decimal) - Need to always remember that a computer has a
finite range (especially when working with
integers) - range of a 32-bit cell?
18Range Continued
- Ranges of values
- Q Given k positions in base r, how many values
can you represent? - A rk values over the range (0rk-1)10
- r10, k3 1031000 range is (0999)10
- r2, k8 28256 range is (0255)10
- r16, k4 16465536 range is (065535)10
- Q How are negative numbers represented?
19Unsigned Addition
- Rules for unsigned binary addition similar to
unsigned decimal addition only simpler to
actually do
Binary Addition Rules
0 0 0 0 1 1 1 0 1 1 1 10
20Unsigned Addition
Example 2
Example 1
00 0011 01 0000
00 1111 00 1001
ADD
ADD
01 0100
01 1000
21Carry Bit
- So far so good .. But..
- Need to think about range when adding unsigned or
signed numbers - Possible to have two number, each of who are in
range, but with a result that is out of range - The CPU has a special bit (the Carry Bit) that is
set when the addition result is larger than the
range allows
22Carry Bit Addition
Example 2
Example 1
01 0110 10 0010
10 1010 01 1010
ADD
ADD
C 0
11 1000
00 0100
C 1
23Do we need more?
- What about subtraction?
- How will the computer handle that?
- Decisions on how a computer will and will not
handle numbers has a significant impact on higher
order language constructs
24OVERVIEW
- Unsigned Binary Representation
- 2s Compliment Binary Representation
- Operations in Binary
- Hexadecimal and Character Representation
25Twos Complement Representation
- Unsigned binary representation works for
nonnegative integers only - Need to have a way to store and handle negative
numbers - What about representing fractions? Not yet!
- Four common methods for integers simple binary,
signed magnitude, binary coded decimal, and 2s
complement. - Simple binary as seen before, all numbers are
assumed to be positive. e.g. 8-bit representation
of - 4110 0010 10012
26Signed Magnitude
- Signed magnitude simple binary with leading sign
bit. 0 positive, 1 negative. - 4110 0010 10012
- -4110 1010 10012
- What ranges of numbers may be expressed in 8
bits? - Largest 127
- Smallest -127
- What about ZERO ? 1000 0000 or 0000 0000 ?
- Must have additional hardware to handle
- Extend 1010 1001 to 12 bits
- 1000 0010 1001
27Binary Coded Decimal
- Binary Coded Decimal (BCD) use a 4 bit pattern
to express each digit of a Base 10 number - 0000 0 0001 1 0010 2 0011 3
- 0100 4 0101 5 0110 6 0111 7
- 1000 8 1001 9 1010 1011 -
- e.g. 123 0001 0010 0011
- 123 1010 0001 0010 0011
- -123 1011 0001 0010 0011
28Binary Coded Decimal
- BCD Advantages
- Used in business machines and languages i.e.
COBOL for precise decimal math. - Can have arrays of BCD numbers for essentially
arbitrary precision arithmetic.
29Binary Coded Decimal
- BCD Disadvantages
- Takes more memory. 32 bit simple binary can
represent more than 4 billion discrete values.
32 bit BCD can hold a sign and 7 digits for a
maximum of 10 million values. - More difficult to do math. Essentially, we must
force the Base 2 computer to do Base 10
arithmetic.
30Twos Complement
- Twos Complement
- Used by most machines and languages to represent
integers. - Fixes the -0 in the signed magnitude, and
simplifies machine hardware for arithmetic. - Divides bit patterns into a positive half and a
negative half. Zero is considered positive. - n bits creates a range over -2n-1 2n-1-1
31Twos Complement Representation
- Conversion to 2s complement.
- Positive numbers same as simple binary
- Negative numbers
- Obtain the n-bit simple binary equivalent
- Invert the bits of that representation
- Add 1 to the result
- ex Convert -32010 to 16-bit 2s complement
- 32010 0000 0001 0100 00002
- 1111 1110 1011 1111
- 0000 0000 0000 0001
- 1111 1110 1100 0000
- ex Extend the 12-bit 2s complement number
- 1101 0111 1000 to 16 bits.
Invert
Add 1
32Twos Complement Math
- 2s Complement overflow
- Different than overflow of unsigned numbers (use
carry bit) - For signed numbers the CPU uses a new flag called
the overflow bit (v) - Opposite signs on operands cant overflow
- If operand signs are same, but result operand is
different, must have overflow
33Hardware Overflow Detection
- Hardware detects the overflow condition by
comparing the carry into the sign bit of the
result with the carry out of the adder - If they are different, an overflow must have
occurred and the overflow bit (V) gets set to 1 - If they are the same, no overflow has occurred
and the overflow bit (V) gets set to 0
Cin C V
0 0 0 0 1 1 1 0
1 1 1 0
Overflow Bit Truth Table
34Twos Complement Math
Cin
Cin
0
1
00 0011 01 0101
01 0110 00 1100
ADD
ADD
Adding two positive s
C 0
C 0
01 1000
10 0010
V 0
V 1
Cin
Cin
1
0
11 1010 11 0111
10 0110 10 0010
ADD
ADD
Adding two negative s
C 1
C 1
11 0001
00 1000
V 0
V 1
35Twos Complement Math
- Addition and subtraction are the same operation
- Still must be aware of overflow.
- ex 8 bit 2s complement 2310 4510
- ex 8 bit 2s complement 10010 4510
- ex 8 bit 2s complement 2310 - 4510
36OVERVIEW
- Unsigned Binary Representation
- 2s Compliment Binary Representation
- Operations in Binary
- Hexadecimal and Character Representation
37Logical Operations
- All information stored in a computer is in binary
form therefore all processing on that information
must be done using binary operators - So far we have discussed Add, Subtract, and Not
- Can also view the binary information stored as a
logical value with each bit given a logical value
of 1 for true and 0 for false - Can think of doing bit-wise logical operations on
that information as well - Think of the 1s and 0s as level 3
representations for level 6 true and false
38Logical AND Operations
Level 6 Logical AND
Level 3 Logical AND
39Logical OR Operations
Level 6 Logical OR
Level 3 Logical OR
40Logical Operation Examples
Example 2
Example 1
01 0110 10 0010
10 1010 01 1010
AND
OR
00 0010
11 1010
Bit-wise AND
41Arithmetic Operations
- Two other arithmetic operations to discuss
- Arithmetic Shift Left (ASL)
- Arithmetic Shift Right (ASR)
- ASL - Each bit in the cell (byte/word) shifts
left one place - Leftmost bit shifts into Carry Bit
- Rightmost bit gets a zero
- ASL - Each bit in the cell (byte/word) shifts
right one place - Rightmost bit shifts into Carry Bit
- Leftmost bit remains whatever it was
42Arithmetic Operations
C
0
LSB
MSB
Arithmetic Shift Left (Multiply by 2)
C
LSB
MSB
Arithmetic Shift Right (Divide by 2)
43OVERVIEW
- Unsigned Binary Representation
- 2s Compliment Binary Representation
- Operations in Binary
- Hexadecimal and Character Representation
44Hexadecimal Conversions
- Convert from Base 16 to Base 10 by expansion of
powers of base 16 and summing terms - e.g. 8BE716 ( ???? )10
8 B E 7
1s place 16s place 256s place 4096s
place
1s place 7 16s place
224 256s place 2,816 4096s
place 32,768
35,815 (dec)
45Hexadecimal Representation
- Representation of numbers in Base 16 format
- A condensed way of writing binary information
- A natural extension of Base 2 (binary) number
system
46Hexadecimal Representation
- Use Hexadecimal numbers in place of binary
numbers when writing binary numbers - Make groups of four bits starting with the LSB
hF011
Write b 1111 0000 0001 0001
h889A
Write b 1000 1000 1001 1010
47Hexadecimal Representation
Example Write the 10-bit binary number
1010011100 in hexadecimal format
First group bits 10 1001 1100
h29C
Imply leading 0bits 0010 1001 1100
48Hexadecimal Representation
Example If a 12-bit memory location contains
hF7A, find the equivalent decimal number
- First group bits 1111 0111 1010
- Recognize this as a negative number in 2s
complement form so find the magnitude by taking
the 2s complement and then converting that value
to decimal (add the minus sign)
Invert 1111 0111 1010 0000 1000
0101 Add 1 1
Magnitude 0000 1000 0110
49Hexadecimal Representation
Example If a 12-bit memory location contains
hF7A, find the equivalent decimal number
Magnitude 0000 1000 0110
0000 1000 0110
1s place 2s place 4s place
8s place 16s place 32s place 64s
place 128s place
1s place 0 2s place
2 4s place 4 8s place
0 16s place 0 32s place
0 64s place 0 128s place 128
- 134 (dec)
50Character Representation
- Because memory is binary, computers must store
characters in some binary coded form - Earliest system EBCDIC
- 8-bits per character
- used by IBM in mainframes (360 architecture and
descendants) - Most common system ASCII American Standard Code
for Information Interchange - 7/8-bits per character
51Character Representation
- ASCII (continued) 7 and 8 bit ASCII
- Extended ASCII (IBM), 8 bit code includes
graphical symbols, lines. Most significant bit
is set. - Fixed code, 7 bit representations (128 char max)
- Printable characters for human interactions
- Control characters for non-human communication
(computer-computer, computer-peripheral, etc.) -
52Character Representation
- To store TOM BROWN starting at location
h000A in memory
What about the extra bit that is not being used?
53What About Level 6?
- Suppose you declare at level 6
- int i, j
- char ch1, ch2
- At level 3 the computer will store and handle the
integer values in 2s complement - At level 3 the computer will store the character
in ASCII (or other) format
54Relation to Level 6
- If the integer j is equal to -2, the computer
will store that as 111 1110 (2s complement
form) - If -2 is of type character, then the computer
will store it as 010 1101 011 0010 taking up
two bytes of storage in the process - It is not a trivial thing to convert from type
character to type integer
55Relation to Level 6
- To ring the bell, you can execute the C
statements below - int j 7
- char ch j
- cout ltlt ch
- Still need to understand the connection between
level 3 and 6 to make it happen
56END Chapter 3