Download Data Representation Outline Radix Numbering Systems From Radix

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

Manchester Mark 1 wikipedia , lookup

Transcript
Outline!
Data Representation!
COMP 251!
Computer Organization and Architecture!
Fall 2009!
Radix Numbering Systems!
!!Radix is the base value in a numbering system.!
"!E.g. Radix 10 is the base 10 numbering system of
which we are all so fond.!
"!Radix 10 (Base 10 / Decimal):!
#!Digits: 0,1,2,3,4,5,6,7,8,9!
#!Example:!
•! 67210 !
!= 6*102+7*101+2*100!
!
!
!
!= 6*100+7*10+2*1 !
!
!
!
!= 67210!
From Base 10 to Radix X!
!!Radix 4:!
"!Digits: !
"!Example!
#!22510!
!!Radix 7:!
"!Digits:!
"!Example: !
#!141310!
!!Radix number systems!
!!Computer data representation!
"!Unsigned integers!
"!Characters!
"!Signed integers!
"!Decimal numbers!
From Radix X to Base 10!
!!Radix 3: !
"!Digits: 0,1,2!
"!Example!
#!10213!
!!Radix 5:!
"!Digits:!
"!Example: !
#!24135!
Computer Data Representation!
!!Computers use 0’s and 1’s to represent all of the data
(numbers / characters / graphics) and instructions that
they store, manipulate or execute.!
"!0’s and 1’s are used because they are technically easy to
represent and manipulate.!
#!presence of a voltage ! 1!
#!absence of a voltage ! 0!
"!The computer’s memory holds a sequence of 0’s and 1’s!
#!1011 1010 0011 0101 0001 0100 0011 1110!
#!The meaning of those 0’s and 1’s depends upon how they are
interpreted.!
Computer Data Terminology!
Interpreting 0’s and 1’s!
!!Some ways computers interpret 0’s and 1’s:!
"!Positive integers!
#!Unsigned Binary / Binary coded decimal!
"!Word: collection of 1 or more bytes.!
"!Byte: 8 bits.!
"!Nibble: 1/2 byte.!
"!Characters!
#!EBCDIC / ASCII / UNICODE!
"!Signed integers!
#!Sign magnitude / One’s Complement / Two’s Complement!
"!Decimal numbers!
"!Most significant bit (MSb) or byte (MSB)!
"!Least significant bit (LSb) or byte (LSB)!
Unsigned Binary Interpretation!
!!Binary (Base / Radix 2): !
"!Digits: 0,1!
"!Example:!
#!Floating point!
"!Machine Instructions!
Binary Coded Decimal (BCD)!
!!Each nibble represents a digit in a decimal number:!
"!Example:!
#!1001 0110 0011 0101!
#!1010 10112 !!
"!Example:!
#!9910!
Embrace your inner geek…!
Unsigned Binary Addition!
!! Addition of Unsigned Binary Numbers:!
www.thinkgeek.com!
"! Examples:!
!
!!
! 0101 0110 !
!+ 0010 1000 !
!
!
! 86 !!
!+40"
0110 1010 !
+ 1000 1010 !
!
!
! 106"
!+138!
Unsigned Binary Addition!
!!Addition of Unsigned Binary Numbers:!
"!Another Example:!
!!
!!
!!
! 1011 0100
!!
!+ 0111 0010
!!
!!
!
!
Unsigned Binary Representation!
!!An interesting view:!
! 180!
!+ 114 !
!!Arithmetic Overflow: Occurs when an arithmetic
operation yields a value that is out of range for the
representation and number of bits being used.!
"!For example: 294, the correct answer above is out of range
for unsigned binary representation using 8 bits.!
Hexadecimal Numbers!
!!Hexadecimal (Base 16):!
"! Digits: 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F!
#!A16=1010, B16=1110, C16=1210, D16=1310, E16=1410, F16=1510!
Hexadecimal!
!!Hex provides a compact way to express binary
values:!
"!Every nibble represents exactly one hex digit.!
"! Example:!
#!0x3A7F
!
!
!
!
!=
!= 3*4096+10*256+7*16+15*1!
!= 1497510!
"! Example:!
#!1109810
!= 0x2B5A!
3*163+A*162+7*161+F*160!
#!1010 0110 = 0xA6!
•! 10102
•! 01102
!= 1010!= 0xA!
!= 610 != 0x6!
"!Every hex digit represents exactly one nibble.!
Embrace your inner geek…!
#!0x7E = 0111 1110!
•! 0x7
•! 0xE
!=710 !=01112!
!=1410 !=11102!
Representing Character Data!
!!To represent character data, the general
approach is simply to assign a unique pattern
of bits (i.e. an unsigned integer value) to each
character.!
"!Standards exist for such assignments:!
#!EBCDIC!
#!ASCII!
#!UNICODE!
www.thinkgeek.com!
EBCDIC!
ASCII!
!!Enhanced Binary Coded Decimal
Interchange Code!
!!American Standard Code for Information
Interchange!
!!Example:!
"!TEST!
#!1110 0011!
#!1100 0101!
#!1110 0010!
#!1110 0011!
ASCII & Parity Checking!
Embrace your inner geek…!
!!Parity Checking is a simple mechanism for
detecting single bit errors in transmission:!
"!7 bits were used to code for one of 128 different
characters.!
"!8th bit was a parity bit used for error detection.!
#!Even Parity Examples:!
•! A " 65 = p100 0001!
•! For even parity, set p = 0 to keep number of 1 bits even."
•! C " 67 = p100 0011!
•! p = 1.!
www.thinkgeek.com!
Unicode!
Signed Binary Whole Numbers!
!!There are several distinct ways that signed whole
numbers can be represented in binary:!
"!Sign Magnitude Representation!
"!One’s Complement Representation!
"!Two’s Complement Representation!
"!Signed BCD (zoned / packed)!
Signed Base 10 Whole Numbers!
!!In base 10 we use the convention of writing a minus
sign ( - ) in front of the number to indicate that it is
negative.!
"!For example:!
Signed Binary Numbers!
!!In sign magnitude, one’s complement and two’s
complement representations:!
"!Signed binary numbers with MSb = 0 are positive.!
#!0110 0011
!0011 0110!
!0101 1001!
#!The value of a positive number is given using unsigned
binary representation.!
#!-9913!
#!-23!
!!Multiplying a number by -1 gives the complement of
the number.!
"!Signed binary numbers with MSb = 1 are negative.!
"!For example: !
#!1001 0110
!1000 0010
!1100 1011!
#!The value of a negative binary number is -1 times the
value of its complement.!
#!-1 * 321 = -321!
#!-1 * -86 = 86!
Sign Magnitude Representation!
!!The complement of a number in Sign Magnitude
Representation is found by flipping the MSb.
!
! !-1 * 1011 01012SM = 0011 01012SM
! !
!
!"
!
-1 * 0110 11002SM = 1110 11002SM!
!= 10310!
#!1110 01112SM
!
!
!
!
!= -1 * 0110 01112SM "
!= -1 * 10310"
!= -10310
!!
Two’s Complement Representation!
!!The complement of a number in Two’s Complement
Representation is found by flipping all of the bits and
adding 1.!
!!-1 * 1011 01012TC = 0100 10112TC!
! !
!
!!
!!
!!
!!
! !-1 * 1011 01012OC = 0100 10102OC
!
0100 1010 <flip bits>
!+0000 0001 <add one>!
! 0100 10112TC!
!!
!"
-1 * 0110 11002OC = 1001 01002TC!
! !
! 1001 0011 <flip bits>
!!
!+0000 0001 <add one>!
! 1001 01002TC!
!
!"
!
-1 * 0110 11002OC = 1001 00112OC!
"!Examples:!
#!0110 01112SM
!!
!!
!!The complement of a number in One’s Complement
Representation is found by flipping all of the bits.!
! !
"!Examples:!
!!
One’s Complement Representation!
#!0110 01112OC
!= 10310!
#!1110 01112OC
!
!
!
!
!= -1 * 0001 10002OC "
!= -1 * 2410"
!= -2410
!!
Two’s Complement Representation!
!!Examples:!
#!0110 01112TC
!= 10310"
!
!
!
!
#!1110 01112TC
!
0001 1000 <flip bits> "
!
+0000 0001 <add one>!
!= -1 * 0001 10012TC"
!
!
!
!
!= -1 * 2510!
!=
-2510!
Signed BCD!
!!Examples:!
"!BCD (unsigned)!
#!272!
•! 0010 0111 0010!
"!Zoned Decimal (signed)!
#!272!
•! 1100 0010 1111 0111 1111 0010!
#!-14!
•! 1101 0001 1111 0100!
"!Packed Decimal (signed)!
Signed Binary Arithmetic!
!!Modern computers use two’s complement to
represent signed whole numbers.!
"!Two’s complement has become dominant because
it simplifies arithmetic.!
"!Sign magnitude, one’s complement and signed
BCD have many special cases that would need to
be handled.!
#!272!
•! 0010 0111 0010 1100 !
#!-14!
•! 1111 0001 0100 1101!
Two’s Complement Addition!
!!Examples:!
0010 0110
+ 1001 1100
!
38!
!+ -100!
Detecting Arithmetic Overflow!
!!Recall that arithmetic overflow occurs when an
arithmetic operation yields a value that is out
of range for the representation and number of
bits being used.!
"!Shortcuts for detecting overflow:!
#!Unsigned Binary Representation!
1011 0011
+ 1101 1010
! -77!
!+ -38!
•! If addition results in a carry out from the MSb!
#!Two’s complement!
•! If adding a positive and a positive gives a negative.!
•! If adding a negative and a negative gives a positive.!
Two’s Complement Subtraction!
!!Example:!
0110 0100
- 0001 0100
!
!
100!
- 20!
Two’s Complement Representation!
HLL Data Types!
Ranges for Data Types!
!!The number of bits used to store whole number
values in most programming languages
depends on the type of the variable:!
"! !Java Type:
!C/C++ Type:
!char
short int
!int
long int
!byte
!short
!int
!
!long
!
!
!
!
!
!Size:"
!8 bits"
!16 bits"
!32 bits"
!64 bits !!
!!Given n bits, what range of values can be
represented in:!
"!Unsigned representation?!
"!Two’s complement representation?!
#!Java uses two’s complement for all whole number data types.!
#!C/C++ uses either two’s complement for signed types and unsigned
for unsigned types.!
•! short int x = -1293; !
!
•! unsigned short int x = 23179;
!// 2’s comp.!
!// unsigned!
HLL Data Types!
!!Ranges for different data types:!
"! !Java Type:
!byte !
!Size:
!8 bits
!short
16 bits !
!-32,768…32,767"
!
!
!32 bits !
!
!
!-2,147,483,648…!
!+2,147,483,647"
!long
!
!
64 bits !
!
!
!-9,223,372,036,854,775,808…"
!+9,223,372,036,854,775,808!
!int
! !
!
!
!Range: "
!-128…127"
Fixed Point Representation!
!!Examples:!
6*100+3*10-1+4*10-2+3*10-3+7*10-4+5*10-5"
6*1+3*0.1+4*0.01+3*0.001+7*0.0001+5*0.00001!
6.34375!
"!0110.01011!
"
1*4+1*2+1*1/4+1*1/16+1*1/32!
4+2+0.25+0.0625+0.03125!
6.34375!
!!Rational Numbers:!
"!Example: 12.25!
#!Decimal:
!49/4!
#!Binary:
!0011 0001 / 0000 0100!
!!Fixed Point:!
"!Example: 115.625!
#!Binary:
!0111 0011.101!
!!Floating Point (Scientific Notation):!
"!Example: 115.625!
#!Decimal:
!11562.5 x 10-2!
!
!or
!0.115625 x 103!
#!Binary:
!111 0011 101.0 x 2-3!
!
!or
!1.11 0011 1010 x 26!
Fixed Point Representation!
!! Represent each number using eight bit fixed point
representation with 4 bits to the right of the binary point.!
!! Example 2:!
!! Example 1:!
"!6.34375!
1*22+1*21+1*2-2+1*2-4+1*2-5
Representing Fractions!
"! 5.312510 = 0101.01012!
#!510 !
!= 01012!
#!0.312510 != 0.01012!
! 0.3125"
-0.0
0.3125"
-0.25
0.0625"
-0.0
0.0625"
-0.0625
0.0!
!0*2-1"
!1*2-2"
!0*2-3"
!1*2-4"
"! 3.1415910 # 0011.00102!
#!310 !
!= 00112!
#!0.1415910 !# 0.00102"
! 0.14159"
-0.0
!0*2-1"
0.14159"
-0.0
!0*2-2"
0.14159"
-0.125
!1*2-3"
0.01659"
-0.0
!0*2-4"
0.01659!
Rounding Modes!
Floating Point Representation!
!!When dealing with binary fractions rounding
becomes an issue.!
!!Example: !
"!Base 10:!
#!115.875!
#!115875 x 10-3!
#!1.15875 x 102 !!
"!Example: !
#!Represent 4.49 using 8 bit fixed point representation with 4 bits to
the right of the binary point.!
"!Base 2:!
"!Rounding Modes:!
#!0111 0011.101!
#!0111 0011 101 x 2-3!
#!1.11 0011 101 x 26!
#!Round to nearest.!
#!Round to zero.!
#!Round to +$!
#!Round to -$!
Floating Point Representation!
Normalized Form!
!!A 14 bit Model:!
!!Normalized Form: The scientific notation
form of a number with exactly one significant
digit to the left of the radix point.!
n10 = ("1)
"!Examples:!
#!23793.13 = 2.379313 x 104!
#!0.003298 = 3.298 x 10-3!
#!1001000.0110 = 1.0010000110 x 26!
#!0.0101101 = 1.01101 x 2-2!
Sign
# 2( Exponent"excess) # Significand
"! Sign:!
#!0 = positive!
#!1 = negative !
!
"! excess: Subtracted from the unsigned Exponent to permit
representation of negative exponents.!
#!excess = 1/2 of the range for unsigned representation with the number of
bits in the exponent.!
#!e.g. excess = 25/2 = 32/2 = 16 in our 14 bit model.!
"! Normalized values will have an implied 1 in"
the significand to the left of the binary point.!
Problems with Normalized Forms!
!!Using normalized forms usually makes sense
but it has two problems:!
"!There is no way to represent zero as a normalized
floating point number.!
0 00000 0000 0000 "
2-16!
"!The size of the smallest numbers that can be
represented is larger than it needs to be."
Denormalized Forms!
!!When the exponent of a floating point number is all
0’s it is defined to be in denormalized form.!
"!In denormalized form the implied bit to the left of the
binary point is a 0 instead of a 1 as it is in normalized
forms.!
#!Example:!
0 00000 0011 0000 " 1 • 2-16 •(2-3 + 2-4)!
#!Note that now we can also represent 0:!
0 00000 0000 0000 " 1 • 2-16 •(0)
Floating Point Arithmetic!
Floating Point Arithmetic!
!!Addition:!
"!Example:!
!!Addition:!
#!14 bit, excess 16, base 2 normalized floating point:!
"!Example:!
#!Base 10:!
2.225 x 101 !!
+ 8.75 x 10-1 !
!
!
!
!
222.5 x 10-1!
!!
+ 8.75 x 10-1 !
!! 231.25 x 10-1!
!
!
!
!%!
!
!!
!
! !
!
!
!
!!
0 10100 0110 0100
+ 0 01111 1100 0000
! 22.25!
!+ 0.875!
1.011 0010 x 24 !!
+ 1.110 0000 x 2-1!
!
!
!
!
101100.1000000 x 2-1!
+!!
1.1100000 x 2-1!
!!
101110.0100000 x 2-1!
!
!
!
!
!
!!
!%!
1.0111001 x 24!
!%!
0 10100 01110010!
2.3125 x 101!
Floating Point Errors!
!!Overflow: When a result is out of range for the
representation and number of bits being used.!
"!For 14 bit, excess 16, base 2 floating point!
#!Largest Negative:
#!Largest Positive:!
Floating Point Errors!
!!Precision Errors: Some numbers can not be
represented exactly:!
#!Example:
!
!
!
!
!
!
!
-1.1111
1111 x 215!
!!
1.1111 1111 x 215!
!!Underflow: When a result is too close to zero for the
representation and number of bits being used.!
"!For 14 bit, excess 16, base 2 floating point!
#!Smallest Negative: -0.0000
!!
0001 x 2-16!
0.0000
0001 x 2-16!
#!Smallest Positive:
!!
!109.125 = 1101101.001"
!
!
1.101101001 x 26"
!
!
! % !
!
!0 10110 1011 0100
!
!
! %!
!
!
!109.0!
!
"!Round-off Errors: Another name for precision errors particularly when they occur in the results of arithmetic
operations.!
#!See the Failure2 & Failure3 examples from class #1.!
Error Propagation!
!!Error Propagation: The accumulation of round-off
errors during a sequence of floating point arithmetic
operations.!
IEEE 754 Standards!
!! Single Precision (float):!
"!
"!
"!
"!
Excess: 127!
Range: ±3.4x1038!
Underflow: ±3.4x10-38!
Special values:!
!Sign
!0 !
!1 !
!0/1
!0/1
!Exponent
!1111 1111
!1111 1111
!1111 1111
!0000 0000
!Significand
!0000…0000
!0000…0000
!????…????
0000…0000
!Meaning!
!+ Infinity!
!- Infinity!
!NAN!
!Zero!
"! Normalized form is used unless the exponent is all 0’s
which indicates denormalized form.!
IEEE 754 Standards!
!! Double Precision (double):!
"!
"!
"!
"!
Excess: 1023!
Range: ±1.7x10308!
Underflow: ±1.7x10-308!
Special values and normalization are analogous to Single
Precision.!