SlideShare a Scribd company logo
Chapter 2 - Data Types
Today… Unit 1 slides and homework in JFSB B115 Problems with CCE on Talmage Window’s machines… Drivers have to be installed with administrator privileges Ready shortly… Help Sessions begin Monday @4:00 pm Reading Assignments on-line under the Schedule Tab Concerns or problems??
Concepts to Learn… Binary Digital System Data Types Conversions Binary Arithmetic Overflow Logical Operations Floating Point Hexadecimal Numbers ASCII Characters
What are Decimal Numbers? “Decimal” means that we have  ten  digits to use in our representation  the  symbols  0 through 9 What is 3,546? 3   thousands   +   5   hundreds   +  4   tens   +  6   ones . 3,546 10  = 3  10 3   +  5  10 2   +  4  10 1   +  6  10 0 How about negative numbers? Use two more  symbols  to distinguish positive and negative, namely,  +  and  -. Digital Binary System
What are Binary Numbers? “Binary” means that we have  two  digits to use in our representation the  symbols  0 and 1 What is 1011? 1   eights   +   0   fours   +  1   twos   +  1   ones 1011 2  = 1  2 3   +  0  2 2   +  1  2 1   +  1  2 0 How about negative numbers? We don’t want to add additional symbols So… Digital Binary System
Binary Digital System Binary  (base 2) because there are two states, 0 and 1. Digital  because there are a finite number of symbols. Basic unit of information is the  binary digit , or  bit . Bit values are represented by various  physical  means. Voltages Residual magnetism Light Electromagnetic Radiation Polarization Values with more than two states require multiple bits. A collection of  2  bits has  4  possible states:  00, 01, 10, 11 A collection of  3  bits has  8  possible states:  000, 001, 010, 011, 100, 101, 110, 111 A collection of  n  bits has  2 n  possible states. Digital Binary System
Electronic Representation of a Bit Relies only on  approximate  physical values. A logical ‘1’ is a relatively high voltage (2.4V - 5V). A logical ‘0’ is a relatively low voltage (0V - 1V). Analog processing relies on exact values which are affected by temperature, age, etc. Analog values are never quite the same. Each time you play a vinyl album, it will sound a bit different. CDs sound the same no matter how many times you play them. Digital Binary System
The Power of the Bit… Bits rely on approximate physical values that are not affected by age, temperature, etc. Music that never degrades. Pictures that never get dusty or scratched. By using groups of bits, we can achieve high precision. 8 bits  => each bit pattern represents 1/256. 16 bits => each bit pattern represents 1/65,536 32 bits => each bit pattern represents 1/4,294,967,296 64 bits => each bit pattern represents 1/18,446,744,073,709,550,000 Disadvantage: bits only represent  discrete  values Digital = Discrete Digital Binary System
Binary Nomenclature Binary Digit:  0 or 1  Bit (short for binary digit):  A single binary digit  LSB (least significant bit):  The rightmost bit  MSB (most significant bit):  The leftmost bit  Data sizes 1 Nibble (or nybble) = 4 bits  1 Byte = 2 nibbles = 8 bits  1 Kilobyte (KB) = 1024 bytes  1 Megabyte (MB) = 1024 kilobytes = 1,048,576 bytes  1 Gigabyte (GB) = 1024 megabytes = 1,073,741,824 bytes Digital Binary System
What Kinds of Data? All kinds… Numbers  – signed, unsigned, integers, floating point, complex, rational, irrational, … Text  – characters, strings, … Images  – pixels, colors, shapes, … Sound  – pitch, amplitude, … Logical  – true / false, open / closed, on / off, … Instructions  – programs, … … Data type:  representation  and  operations  within the computer We’ll start with numbers… Data Types
Some Important Data Types Unsigned integers only non-negative numbers 0, 1, 2, 3, 4, … Signed integers negative, zero, positive numbers … , -3, -2, -1, 0, 1, 2, 3, … Floating point numbers numbers with decimal point PI = 3.14159 x 10 0 Characters 8-bit, unsigned integers ‘ 0’, ‘1’, ‘2’, … , ‘a’, ‘b’, ‘c’, … , ‘A’, ‘B’, ‘C’, … , ‘@’, ‘#’, Data Types
Unsigned Integers What do these unsigned binary numbers represent? 3x100 + 2x10 + 9x1 = 329 1x4 + 0x2 + 1x1 = 5 0000 0110 1111 1010 0001 1000 0111 1100 1011 1001 Weighted positional notation “ 3” is worth 300, because of its position, while “9” is only worth 9 Data Types 329 10 2 10 1 10 0 101 2 2 2 1 2 0 most significant least significant
Unsigned Integers  (continued…) Data Types 7 1 1 1 6 0 1 1 5 1 0 1 4 0 0 1 3 1 1 0 2 0 1 0 1 1 0 0 0 0 0 0 2 0 2 1 2 2
Unsigned Binary Arithmetic Base 2 addition – just like base 10! add from right to left, propagating carry 10010 10010 1111 + 1001   + 1011 + 1 11011 11101 10000 10111 + 111 Subtraction, multiplication, division,… 0 1 1 1 1 Data Types carry
Signed Integers With n bits, we have 2 n  distinct values. assign about half to positive integers (1 through 2 n-1 ) and about half to negative (- 2 n-1  through -1) that leaves two values: one for 0, and one extra Positive integers just like unsigned – zero in  most significant  (MS) bit 00101 = 5 Negative integers sign-magnitude – set MS bit to show negative 10101 = -5 one’s complement – flip every bit to represent negative 11010 = -5 MS bit indicates sign: 0=positive, 1=negative Data Types
Sign-Magnitude Integers Representations 01111 binary   => 15 decimal 11111 => -15 00000 => 0 10000 => -0 Problems Difficult addition/subtraction check signs convert to positive use adder or subtractor as required The left-bit encodes the sign: 0  =  + 1  =   Data Types
1’s Complement Integers Representations 00110 binary => 6 decimal 11001 => -6 00000 => 0 11111 => -0 Problem Difficult addition/subtraction no need to check signs as before cumbersome logic circuits end-around-carry To negate a number, Invert it, bit-by-bit. The left-bit still encodes the sign: 0  =  + 1  =   Data Types
2’s Complement Problems with sign-magnitude and 1’s complement two representations of zero (+0 and –0) arithmetic circuits are complex How to add two sign-magnitude numbers? e.g., try 2 + (-3) How to add to one’s complement numbers?  e.g., try 4 + (-3) Two’s complement representation developed to make circuits easy for arithmetic. Data Types
2’s Complement  (continued…) Simplifies logic circuit construction because addition and subtraction are  always  done using the same circuitry. there is no need to check signs and convert. operations are done same way as in decimal right to left with carries and borrows Bottom line: simpler hardware units! Data Types
2’s Complement  (continued…) If number is positive or zero, normal binary representation If number is negative, start with positive number flip every bit (i.e., take the one’s complement) then add one 00101 (5) 01001 (9) 11010 (1’s comp) (1’s comp) + 1 + 1 11011 (-5) (-9) 10110 10111 Data Types
2’s Complement  (continued…) Positional number representation with a twist the most significant (left-most) digit has a  negative  weight n -bits represent numbers in the range   2 n  1  …  2 n  1   1 What are these? 0110 = 2 2  + 2 1  = 6 1110 = -2 3  + 2 2  + 2 1  = -2 0000 0110 1111 1010 0001 1000 0111 1100 1011 1001 Data Types
2’s Complement Shortcut To take the two’s complement of a number: copy bits from right to left until (and including) the first “1” flip remaining bits to the left 011010000 011010000 100101111 (1’s comp) + 1 100110000 100110000 (copy) (flip) Data Types
2’s Complement Negation To negate a number, invert all the bits and add 1 6  1010 7  1001 0  0000 -1  0001 4  1100 -8  1000  (??) Data Types
Quiz 00100110 (unsigned int) + 10001101  (signed magnitude)   + 11111101  (1’s complement)   + 00001101  (2’s complement)   + 10111101  (2’s complement)   (decimal)
Quiz 00100110 (unsigned int) + 10001101 (signed magnitude) (unsigned int) + 11111101 (1’s complement) (signed int) + 00001101 (2’s complement) (2’s complement) + 10111101 (2’s complement) Decimal 00011001 00010111 00100100   -31 11100001  (2’s complement) 11100000  (1’s complement) 10011111  (signed magnitude)   38 + -13   25 +  -2   23 +  13   36 + -67 -31
Continually divide the number by 2 and track the remainders. Decimal to Binary Conversion 1     2 5  + 0     2 4  + 1     2 3  + 0     2 2  + 1     2 1  + 1     2 0  32  +  0  +  8  +  0  +  2  +  1 =  43 43 For negative numbers, do above for positive number and negate result Conversions 2 2 2 2 2 2 5  R 0 0  2  R 1 1  21  R 1 1  10  R 1 1  1  R 0 0  0  R 1 1
Decimal to Binary Conversion 0101 0110 01111011 00100011 11011101 01111101111 Conversions
Sign-Extension in 2’s Complement You can make a number wider by simply replicating its leftmost bit as desired. 0110 = 000000000000000110 =  1111 =  11111111111111111 =  1 =  6 6 -1 -1 -1 What do these represent? Conversions
Word Sizes In the preceding slides, every bit pattern was a different length (15 was represented as 01111). Every real computer has a  base word size our machine (MPS430) is  16-bits Memory fetches are word-by-word even if you only want 8 bits (a byte) Instructions are packed into words Numeric representations are word-sized 15 is represented as  0000000000001111 Conversions
Rules of Binary Addition Rules of Binary Addition 0 + 0 = 0  0 + 1 = 1  1 + 0 = 1  1 + 1 = 0, with carry Two's complement addition follows the same rules as binary addition Two's complement subtraction is the binary addition of the minuend to the 2's complement of the subtrahend adding a negative number is the same as subtracting a positive one Binary Arithmetic 5 + (-3) = 2  0000 0101 =  +5 + 1111 1101 =  -3 ---------  -- 0000 0010  = +2
Adding 2’s Complement Integers Issues Overflow:  the result cannot be represented by the number of bits available c 00110 +00101 01011 b1 00110 -00101 00001 0110 +0101 1011 Hmmm.  6 + 5   -5.   Obviously something went wrong. This is a case of  overflow . You can tell there is a problem - a positive plus a positive  cannot  give a negative. Binary Arithmetic
Overflow Revisited Overflow = the result doesn’t fit in the capacity of the representation ALU’s are designed to detect overflow It’s really quite simple if the  carry in  to the most significant position (MSB) is different from the  carry out  from the most significant position (MSB), then overflow occurred. Generally, overflows represented in CPU status bit Overflow
Logical Operations on Bits A B  AND 0 0  0 0 1  0 1 0  0 1 1  1 A B  OR 0 0  0 0 1  1 1 0  1 1 1  1 A  NOT 0  1 1  0  a = 001100101 b = 110010100 a AND b = ? a  OR b = ? NOT a  = ? A XOR b = ?  a AND b = 000000100 a  = 001100101 b  = 110010100 a OR  b = 111110101 NOT a  = 110011010 A B  XOR 0 0  0 0 1  1 1 0  1 1 1  0 A XOR b = 111110001 Logical Operations
Examples of Logical Operations AND useful for clearing bits AND with zero = 0 AND with one = no change OR useful for setting bits OR with zero = no change OR with one = 1 NOT unary operation -- one argument flips every bit 11000101 AND 00001111 00000101 11000101 OR 00001111 11001111 NOT 11000101 00111010 Logical Operations
Floating Point Numbers Binary  scientific notation 32-bit floating point Exponent is  biased Implied leading 1 in mantissa s exponent mantissa 1 8 23 Floating Point
Floating Point Numbers Why the leading implied 1? Always  normalize  after an operation shift mantissa until leading digit is a 1 can assume it is always there, so don’t store it Why the biased exponent? To avoid signed exponent representations s exponent mantissa 1 8 23 Floating Point
Floating Point Numbers What does this represent? Mantissa is to be interpreted as  1 .1 This is 2 0  + 2 -1   = 1 + 1/2 = 1.5 The final number is 1.5 x 2 1  = 3 0 10000000 10000000000000000000000 Floating Point Positive number Exponent is 128 which means the  real exponent is 1
Floating Point Numbers What does this represent? The final number is -1.65625 x 2 2  = -6.625 1 10000001 10101000000000000000000 Floating Point Negative number Exponent is 129 which means the  real exponent is 2 Mantissa is to be interpreted as  1 .10101 This is 2 0  + 2 -1  + 2 -3  + 2 -5  = 1 + 1/2 + 1/8 + 1/32 = 1.65625
Hexadecimal Notation Binary is hard to read and write by hand Hexadecimal is a common alternative 16 digits are 0123456789ABCDEF 0100 0111 1000 1111 = 0x478F 1101 1110 1010 1101 = 0xDEAD 1011 1110 1110 1111 = 0xBEEF 1010 0101 1010 0101 = 0xA5A5 Binary  Hex 0000  0 0001  1 0010  2 0011  3 0100  4 0101  5 0110  6 0111  7 1000  8 1001  9 1010  A 1011  B 1100  C 1101  D 1110  E 1111  F Separate binary code into groups of 4 bits (starting from the right) Translate each group into a single hex digit Hexadecimal 0x is a common prefix for writing numbers which means hexadecimal
Binary to Hex Conversion Every four bits is a hex digit. start grouping from right-hand side 011101010001111010011010111 7 D 4 F 8 A 3 This is not a new machine representation, just a convenient way to write the number. Hexadecimal
Decimal to Hex Conversion Positive  numbers start with empty result next digit to prepend is number  modulo  16 divide number by 16, throw away fractional part if new number is non-zero, go back to    else you are done Negative numbers do above for positive version of number and negate result. Hexadecimal
Decimal to Hex Examples 12 decimal   = 1100  = 0xc 21 decimal   = 0001 0101  = 0x15 55 decimal   = 0011 0111  = 0x37 256 decimal  = 0001 0000 0000 = 0x100 47 decimal   = 0010 1111  = 0x2f 3 decimal   = 0011  = 0x3 127 decimal   = 0111 1111  = 0x7f 1029 decimal  = 0100 0000 0101 = 0x405 Hexadecimal
ASCII Codes How do you represent characters?  ‘A’ ASCII is a set of standard 8-bit, unsigned integers (codes) ' ' = 32, '0' = 48, '1' = 49, 'A' = 65, 'B' = 66 Zero-extended  to word size To convert an integer digit to ASCII character, add 48 (=‘0’) 1 + 48 = 49 => ‘1’ ASCII Characters
ASCII Characters 0 1 2 3 4 5 6 7 8 9 a b c d e f 0  1  2  3  4  5  6  7  8-9  a-f More controls More symbols ASCII Characters DEL o _ O ? / US SI ~ n ^ N > . RS SO } m ] M = - GS CR | l \ L < , FS FF { k [ K ; + ESC VT z j Z J : * SUB LF y i Y I 9 ) EM HT x h X H 8 ( CAN BS w g W G 7 ‘ ETB BEL v f V F 6 & SYN ACK u e U E 5 % NAK ENQ t d T D 4 $ DC4 EOT s c S C 3 # DC3 ETX r b R B 2 “ DC2 STX q a Q A 1 ! DC1 SOH p ` P @ 0 SP DLE NUL
Properties of ASCII Code What is relationship between a decimal digit ('0', '1', …) and its ASCII code? What is the difference between an upper-case letter ('A', 'B', …) and its lower-case equivalent ('a', 'b', …)? Given two ASCII characters, how do we tell which comes first in alphabetical order? What is significant about the first 32 ASCII codes? Are 128 characters enough?  (http://www.unicode.org/) ASCII Characters
Displaying Characters ASCII Characters 48 Decimal 58 Decimal 116 Decimal 53 Decimal
MSP430 Data Types Words and bytes are supported directly by the Instruction Set Architecture. add.b add.w 8-bit and 16-bit 2’s complement signed integers Other data types are supported by  interpreting  variable length values as logical, text, fixed-point, etc., in the software that we write. Data Types
Review: Representation Everything is stored in memory as one’s and zero’s integers, floating point numbers, characters program code Data Type = Representation + Operations You can’t tell what is what just by looking at the binary representation memory could have multiple meanings it is possible to  execute  your Word document Review
Review: Numbers… 7 6 5 4 3 2 1 0 -1 -2 -3 -4 111 110 101 100 011 010 001 000 011 010 001 000, 100 101 110 111 011 010 001 000, 111 110 101 100 011 010 001 000 111 110 101 100 Un-signed Signed  Magnitude 1’s  Complement 2’s  Complement Range: 0 to 7 -3 to 3 -3 to 3 -4 to 3 Review
 
ASCII Characters ASCII Characters

More Related Content

PPTX
Representation of Signed Numbers - R.D.Sivakumar
PPTX
Chapter 2.1 introduction to number system
PPT
Representation of Real Numbers
PPT
Computer Systems Data Representation
PPTX
Data representation
PPTX
data representation
PPTX
Data Representation
PDF
Binary reference guide csit vn1202
Representation of Signed Numbers - R.D.Sivakumar
Chapter 2.1 introduction to number system
Representation of Real Numbers
Computer Systems Data Representation
Data representation
data representation
Data Representation
Binary reference guide csit vn1202

What's hot (20)

PPTX
Data Representation
PPT
Data representation
PPTX
Representation of Integers
PPT
[1] Data Representation
PDF
Data representation in computers
PPT
Binary number systems multiplication
PPT
Binary operations
PPT
Number systems r002
PPT
IS 139 Lecture 4
PPTX
Binary number ppt
PPT
Lec 02 data representation part 1
PPTX
Number systems and conversions
PPTX
Codes r005
PPT
Top schools in delhi ncr
PPTX
Number System
PDF
Binary addition
PPTX
Data representation
PDF
Number system
Data Representation
Data representation
Representation of Integers
[1] Data Representation
Data representation in computers
Binary number systems multiplication
Binary operations
Number systems r002
IS 139 Lecture 4
Binary number ppt
Lec 02 data representation part 1
Number systems and conversions
Codes r005
Top schools in delhi ncr
Number System
Binary addition
Data representation
Number system
Ad

Viewers also liked (14)

PDF
HKG15-405: Redundant zero/sign-extension elimination in GCC
PPT
05 multiply divide
PDF
Research ethics
PPTX
Importance of research
PPT
Importance of Research
PPTX
Introduction to Research Ethics
PPTX
Ethics in research
PPTX
Meaning and characteristics of research
PPTX
Review of Related Literature
PPTX
Sampling Methods in Qualitative and Quantitative Research
PPTX
Literature Review (Review of Related Literature - Research Methodology)
PPTX
Ethics in research ppt by jiya
PDF
Research questions and research objectives
PPTX
Research process
HKG15-405: Redundant zero/sign-extension elimination in GCC
05 multiply divide
Research ethics
Importance of research
Importance of Research
Introduction to Research Ethics
Ethics in research
Meaning and characteristics of research
Review of Related Literature
Sampling Methods in Qualitative and Quantitative Research
Literature Review (Review of Related Literature - Research Methodology)
Ethics in research ppt by jiya
Research questions and research objectives
Research process
Ad

Similar to Chapter 02 Data Types (20)

PPT
Number Systems.ppt
PPT
digitalelectronics.ppt
PPT
Number_System and Boolean Algebra in Digital System Design
PDF
Finite word length effects
PDF
Digital and Logic Design Chapter 1 binary_systems
PPTX
Digital Logic Design.pptx
PPT
intro_Bit_Data_Type_and_opreationon_in computer.ppt
PPT
Basic Digital_Systems_and_Binary_Numbers Sample.ppt
PPT
Chapter 1 Digital Systems and Binary Numbers.ppt
PPT
Logic Design 2009
PPTX
Lecture 7 Data Representation (1).pptx for computer organization and architec...
PDF
NumberSystemsAnnotated.pdf
PPT
Module 4
PPTX
Eln PPT module on electrical and electronics
PPTX
Chapter_Two.pptx data representation in computer
PPTX
chapter1digitalsystemsandbinarynumbers-151021072016-lva1-app6891.pptx
PPT
Number system and codes
PDF
Digital Electronics – Unit I.pdf
PPT
Number Systems.ppt
PPTX
Unit-1 Digital Design and Binary Numbers:
Number Systems.ppt
digitalelectronics.ppt
Number_System and Boolean Algebra in Digital System Design
Finite word length effects
Digital and Logic Design Chapter 1 binary_systems
Digital Logic Design.pptx
intro_Bit_Data_Type_and_opreationon_in computer.ppt
Basic Digital_Systems_and_Binary_Numbers Sample.ppt
Chapter 1 Digital Systems and Binary Numbers.ppt
Logic Design 2009
Lecture 7 Data Representation (1).pptx for computer organization and architec...
NumberSystemsAnnotated.pdf
Module 4
Eln PPT module on electrical and electronics
Chapter_Two.pptx data representation in computer
chapter1digitalsystemsandbinarynumbers-151021072016-lva1-app6891.pptx
Number system and codes
Digital Electronics – Unit I.pdf
Number Systems.ppt
Unit-1 Digital Design and Binary Numbers:

Chapter 02 Data Types

  • 1. Chapter 2 - Data Types
  • 2. Today… Unit 1 slides and homework in JFSB B115 Problems with CCE on Talmage Window’s machines… Drivers have to be installed with administrator privileges Ready shortly… Help Sessions begin Monday @4:00 pm Reading Assignments on-line under the Schedule Tab Concerns or problems??
  • 3. Concepts to Learn… Binary Digital System Data Types Conversions Binary Arithmetic Overflow Logical Operations Floating Point Hexadecimal Numbers ASCII Characters
  • 4. What are Decimal Numbers? “Decimal” means that we have ten digits to use in our representation the symbols 0 through 9 What is 3,546? 3 thousands + 5 hundreds + 4 tens + 6 ones . 3,546 10 = 3  10 3 + 5  10 2 + 4  10 1 + 6  10 0 How about negative numbers? Use two more symbols to distinguish positive and negative, namely, + and -. Digital Binary System
  • 5. What are Binary Numbers? “Binary” means that we have two digits to use in our representation the symbols 0 and 1 What is 1011? 1 eights + 0 fours + 1 twos + 1 ones 1011 2 = 1  2 3 + 0  2 2 + 1  2 1 + 1  2 0 How about negative numbers? We don’t want to add additional symbols So… Digital Binary System
  • 6. Binary Digital System Binary (base 2) because there are two states, 0 and 1. Digital because there are a finite number of symbols. Basic unit of information is the binary digit , or bit . Bit values are represented by various physical means. Voltages Residual magnetism Light Electromagnetic Radiation Polarization Values with more than two states require multiple bits. A collection of 2 bits has 4 possible states: 00, 01, 10, 11 A collection of 3 bits has 8 possible states: 000, 001, 010, 011, 100, 101, 110, 111 A collection of n bits has 2 n possible states. Digital Binary System
  • 7. Electronic Representation of a Bit Relies only on approximate physical values. A logical ‘1’ is a relatively high voltage (2.4V - 5V). A logical ‘0’ is a relatively low voltage (0V - 1V). Analog processing relies on exact values which are affected by temperature, age, etc. Analog values are never quite the same. Each time you play a vinyl album, it will sound a bit different. CDs sound the same no matter how many times you play them. Digital Binary System
  • 8. The Power of the Bit… Bits rely on approximate physical values that are not affected by age, temperature, etc. Music that never degrades. Pictures that never get dusty or scratched. By using groups of bits, we can achieve high precision. 8 bits => each bit pattern represents 1/256. 16 bits => each bit pattern represents 1/65,536 32 bits => each bit pattern represents 1/4,294,967,296 64 bits => each bit pattern represents 1/18,446,744,073,709,550,000 Disadvantage: bits only represent discrete values Digital = Discrete Digital Binary System
  • 9. Binary Nomenclature Binary Digit: 0 or 1 Bit (short for binary digit): A single binary digit LSB (least significant bit): The rightmost bit MSB (most significant bit): The leftmost bit Data sizes 1 Nibble (or nybble) = 4 bits 1 Byte = 2 nibbles = 8 bits 1 Kilobyte (KB) = 1024 bytes 1 Megabyte (MB) = 1024 kilobytes = 1,048,576 bytes 1 Gigabyte (GB) = 1024 megabytes = 1,073,741,824 bytes Digital Binary System
  • 10. What Kinds of Data? All kinds… Numbers – signed, unsigned, integers, floating point, complex, rational, irrational, … Text – characters, strings, … Images – pixels, colors, shapes, … Sound – pitch, amplitude, … Logical – true / false, open / closed, on / off, … Instructions – programs, … … Data type: representation and operations within the computer We’ll start with numbers… Data Types
  • 11. Some Important Data Types Unsigned integers only non-negative numbers 0, 1, 2, 3, 4, … Signed integers negative, zero, positive numbers … , -3, -2, -1, 0, 1, 2, 3, … Floating point numbers numbers with decimal point PI = 3.14159 x 10 0 Characters 8-bit, unsigned integers ‘ 0’, ‘1’, ‘2’, … , ‘a’, ‘b’, ‘c’, … , ‘A’, ‘B’, ‘C’, … , ‘@’, ‘#’, Data Types
  • 12. Unsigned Integers What do these unsigned binary numbers represent? 3x100 + 2x10 + 9x1 = 329 1x4 + 0x2 + 1x1 = 5 0000 0110 1111 1010 0001 1000 0111 1100 1011 1001 Weighted positional notation “ 3” is worth 300, because of its position, while “9” is only worth 9 Data Types 329 10 2 10 1 10 0 101 2 2 2 1 2 0 most significant least significant
  • 13. Unsigned Integers (continued…) Data Types 7 1 1 1 6 0 1 1 5 1 0 1 4 0 0 1 3 1 1 0 2 0 1 0 1 1 0 0 0 0 0 0 2 0 2 1 2 2
  • 14. Unsigned Binary Arithmetic Base 2 addition – just like base 10! add from right to left, propagating carry 10010 10010 1111 + 1001 + 1011 + 1 11011 11101 10000 10111 + 111 Subtraction, multiplication, division,… 0 1 1 1 1 Data Types carry
  • 15. Signed Integers With n bits, we have 2 n distinct values. assign about half to positive integers (1 through 2 n-1 ) and about half to negative (- 2 n-1 through -1) that leaves two values: one for 0, and one extra Positive integers just like unsigned – zero in most significant (MS) bit 00101 = 5 Negative integers sign-magnitude – set MS bit to show negative 10101 = -5 one’s complement – flip every bit to represent negative 11010 = -5 MS bit indicates sign: 0=positive, 1=negative Data Types
  • 16. Sign-Magnitude Integers Representations 01111 binary => 15 decimal 11111 => -15 00000 => 0 10000 => -0 Problems Difficult addition/subtraction check signs convert to positive use adder or subtractor as required The left-bit encodes the sign: 0 = + 1 =  Data Types
  • 17. 1’s Complement Integers Representations 00110 binary => 6 decimal 11001 => -6 00000 => 0 11111 => -0 Problem Difficult addition/subtraction no need to check signs as before cumbersome logic circuits end-around-carry To negate a number, Invert it, bit-by-bit. The left-bit still encodes the sign: 0 = + 1 =  Data Types
  • 18. 2’s Complement Problems with sign-magnitude and 1’s complement two representations of zero (+0 and –0) arithmetic circuits are complex How to add two sign-magnitude numbers? e.g., try 2 + (-3) How to add to one’s complement numbers? e.g., try 4 + (-3) Two’s complement representation developed to make circuits easy for arithmetic. Data Types
  • 19. 2’s Complement (continued…) Simplifies logic circuit construction because addition and subtraction are always done using the same circuitry. there is no need to check signs and convert. operations are done same way as in decimal right to left with carries and borrows Bottom line: simpler hardware units! Data Types
  • 20. 2’s Complement (continued…) If number is positive or zero, normal binary representation If number is negative, start with positive number flip every bit (i.e., take the one’s complement) then add one 00101 (5) 01001 (9) 11010 (1’s comp) (1’s comp) + 1 + 1 11011 (-5) (-9) 10110 10111 Data Types
  • 21. 2’s Complement (continued…) Positional number representation with a twist the most significant (left-most) digit has a negative weight n -bits represent numbers in the range  2 n  1 … 2 n  1  1 What are these? 0110 = 2 2 + 2 1 = 6 1110 = -2 3 + 2 2 + 2 1 = -2 0000 0110 1111 1010 0001 1000 0111 1100 1011 1001 Data Types
  • 22. 2’s Complement Shortcut To take the two’s complement of a number: copy bits from right to left until (and including) the first “1” flip remaining bits to the left 011010000 011010000 100101111 (1’s comp) + 1 100110000 100110000 (copy) (flip) Data Types
  • 23. 2’s Complement Negation To negate a number, invert all the bits and add 1 6 1010 7 1001 0 0000 -1 0001 4 1100 -8 1000 (??) Data Types
  • 24. Quiz 00100110 (unsigned int) + 10001101 (signed magnitude) + 11111101 (1’s complement) + 00001101 (2’s complement) + 10111101 (2’s complement) (decimal)
  • 25. Quiz 00100110 (unsigned int) + 10001101 (signed magnitude) (unsigned int) + 11111101 (1’s complement) (signed int) + 00001101 (2’s complement) (2’s complement) + 10111101 (2’s complement) Decimal 00011001 00010111 00100100 -31 11100001 (2’s complement) 11100000 (1’s complement) 10011111 (signed magnitude) 38 + -13 25 + -2 23 + 13 36 + -67 -31
  • 26. Continually divide the number by 2 and track the remainders. Decimal to Binary Conversion 1  2 5 + 0  2 4 + 1  2 3 + 0  2 2 + 1  2 1 + 1  2 0 32 + 0 + 8 + 0 + 2 + 1 = 43 43 For negative numbers, do above for positive number and negate result Conversions 2 2 2 2 2 2 5 R 0 0 2 R 1 1 21 R 1 1 10 R 1 1 1 R 0 0 0 R 1 1
  • 27. Decimal to Binary Conversion 0101 0110 01111011 00100011 11011101 01111101111 Conversions
  • 28. Sign-Extension in 2’s Complement You can make a number wider by simply replicating its leftmost bit as desired. 0110 = 000000000000000110 = 1111 = 11111111111111111 = 1 = 6 6 -1 -1 -1 What do these represent? Conversions
  • 29. Word Sizes In the preceding slides, every bit pattern was a different length (15 was represented as 01111). Every real computer has a base word size our machine (MPS430) is 16-bits Memory fetches are word-by-word even if you only want 8 bits (a byte) Instructions are packed into words Numeric representations are word-sized 15 is represented as 0000000000001111 Conversions
  • 30. Rules of Binary Addition Rules of Binary Addition 0 + 0 = 0 0 + 1 = 1 1 + 0 = 1 1 + 1 = 0, with carry Two's complement addition follows the same rules as binary addition Two's complement subtraction is the binary addition of the minuend to the 2's complement of the subtrahend adding a negative number is the same as subtracting a positive one Binary Arithmetic 5 + (-3) = 2 0000 0101 = +5 + 1111 1101 = -3 --------- -- 0000 0010 = +2
  • 31. Adding 2’s Complement Integers Issues Overflow: the result cannot be represented by the number of bits available c 00110 +00101 01011 b1 00110 -00101 00001 0110 +0101 1011 Hmmm. 6 + 5  -5. Obviously something went wrong. This is a case of overflow . You can tell there is a problem - a positive plus a positive cannot give a negative. Binary Arithmetic
  • 32. Overflow Revisited Overflow = the result doesn’t fit in the capacity of the representation ALU’s are designed to detect overflow It’s really quite simple if the carry in to the most significant position (MSB) is different from the carry out from the most significant position (MSB), then overflow occurred. Generally, overflows represented in CPU status bit Overflow
  • 33. Logical Operations on Bits A B AND 0 0 0 0 1 0 1 0 0 1 1 1 A B OR 0 0 0 0 1 1 1 0 1 1 1 1 A NOT 0 1 1 0 a = 001100101 b = 110010100 a AND b = ? a OR b = ? NOT a = ? A XOR b = ? a AND b = 000000100 a = 001100101 b = 110010100 a OR b = 111110101 NOT a = 110011010 A B XOR 0 0 0 0 1 1 1 0 1 1 1 0 A XOR b = 111110001 Logical Operations
  • 34. Examples of Logical Operations AND useful for clearing bits AND with zero = 0 AND with one = no change OR useful for setting bits OR with zero = no change OR with one = 1 NOT unary operation -- one argument flips every bit 11000101 AND 00001111 00000101 11000101 OR 00001111 11001111 NOT 11000101 00111010 Logical Operations
  • 35. Floating Point Numbers Binary scientific notation 32-bit floating point Exponent is biased Implied leading 1 in mantissa s exponent mantissa 1 8 23 Floating Point
  • 36. Floating Point Numbers Why the leading implied 1? Always normalize after an operation shift mantissa until leading digit is a 1 can assume it is always there, so don’t store it Why the biased exponent? To avoid signed exponent representations s exponent mantissa 1 8 23 Floating Point
  • 37. Floating Point Numbers What does this represent? Mantissa is to be interpreted as 1 .1 This is 2 0 + 2 -1 = 1 + 1/2 = 1.5 The final number is 1.5 x 2 1 = 3 0 10000000 10000000000000000000000 Floating Point Positive number Exponent is 128 which means the real exponent is 1
  • 38. Floating Point Numbers What does this represent? The final number is -1.65625 x 2 2 = -6.625 1 10000001 10101000000000000000000 Floating Point Negative number Exponent is 129 which means the real exponent is 2 Mantissa is to be interpreted as 1 .10101 This is 2 0 + 2 -1 + 2 -3 + 2 -5 = 1 + 1/2 + 1/8 + 1/32 = 1.65625
  • 39. Hexadecimal Notation Binary is hard to read and write by hand Hexadecimal is a common alternative 16 digits are 0123456789ABCDEF 0100 0111 1000 1111 = 0x478F 1101 1110 1010 1101 = 0xDEAD 1011 1110 1110 1111 = 0xBEEF 1010 0101 1010 0101 = 0xA5A5 Binary Hex 0000 0 0001 1 0010 2 0011 3 0100 4 0101 5 0110 6 0111 7 1000 8 1001 9 1010 A 1011 B 1100 C 1101 D 1110 E 1111 F Separate binary code into groups of 4 bits (starting from the right) Translate each group into a single hex digit Hexadecimal 0x is a common prefix for writing numbers which means hexadecimal
  • 40. Binary to Hex Conversion Every four bits is a hex digit. start grouping from right-hand side 011101010001111010011010111 7 D 4 F 8 A 3 This is not a new machine representation, just a convenient way to write the number. Hexadecimal
  • 41. Decimal to Hex Conversion Positive numbers start with empty result next digit to prepend is number modulo 16 divide number by 16, throw away fractional part if new number is non-zero, go back to  else you are done Negative numbers do above for positive version of number and negate result. Hexadecimal
  • 42. Decimal to Hex Examples 12 decimal = 1100 = 0xc 21 decimal = 0001 0101 = 0x15 55 decimal = 0011 0111 = 0x37 256 decimal = 0001 0000 0000 = 0x100 47 decimal = 0010 1111 = 0x2f 3 decimal = 0011 = 0x3 127 decimal = 0111 1111 = 0x7f 1029 decimal = 0100 0000 0101 = 0x405 Hexadecimal
  • 43. ASCII Codes How do you represent characters? ‘A’ ASCII is a set of standard 8-bit, unsigned integers (codes) ' ' = 32, '0' = 48, '1' = 49, 'A' = 65, 'B' = 66 Zero-extended to word size To convert an integer digit to ASCII character, add 48 (=‘0’) 1 + 48 = 49 => ‘1’ ASCII Characters
  • 44. ASCII Characters 0 1 2 3 4 5 6 7 8 9 a b c d e f 0 1 2 3 4 5 6 7 8-9 a-f More controls More symbols ASCII Characters DEL o _ O ? / US SI ~ n ^ N > . RS SO } m ] M = - GS CR | l \ L < , FS FF { k [ K ; + ESC VT z j Z J : * SUB LF y i Y I 9 ) EM HT x h X H 8 ( CAN BS w g W G 7 ‘ ETB BEL v f V F 6 & SYN ACK u e U E 5 % NAK ENQ t d T D 4 $ DC4 EOT s c S C 3 # DC3 ETX r b R B 2 “ DC2 STX q a Q A 1 ! DC1 SOH p ` P @ 0 SP DLE NUL
  • 45. Properties of ASCII Code What is relationship between a decimal digit ('0', '1', …) and its ASCII code? What is the difference between an upper-case letter ('A', 'B', …) and its lower-case equivalent ('a', 'b', …)? Given two ASCII characters, how do we tell which comes first in alphabetical order? What is significant about the first 32 ASCII codes? Are 128 characters enough? (http://www.unicode.org/) ASCII Characters
  • 46. Displaying Characters ASCII Characters 48 Decimal 58 Decimal 116 Decimal 53 Decimal
  • 47. MSP430 Data Types Words and bytes are supported directly by the Instruction Set Architecture. add.b add.w 8-bit and 16-bit 2’s complement signed integers Other data types are supported by interpreting variable length values as logical, text, fixed-point, etc., in the software that we write. Data Types
  • 48. Review: Representation Everything is stored in memory as one’s and zero’s integers, floating point numbers, characters program code Data Type = Representation + Operations You can’t tell what is what just by looking at the binary representation memory could have multiple meanings it is possible to execute your Word document Review
  • 49. Review: Numbers… 7 6 5 4 3 2 1 0 -1 -2 -3 -4 111 110 101 100 011 010 001 000 011 010 001 000, 100 101 110 111 011 010 001 000, 111 110 101 100 011 010 001 000 111 110 101 100 Un-signed Signed Magnitude 1’s Complement 2’s Complement Range: 0 to 7 -3 to 3 -3 to 3 -4 to 3 Review
  • 50.