SlideShare a Scribd company logo
Data Representation

                  COAL
Computer Organization and Assembly Language
Outline
 Introduction
 Numbering Systems
 Binary & Hexadecimal Numbers
 Base Conversions
 Integer Storage Sizes
 Binary and Hexadecimal Addition
 Signed Integers and 2's Complement Notation
 Binary and Hexadecimal subtraction
 Carry and Overflow
Introduction
 Computers only deal with binary data (0s and 1s), hence all data
  manipulated by computers must be represented in binary format.
 Machine instructions manipulate many different forms of data:
    Numbers:
        Integers: 33, +128, -2827
        Real numbers: 1.33, +9.55609, -6.76E12, +4.33E-03
    Alphanumeric characters (letters, numbers, signs, control characters):
     examples: A, a, c, 1 ,3, ", +, Ctrl, Shift, etc.
    Images (still or moving): Usually represented by numbers representing
     the Red, Green and Blue (RGB) colors of each pixel in an image,
    Sounds: Numbers representing sound amplitudes sampled at a certain
     rate (usually 20kHz).
 So in general we have two major data types that need to be
  represented in computers; numbers and characters.
Numbering Systems
 Numbering systems are characterized by their base
  number.
 In general a numbering system with a base r will have r
  different digits (including the 0) in its number set. These
  digits will range from 0 to r-1
 The most widely used numbering systems are listed in
  the table below:
Binary Numbers
 Each digit (bit) is either 1 or 0
 Each bit represents a power of 2
 Every binary number is a sum of powers of 2
Converting Binary to Decimal
 Weighted positional notation shows how to calculate
the decimal value of each binary bit:
Decimal = (dn-1 × 2n-1) + (dn-2 × 2n-2) + ... + (d1 × 21) + (d0 × 20)
d = binary digit
 binary 10101001 = decimal 169:
(1 × 27) + (1 × 25) + (1 × 23) + (1 × 20) = 128+32+8+1=169
Convert Unsigned Decimal to Binary
 Repeatedly divide the decimal integer by 2. Each
  remainder is a binary digit in the translated value:


                                                   least significant bit




                                                   most significant bit


                                  stop when
          37 = 100101           quotient is zero
Another Procedure for Converting from
           Decimal to Binary
 Start with a binary representation of all 0’s
 Determine the highest possible power of two that is less
  or equal to the number.
 Put a 1 in the bit position corresponding to the highest
  power of two found above.
 Subtract the highest power of two found above from the
  number.
 Repeat the process for the remaining number
Another Procedure for Converting from
           Decimal to Binary
 Example: Converting 76d to Binary
    The highest power of 2 less or equal to 76 is 64, hence the
     seventh (MSB) bit is 1
    Subtracting 64 from 76 we get 12.
    The highest power of 2 less or equal to 12 is 8, hence the fourth
     bit position is 1
    We subtract 8 from 12 and get 4.
    The highest power of 2 less or equal to 4 is 4, hence the third bit
     position is 1
    Subtracting 4 from 4 yield a zero, hence all the left bits are set
     to 0 to yield the final answer
Converting from Decimal fractions to
               Binary
 Using the multiplication
  method to convert the
  decimal 0.8125 to binary,
  we multiply by the radix 2.
    The first product carries
     into the units place.




                                 10
Converting from Decimal fractions to
               Binary
 Converting 0.8125 to binary . . .
    Ignoring the value in the
     units place at each step,
     continue multiplying each
     fractional part by the radix.




                                      11
Converting from Decimal fractions to
               Binary
 Converting 0.8125 to binary . . .
    You are finished when the
     product is zero, or until you
     have reached the desired
     number of binary places.
    Our result, reading from top
     to bottom is:
        0.812510 = 0.11012
    This method also works
     with any base. Just use the
     target radix as the
     multiplier.                      12
Hexadecimal Integers
 Binary values are represented in hexadecimal.
Converting Binary to Hexadecimal
 Each hexadecimal digit corresponds to 4 binary bits.
 Example: Translate the binary integer
  000101101010011110010100 to hexadecimal




                                 M1023.swf
Converting Hexadecimal to Binary
 Each Hexadecimal digit can be replaced by its 4-bit
  binary number to form the binary equivalent.




                                              M1021.swf
Converting Hexadecimal to Decimal
 Multiply each digit by its corresponding power of 16:
  Decimal = (d3 × 163) + (d2 × 162) + (d1 × 161) + (d0 × 160)
  d = hexadecimal digit
 Examples:
    Hex 1234 = (1 × 163) + (2 × 162) + (3 × 161) + (4 × 160) =
      Decimal 4,660
    Hex 3BA4 = (3 × 163) + (11 * 162) + (10 × 161) + (4 × 160) =
      Decimal 15,268
Converting Decimal to Hexadecimal
 Repeatedly divide the decimal integer by 16. Each
  remainder is a hex digit in the translated value:



                                                least significant digit


                                                most significant digit


                               stop when
                             quotient is zero


               Decimal 422 = 1A6 hexadecimal
Integer Storage Sizes

         Standard sizes:




What is the largest unsigned integer that may be stored in 20 bits?
Binary Addition
 Start with the least significant bit (rightmost bit)
 Add each pair of bits
 Include the carry in the addition, if present


                                         carry: 1

                            0    0   0     0    0   1   0   0   (4)

                      +     0    0   0     0    0   1   1   1   (7)


                            0    0   0     0    1   0   1   1   (11)
               bit position: 7   6   5     4    3   2   1   0
Hexadecimal Addition
 Divide the sum of two digits by the number base (16).
  The quotient becomes the carry value, and the
  remainder is the sum digit.
                                 1           1
                36       28      28         6A
                42       45      58         4B
                78       6D      80         B5


                                      21 / 16 = 1, remainder 5



  Important skill: Programmers frequently add and subtract the
  addresses of variables and instructions.
Signed Integer Representation

   There are three ways in which signed binary
    numbers may be expressed:
      Signed magnitude,
      One’s complement and
      Two’s complement.
   In an 8-bit word, signed magnitude
    representation places the absolute value of
    the number in the 7 bits to the right of the
    sign bit.

                                           21
Signed Integer Representation

   For example, in 8-bit signed magnitude,
    positive 3 is:  00000011
   Negative 3 is:    10000011
   Computers perform arithmetic operations on
    signed magnitude numbers in much the same
    way as humans carry out pencil and paper
    arithmetic.
      Humans often ignore the signs of the
       operands while performing a calculation,
       applying the appropriate sign after the
       calculation is complete.                22
Signed Integer Representation

   Binary addition is as easy as it gets. You need
    to know only four rules:
       0 + 0 =    0       0 + 1 = 1
       1 + 0 =    1       1 + 1 = 10
   The simplicity of this system makes it possible
    for digital circuits to carry out arithmetic
    operations.
      We will describe these circuits in Chapter 3.
          Let’s see how the addition rules work with
          signed magnitude numbers . . .

                                              23
Signed Integer Representation

   Example:
      Using signed magnitude
       binary arithmetic, find the
       sum of 75 and 46.
   First, convert 75 and 46 to
    binary, and arrange as a sum,
    but separate the (positive)
    sign bits from the magnitude
    bits.



                                     24
Signed Integer Representation

   Example:
      Using signed magnitude
       binary arithmetic, find the
       sum of 75 and 46.
   Just as in decimal arithmetic,
    we find the sum starting with
    the rightmost bit and work left.




                                       25
Signed Integer Representation

   Example:
      Using signed magnitude
       binary arithmetic, find the
       sum of 75 and 46.
   In the second bit, we have a
    carry, so we note it above the
    third bit.




                                     26
Signed Integer Representation

   Example:
      Using signed magnitude
       binary arithmetic, find the
       sum of 75 and 46.
   The third and fourth bits also
    give us carries.




                                     27
Signed Integer Representation

   Example:
      Using signed magnitude
       binary arithmetic, find the
       sum of 75 and 46.
   Once we have worked our way
    through all eight bits, we are
    done.
       In this example, we were careful careful to pick two
       values whose sum would fit into seven bits. If that
       is not the case, we have a problem.

                                               28
Signed Integer Representation

   Example:
      Using signed magnitude
       binary arithmetic, find the
       sum of 107 and 46.
   We see that the carry from the
    seventh bit overflows and is
    discarded, giving us the
    erroneous result: 107 + 46 = 25.




                                       29
Signed Integer Representation

   The signs in signed
    magnitude representation
    work just like the signs in
    pencil and paper arithmetic.
      Example: Using signed
       magnitude binary
       arithmetic, find the sum of -
       46 and - 25.
  • Because the signs are the same, all we do is
    add the numbers and supply the negative sign
    when we are done.

                                         30
Signed Integer Representation

   Mixed sign addition (or
    subtraction) is done the
    same way.
      Example: Using signed
       magnitude binary
       arithmetic, find the sum of
       46 and - 25.
  • The sign of the result gets the sign of the number
    that is larger.
     – Note the “borrows” from the second and sixth bits.


                                             31
Signed Integer Representation

   Signed magnitude representation is easy for
    people to understand, but it requires
    complicated computer hardware.
   Another disadvantage of signed magnitude is
    that it allows two different representations for
    zero: positive zero and negative zero.
   For these reasons (among others) computers
    systems employ complement systems for
    numeric value representation.

                                            32
Signed Integer Representation

   In complement systems, negative values are
    represented by some difference between a
    number and its base.
   In diminished radix complement systems, a
    negative value is given by the difference between
    the absolute value of a number and one less than
    its base.
   In the binary system, this gives us one’s
    complement. It amounts to little more than flipping
    the bits of a binary number.
                                           33
Signed Integer Representation

   For example, in 8-bit one’s complement,
    positive 3 is:  00000011
   Negative 3 is: 11111100
      In one’s complement, as with signed magnitude,
       negative values are indicated by a 1 in the high
       order bit.
   Complement systems are useful because they
    eliminate the need for special circuitry for
    subtraction. The difference of two values is found
    by adding the minuend to the complement of the
    subtrahend.
                                             34
Signed Integer Representation

   With one’s complement
    addition, the carry bit is
    “carried around” and added
    to the sum.
     Example: Using one’s
      complement binary
      arithmetic, find the sum of
      48 and - 19 19 in one’s complement is
        We note that
        00010011, so -19 in   one’s complement is:
              11101100.



                                                 35
Signed Integer Representation

   Although the “end carry around” adds some
    complexity, one’s complement is simpler to
    implement than signed magnitude.
   But it still has the disadvantage of having two
    different representations for zero: positive
    zero and negative zero.
   Two’s complement solves this problem.
   Two’s complement is the radix complement
    of the binary numbering system.

                                           36
Signed Integer Representation

   To express a value in two’s complement:
     If the number is positive, just convert it to binary
      and you’re done.
     If the number is negative, find the one’s
      complement of the number and then add 1.
   Example:
     In 8-bit one’s complement, positive 3 is: 00000011
     Negative 3 in one’s complement is:      11111100
     Adding 1 gives us -3 in two’s complement form:
       11111101.
                                               37
Signed Integer Representation

   With two’s complement arithmetic, all we do is add
    our two binary numbers. Just discard any carries
    emitting from the high order bit.
   – Example: Using one’s
     complement binary
     arithmetic, find the sum of
     48 and - 19.
      We note that 19 in one’s complement is:
      00010011, so -19 in one’s complement is:
            11101100,
      and -19 in two’s complement is:    11101101.

                                               38
Signed Integer Representation

   When we use any finite number of bits to
    represent a number, we always run the risk of
    the result of our calculations becoming too large
    to be stored in the computer.
   While we can’t always prevent overflow, we can
    always detect overflow.
   In complement arithmetic, an overflow condition
    is easy to detect.



                                           39
Signed Integer Representation

   Example:
      Using two’s complement
       binary arithmetic, find the sum
       of 107 and 46.
   We see that the nonzero carry
    from the seventh bit overflows into
    the sign bit, giving us the
    erroneous result: 107 + 46 = -103.
    Rule for detecting two’s complement overflow: When
    the “carry in” and the “carry out” of the sign bit differ,
    overflow has occurred.
                                                40
Two's Complement Representation
 Positive numbers                          8-bit Binary Unsigned   Signed
                                               value      value      value
    Signed value = Unsigned value
                                            00000000        0         0
 Negative numbers                          00000001        1        +1

    Signed value = Unsigned value - 2n     00000010        2        +2
                                               ...         ...       ...
    n = number of bits
                                            01111110      126       +126
 Negative weight for MSB
                                            01111111      127       +127
    Another way to obtain the signed       10000000      128        -128
     value is to assign a negative weight
                                            10000001      129        -127
     to most-significant bit
                                               ...         ...       ...
       1   0    1    1    0   1   0   0
                                            11111110      254         -2
      -128 64   32   16   8   4   2   1
                                            11111111      255         -1
   = -128 + 32 + 16 + 4 = -76
Forming the Two's Complement
starting value                                  00100100 = +36
step1: reverse the bits (1's complement)        11011011
step 2: add 1 to the value from step 1          +        1
sum = 2's complement representation             11011100 = -36

 Sum of an integer and its 2's complement must be zero:
  00100100 + 11011100 = 00000000 (8-bit sum) ⇒ Ignore Carry

          The easiest way to obtain the 2's complement of a
        binary number is by starting at the LSB, leaving all the
       0s unchanged, look for the first occurrence of a 1. Leave
        this 1 unchanged and complement all the bits after it.
Sign Bit
Highest bit indicates the sign. 1 = negative, 0 = positive




  If highest digit of a hexadecimal is > 7, the value is negative
  Examples: 8A and C5 are negative bytes
  A21F and 9D03 are negative words
  B1C42A00 is a negative double-word
Sign Extension
Step 1: Move the number into the lower-significant bits
Step 2: Fill all the remaining higher bits with the sign bit
 This will ensure that both magnitude and sign are correct
 Examples
    Sign-Extend 10110011 to 16 bits
      10110011 = -77                  11111111 10110011 = -77
    Sign-Extend 01100010 to 16 bits
      01100010 = +98                  00000000 01100010 = +98

 Infinite 0s can be added to the left of a positive number
 Infinite 1s can be added to the left of a negative number
   Sign ExtensionRequired when manipulating signed values of variable
   lengths (converting 8-bit signed 2’s comp value to 16-bit)
Two's Complement of a Hexadecimal
 To form the two's complement of a hexadecimal
    Subtract each hexadecimal digit from 15
    Add 1

 Examples:
    2's complement of 6A3D = 95C3
    2's complement of 92F0 = 6D10
    2's complement of FFFF = 0001

 No need to convert hexadecimal to binary
Two's Complement of a Hexadecimal
 Start at the least significant digit, leaving all the 0s
  unchanged, look for the first occurrence of a non-zero
  digit.
 Subtract this digit from 16.
 Then subtract all remaining digits from 15.
 Examples:
    2's complement of 6A3D = 95C3      F F F 16         F F 16
                                     - 6A3 D          - 92 F0
    2's complement of 92F0 = 6D10   --------------   --------------
                                        95C3             6D10
    2's complement of FFFF = 0001
Binary Subtraction
 When subtracting A – B, convert B to its 2's complement
 Add A to (–B)
     00001100                          00001100
 –                                 +
     00000010                          11111110          (2's complement)

     00001010                          00001010          (same result)

 Carry is ignored, because
      Negative number is sign-extended with 1's
      You can imagine infinite 1's to the left of a negative number
      Adding the carry to the extended 1's produces extended zeros

               Practice: Subtract 00100101 from 01101001.
Hexadecimal Subtraction
 When a borrow is required from the digit to the left,
  add 16 (decimal) to the current digit's value

       16 + 5 = 21

         -1                        11
       C675                        C675
   -                           +
       A247                        5DB9   (2's complement)
       242E                        242E   (same result)


 Last Carry is ignored
  Practice: The address of var1 is 00400B20. The address of the
  next variable after var1 is 0040A06C. How many bytes are used
  by var1?
Ranges of Signed Integers
The unsigned range is divided into two signed ranges for positive
and negative numbers




  Practice: What is the range of signed values that may be stored
  in 20 bits?
Carry and Overflow
 Carry is important when …
    Adding or subtracting unsigned integers
    Indicates that the unsigned sum is out of range
    Either < 0 or > maximum unsigned n-bit value
 Overflow is important when …
    Adding or subtracting signed integers
    Indicates that the signed sum is out of range
 Overflow occurs when
    Adding two positive numbers and the sum is negative
    Adding two negative numbers and the sum is positive
    Can happen because of the fixed number of sum bits
Carry and Overflow Examples
 We can have carry without overflow and vice-versa
 Four cases are possible
                    1                               1   1     1   1     1

    0     0   0     0     1   1   1    1    15          0     0   0     0     1   1   1    1     15
+                                                   +
    0     0   0     0     1   0   0    0     8          1     1   1     1     1   0   0    0   245 (-8)

    0     0   0     1     0   1   1    1    23          0     0   0     0     0   1   1    1      7

        Carry = 0       Overflow = 0                        Carry = 1       Overflow = 0

    1                                               1             1     1

    0     1   0     0     1   1   1    1    79          1     1   0     1     1   0   1    0 218 (-38)
+                                                   +
    0     1   0     0     0   0   0    0    64          1     0   0     1     1   1   0    1 157 (-99)

    1     0   0     0     1   1   1    1     143        0     1   1     1     0   1   1    1     119
                                           (-113)
        Carry = 0       Overflow = 1                        Carry = 1       Overflow = 1

More Related Content

PPTX
Computer architecture data representation
Anil Pokhrel
 
PPT
Pseudocode basics
kiran_kaur
 
PPTX
Introduction to Julia Language
Diego Marinho de Oliveira
 
PPTX
Intro to assembly language
United International University
 
PPTX
EE5440 – Computer Architecture - Lecture 1
Dilawar Khan
 
PPTX
Binary computing
samina khan
 
PDF
Chapter 05 computer arithmetic
IIUI
 
PPT
Introduction to c programming
ABHISHEK fulwadhwa
 
Computer architecture data representation
Anil Pokhrel
 
Pseudocode basics
kiran_kaur
 
Introduction to Julia Language
Diego Marinho de Oliveira
 
Intro to assembly language
United International University
 
EE5440 – Computer Architecture - Lecture 1
Dilawar Khan
 
Binary computing
samina khan
 
Chapter 05 computer arithmetic
IIUI
 
Introduction to c programming
ABHISHEK fulwadhwa
 

What's hot (20)

PPTX
Instruction Set Architecture: MIPS
Prasenjit Dey
 
PPTX
Digital logic gates
jsearle11
 
PDF
Microprocessor and Microcontroller Lab Manual!
PRABHAHARAN429
 
PDF
Transformers in 2021
Grigory Sapunov
 
PPTX
Computer architecture and organization
Tushar B Kute
 
PPTX
Direct Memory Access ppt
OECLIB Odisha Electronics Control Library
 
PPTX
Datapath Design of Computer Architecture
Abu Zaman
 
PPTX
Presentation on C programming language
Ashmita Tuition Center
 
PPTX
BERT
Khang Pham
 
PPTX
Programing techniques
Prabhjit Singh
 
PPT
Chapter 2 instructions language of the computer
BATMUNHMUNHZAYA
 
PPT
Introduction to problem solving in c++
Online
 
PDF
Assembly level language
PDFSHARE
 
PPTX
Logic gates ppt
parassini
 
PDF
Computational Thinking
showslidedump
 
PPTX
Programming Languages / Translators
Project Student
 
PPT
Intro to scikit-learn
AWeber
 
PPTX
Introduction to programming
Neeru Mittal
 
PPTX
BERT introduction
Hanwha System / ICT
 
PPTX
MicroProgrammed Explained .
Muhammad Umar
 
Instruction Set Architecture: MIPS
Prasenjit Dey
 
Digital logic gates
jsearle11
 
Microprocessor and Microcontroller Lab Manual!
PRABHAHARAN429
 
Transformers in 2021
Grigory Sapunov
 
Computer architecture and organization
Tushar B Kute
 
Datapath Design of Computer Architecture
Abu Zaman
 
Presentation on C programming language
Ashmita Tuition Center
 
Programing techniques
Prabhjit Singh
 
Chapter 2 instructions language of the computer
BATMUNHMUNHZAYA
 
Introduction to problem solving in c++
Online
 
Assembly level language
PDFSHARE
 
Logic gates ppt
parassini
 
Computational Thinking
showslidedump
 
Programming Languages / Translators
Project Student
 
Intro to scikit-learn
AWeber
 
Introduction to programming
Neeru Mittal
 
BERT introduction
Hanwha System / ICT
 
MicroProgrammed Explained .
Muhammad Umar
 
Ad

Similar to Lec 02 data representation part 1 (20)

PPTX
data representation
Haroon_007
 
PPT
computer architecture organization in Ece
Bayesayohannis
 
PPT
Ch02_4web456489478465165489445156568.ppt
nemoo80nemoo90
 
PPTX
Representation Of Numbers and Characters
Shaikh Kamrul Islam (Konok kamrul)
 
PDF
Digital Electronics – Unit I.pdf
Kannan Kanagaraj
 
PPT
IS 139 Lecture 4 - 2015
Aron Kondoro
 
PPT
IS 139 Lecture 4
wajanga
 
PPT
100_2_digitalSystem_Chap1 (2).ppt
namraashraf56
 
PPT
Lesson 1 basic theory of information
Roma Kimberly Erolin
 
PPT
Lesson 1 basic theory of information
Roma Kimberly Erolin
 
PPTX
UNIT 4 -Data Representation.pptxfghfghhggh
KwadjoOwusuAnsahQuar
 
PPT
digital systems and information
Kamran Zafar
 
PDF
Slide03 Number System and Operations Part 1
อภิเษก หงษ์วิทยากร
 
PPTX
Lecture4 binary-numbers-logic-operations
markme18
 
PPTX
Digital Logic Design.pptx
AminaZahid16
 
PDF
Digital and Logic Design Chapter 1 binary_systems
Imran Waris
 
PPT
Chapter 02 Data Types
Nathan Yeung
 
PPT
1. basic theories of information
'Cedrick SuperGreenilistic Expialidocious Antonino
 
PPTX
Number system
Mantra VLSI
 
data representation
Haroon_007
 
computer architecture organization in Ece
Bayesayohannis
 
Ch02_4web456489478465165489445156568.ppt
nemoo80nemoo90
 
Representation Of Numbers and Characters
Shaikh Kamrul Islam (Konok kamrul)
 
Digital Electronics – Unit I.pdf
Kannan Kanagaraj
 
IS 139 Lecture 4 - 2015
Aron Kondoro
 
IS 139 Lecture 4
wajanga
 
100_2_digitalSystem_Chap1 (2).ppt
namraashraf56
 
Lesson 1 basic theory of information
Roma Kimberly Erolin
 
Lesson 1 basic theory of information
Roma Kimberly Erolin
 
UNIT 4 -Data Representation.pptxfghfghhggh
KwadjoOwusuAnsahQuar
 
digital systems and information
Kamran Zafar
 
Slide03 Number System and Operations Part 1
อภิเษก หงษ์วิทยากร
 
Lecture4 binary-numbers-logic-operations
markme18
 
Digital Logic Design.pptx
AminaZahid16
 
Digital and Logic Design Chapter 1 binary_systems
Imran Waris
 
Chapter 02 Data Types
Nathan Yeung
 
1. basic theories of information
'Cedrick SuperGreenilistic Expialidocious Antonino
 
Number system
Mantra VLSI
 
Ad

More from Abdul Khan (9)

PPT
Lec 04 intro assembly
Abdul Khan
 
PPTX
Algorithm & data structures lec4&5
Abdul Khan
 
PPTX
Algorithm & data structure lec2
Abdul Khan
 
PPTX
Algorithm & data structures lec1
Abdul Khan
 
PPT
Lec 03 ia32 architecture
Abdul Khan
 
PPT
Lec 02 data representation part 2
Abdul Khan
 
PPT
Lec 01 basic concepts
Abdul Khan
 
PPTX
war on terror
Abdul Khan
 
PPT
Xhtml
Abdul Khan
 
Lec 04 intro assembly
Abdul Khan
 
Algorithm & data structures lec4&5
Abdul Khan
 
Algorithm & data structure lec2
Abdul Khan
 
Algorithm & data structures lec1
Abdul Khan
 
Lec 03 ia32 architecture
Abdul Khan
 
Lec 02 data representation part 2
Abdul Khan
 
Lec 01 basic concepts
Abdul Khan
 
war on terror
Abdul Khan
 
Xhtml
Abdul Khan
 

Recently uploaded (20)

PDF
The Future of Mobile Is Context-Aware—Are You Ready?
iProgrammer Solutions Private Limited
 
PDF
SparkLabs Primer on Artificial Intelligence 2025
SparkLabs Group
 
PPTX
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
PDF
AI-Cloud-Business-Management-Platforms-The-Key-to-Efficiency-Growth.pdf
Artjoker Software Development Company
 
PDF
Structs to JSON: How Go Powers REST APIs
Emily Achieng
 
PDF
Orbitly Pitch Deck|A Mission-Driven Platform for Side Project Collaboration (...
zz41354899
 
PPTX
cloud computing vai.pptx for the project
vaibhavdobariyal79
 
PDF
Accelerating Oracle Database 23ai Troubleshooting with Oracle AHF Fleet Insig...
Sandesh Rao
 
PDF
Brief History of Internet - Early Days of Internet
sutharharshit158
 
PDF
How-Cloud-Computing-Impacts-Businesses-in-2025-and-Beyond.pdf
Artjoker Software Development Company
 
PDF
Responsible AI and AI Ethics - By Sylvester Ebhonu
Sylvester Ebhonu
 
PDF
Trying to figure out MCP by actually building an app from scratch with open s...
Julien SIMON
 
PPTX
AI and Robotics for Human Well-being.pptx
JAYMIN SUTHAR
 
PDF
Using Anchore and DefectDojo to Stand Up Your DevSecOps Function
Anchore
 
PDF
Advances in Ultra High Voltage (UHV) Transmission and Distribution Systems.pdf
Nabajyoti Banik
 
PDF
REPORT: Heating appliances market in Poland 2024
SPIUG
 
PDF
AI Unleashed - Shaping the Future -Starting Today - AIOUG Yatra 2025 - For Co...
Sandesh Rao
 
PDF
A Strategic Analysis of the MVNO Wave in Emerging Markets.pdf
IPLOOK Networks
 
PDF
Get More from Fiori Automation - What’s New, What Works, and What’s Next.pdf
Precisely
 
PDF
Make GenAI investments go further with the Dell AI Factory
Principled Technologies
 
The Future of Mobile Is Context-Aware—Are You Ready?
iProgrammer Solutions Private Limited
 
SparkLabs Primer on Artificial Intelligence 2025
SparkLabs Group
 
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
AI-Cloud-Business-Management-Platforms-The-Key-to-Efficiency-Growth.pdf
Artjoker Software Development Company
 
Structs to JSON: How Go Powers REST APIs
Emily Achieng
 
Orbitly Pitch Deck|A Mission-Driven Platform for Side Project Collaboration (...
zz41354899
 
cloud computing vai.pptx for the project
vaibhavdobariyal79
 
Accelerating Oracle Database 23ai Troubleshooting with Oracle AHF Fleet Insig...
Sandesh Rao
 
Brief History of Internet - Early Days of Internet
sutharharshit158
 
How-Cloud-Computing-Impacts-Businesses-in-2025-and-Beyond.pdf
Artjoker Software Development Company
 
Responsible AI and AI Ethics - By Sylvester Ebhonu
Sylvester Ebhonu
 
Trying to figure out MCP by actually building an app from scratch with open s...
Julien SIMON
 
AI and Robotics for Human Well-being.pptx
JAYMIN SUTHAR
 
Using Anchore and DefectDojo to Stand Up Your DevSecOps Function
Anchore
 
Advances in Ultra High Voltage (UHV) Transmission and Distribution Systems.pdf
Nabajyoti Banik
 
REPORT: Heating appliances market in Poland 2024
SPIUG
 
AI Unleashed - Shaping the Future -Starting Today - AIOUG Yatra 2025 - For Co...
Sandesh Rao
 
A Strategic Analysis of the MVNO Wave in Emerging Markets.pdf
IPLOOK Networks
 
Get More from Fiori Automation - What’s New, What Works, and What’s Next.pdf
Precisely
 
Make GenAI investments go further with the Dell AI Factory
Principled Technologies
 

Lec 02 data representation part 1

  • 1. Data Representation COAL Computer Organization and Assembly Language
  • 2. Outline  Introduction  Numbering Systems  Binary & Hexadecimal Numbers  Base Conversions  Integer Storage Sizes  Binary and Hexadecimal Addition  Signed Integers and 2's Complement Notation  Binary and Hexadecimal subtraction  Carry and Overflow
  • 3. Introduction  Computers only deal with binary data (0s and 1s), hence all data manipulated by computers must be represented in binary format.  Machine instructions manipulate many different forms of data:  Numbers:  Integers: 33, +128, -2827  Real numbers: 1.33, +9.55609, -6.76E12, +4.33E-03  Alphanumeric characters (letters, numbers, signs, control characters): examples: A, a, c, 1 ,3, ", +, Ctrl, Shift, etc.  Images (still or moving): Usually represented by numbers representing the Red, Green and Blue (RGB) colors of each pixel in an image,  Sounds: Numbers representing sound amplitudes sampled at a certain rate (usually 20kHz).  So in general we have two major data types that need to be represented in computers; numbers and characters.
  • 4. Numbering Systems  Numbering systems are characterized by their base number.  In general a numbering system with a base r will have r different digits (including the 0) in its number set. These digits will range from 0 to r-1  The most widely used numbering systems are listed in the table below:
  • 5. Binary Numbers  Each digit (bit) is either 1 or 0  Each bit represents a power of 2  Every binary number is a sum of powers of 2
  • 6. Converting Binary to Decimal  Weighted positional notation shows how to calculate the decimal value of each binary bit: Decimal = (dn-1 × 2n-1) + (dn-2 × 2n-2) + ... + (d1 × 21) + (d0 × 20) d = binary digit  binary 10101001 = decimal 169: (1 × 27) + (1 × 25) + (1 × 23) + (1 × 20) = 128+32+8+1=169
  • 7. Convert Unsigned Decimal to Binary  Repeatedly divide the decimal integer by 2. Each remainder is a binary digit in the translated value: least significant bit most significant bit stop when 37 = 100101 quotient is zero
  • 8. Another Procedure for Converting from Decimal to Binary  Start with a binary representation of all 0’s  Determine the highest possible power of two that is less or equal to the number.  Put a 1 in the bit position corresponding to the highest power of two found above.  Subtract the highest power of two found above from the number.  Repeat the process for the remaining number
  • 9. Another Procedure for Converting from Decimal to Binary  Example: Converting 76d to Binary  The highest power of 2 less or equal to 76 is 64, hence the seventh (MSB) bit is 1  Subtracting 64 from 76 we get 12.  The highest power of 2 less or equal to 12 is 8, hence the fourth bit position is 1  We subtract 8 from 12 and get 4.  The highest power of 2 less or equal to 4 is 4, hence the third bit position is 1  Subtracting 4 from 4 yield a zero, hence all the left bits are set to 0 to yield the final answer
  • 10. Converting from Decimal fractions to Binary  Using the multiplication method to convert the decimal 0.8125 to binary, we multiply by the radix 2.  The first product carries into the units place. 10
  • 11. Converting from Decimal fractions to Binary  Converting 0.8125 to binary . . .  Ignoring the value in the units place at each step, continue multiplying each fractional part by the radix. 11
  • 12. Converting from Decimal fractions to Binary  Converting 0.8125 to binary . . .  You are finished when the product is zero, or until you have reached the desired number of binary places.  Our result, reading from top to bottom is: 0.812510 = 0.11012  This method also works with any base. Just use the target radix as the multiplier. 12
  • 13. Hexadecimal Integers  Binary values are represented in hexadecimal.
  • 14. Converting Binary to Hexadecimal  Each hexadecimal digit corresponds to 4 binary bits.  Example: Translate the binary integer 000101101010011110010100 to hexadecimal M1023.swf
  • 15. Converting Hexadecimal to Binary  Each Hexadecimal digit can be replaced by its 4-bit binary number to form the binary equivalent. M1021.swf
  • 16. Converting Hexadecimal to Decimal  Multiply each digit by its corresponding power of 16: Decimal = (d3 × 163) + (d2 × 162) + (d1 × 161) + (d0 × 160) d = hexadecimal digit  Examples:  Hex 1234 = (1 × 163) + (2 × 162) + (3 × 161) + (4 × 160) = Decimal 4,660  Hex 3BA4 = (3 × 163) + (11 * 162) + (10 × 161) + (4 × 160) = Decimal 15,268
  • 17. Converting Decimal to Hexadecimal  Repeatedly divide the decimal integer by 16. Each remainder is a hex digit in the translated value: least significant digit most significant digit stop when quotient is zero Decimal 422 = 1A6 hexadecimal
  • 18. Integer Storage Sizes Standard sizes: What is the largest unsigned integer that may be stored in 20 bits?
  • 19. Binary Addition  Start with the least significant bit (rightmost bit)  Add each pair of bits  Include the carry in the addition, if present carry: 1 0 0 0 0 0 1 0 0 (4) + 0 0 0 0 0 1 1 1 (7) 0 0 0 0 1 0 1 1 (11) bit position: 7 6 5 4 3 2 1 0
  • 20. Hexadecimal Addition  Divide the sum of two digits by the number base (16). The quotient becomes the carry value, and the remainder is the sum digit. 1 1 36 28 28 6A 42 45 58 4B 78 6D 80 B5 21 / 16 = 1, remainder 5 Important skill: Programmers frequently add and subtract the addresses of variables and instructions.
  • 21. Signed Integer Representation  There are three ways in which signed binary numbers may be expressed:  Signed magnitude,  One’s complement and  Two’s complement.  In an 8-bit word, signed magnitude representation places the absolute value of the number in the 7 bits to the right of the sign bit. 21
  • 22. Signed Integer Representation  For example, in 8-bit signed magnitude, positive 3 is: 00000011  Negative 3 is: 10000011  Computers perform arithmetic operations on signed magnitude numbers in much the same way as humans carry out pencil and paper arithmetic.  Humans often ignore the signs of the operands while performing a calculation, applying the appropriate sign after the calculation is complete. 22
  • 23. Signed Integer Representation  Binary addition is as easy as it gets. You need to know only four rules: 0 + 0 = 0 0 + 1 = 1 1 + 0 = 1 1 + 1 = 10  The simplicity of this system makes it possible for digital circuits to carry out arithmetic operations.  We will describe these circuits in Chapter 3. Let’s see how the addition rules work with signed magnitude numbers . . . 23
  • 24. Signed Integer Representation  Example:  Using signed magnitude binary arithmetic, find the sum of 75 and 46.  First, convert 75 and 46 to binary, and arrange as a sum, but separate the (positive) sign bits from the magnitude bits. 24
  • 25. Signed Integer Representation  Example:  Using signed magnitude binary arithmetic, find the sum of 75 and 46.  Just as in decimal arithmetic, we find the sum starting with the rightmost bit and work left. 25
  • 26. Signed Integer Representation  Example:  Using signed magnitude binary arithmetic, find the sum of 75 and 46.  In the second bit, we have a carry, so we note it above the third bit. 26
  • 27. Signed Integer Representation  Example:  Using signed magnitude binary arithmetic, find the sum of 75 and 46.  The third and fourth bits also give us carries. 27
  • 28. Signed Integer Representation  Example:  Using signed magnitude binary arithmetic, find the sum of 75 and 46.  Once we have worked our way through all eight bits, we are done. In this example, we were careful careful to pick two values whose sum would fit into seven bits. If that is not the case, we have a problem. 28
  • 29. Signed Integer Representation  Example:  Using signed magnitude binary arithmetic, find the sum of 107 and 46.  We see that the carry from the seventh bit overflows and is discarded, giving us the erroneous result: 107 + 46 = 25. 29
  • 30. Signed Integer Representation  The signs in signed magnitude representation work just like the signs in pencil and paper arithmetic.  Example: Using signed magnitude binary arithmetic, find the sum of - 46 and - 25. • Because the signs are the same, all we do is add the numbers and supply the negative sign when we are done. 30
  • 31. Signed Integer Representation  Mixed sign addition (or subtraction) is done the same way.  Example: Using signed magnitude binary arithmetic, find the sum of 46 and - 25. • The sign of the result gets the sign of the number that is larger. – Note the “borrows” from the second and sixth bits. 31
  • 32. Signed Integer Representation  Signed magnitude representation is easy for people to understand, but it requires complicated computer hardware.  Another disadvantage of signed magnitude is that it allows two different representations for zero: positive zero and negative zero.  For these reasons (among others) computers systems employ complement systems for numeric value representation. 32
  • 33. Signed Integer Representation  In complement systems, negative values are represented by some difference between a number and its base.  In diminished radix complement systems, a negative value is given by the difference between the absolute value of a number and one less than its base.  In the binary system, this gives us one’s complement. It amounts to little more than flipping the bits of a binary number. 33
  • 34. Signed Integer Representation  For example, in 8-bit one’s complement, positive 3 is: 00000011  Negative 3 is: 11111100  In one’s complement, as with signed magnitude, negative values are indicated by a 1 in the high order bit.  Complement systems are useful because they eliminate the need for special circuitry for subtraction. The difference of two values is found by adding the minuend to the complement of the subtrahend. 34
  • 35. Signed Integer Representation  With one’s complement addition, the carry bit is “carried around” and added to the sum.  Example: Using one’s complement binary arithmetic, find the sum of 48 and - 19 19 in one’s complement is We note that 00010011, so -19 in one’s complement is: 11101100. 35
  • 36. Signed Integer Representation  Although the “end carry around” adds some complexity, one’s complement is simpler to implement than signed magnitude.  But it still has the disadvantage of having two different representations for zero: positive zero and negative zero.  Two’s complement solves this problem.  Two’s complement is the radix complement of the binary numbering system. 36
  • 37. Signed Integer Representation  To express a value in two’s complement:  If the number is positive, just convert it to binary and you’re done.  If the number is negative, find the one’s complement of the number and then add 1.  Example:  In 8-bit one’s complement, positive 3 is: 00000011  Negative 3 in one’s complement is: 11111100  Adding 1 gives us -3 in two’s complement form: 11111101. 37
  • 38. Signed Integer Representation  With two’s complement arithmetic, all we do is add our two binary numbers. Just discard any carries emitting from the high order bit. – Example: Using one’s complement binary arithmetic, find the sum of 48 and - 19. We note that 19 in one’s complement is: 00010011, so -19 in one’s complement is: 11101100, and -19 in two’s complement is: 11101101. 38
  • 39. Signed Integer Representation  When we use any finite number of bits to represent a number, we always run the risk of the result of our calculations becoming too large to be stored in the computer.  While we can’t always prevent overflow, we can always detect overflow.  In complement arithmetic, an overflow condition is easy to detect. 39
  • 40. Signed Integer Representation  Example:  Using two’s complement binary arithmetic, find the sum of 107 and 46.  We see that the nonzero carry from the seventh bit overflows into the sign bit, giving us the erroneous result: 107 + 46 = -103. Rule for detecting two’s complement overflow: When the “carry in” and the “carry out” of the sign bit differ, overflow has occurred. 40
  • 41. Two's Complement Representation  Positive numbers 8-bit Binary Unsigned Signed value value value  Signed value = Unsigned value 00000000 0 0  Negative numbers 00000001 1 +1  Signed value = Unsigned value - 2n 00000010 2 +2 ... ... ...  n = number of bits 01111110 126 +126  Negative weight for MSB 01111111 127 +127  Another way to obtain the signed 10000000 128 -128 value is to assign a negative weight 10000001 129 -127 to most-significant bit ... ... ... 1 0 1 1 0 1 0 0 11111110 254 -2 -128 64 32 16 8 4 2 1 11111111 255 -1 = -128 + 32 + 16 + 4 = -76
  • 42. Forming the Two's Complement starting value 00100100 = +36 step1: reverse the bits (1's complement) 11011011 step 2: add 1 to the value from step 1 + 1 sum = 2's complement representation 11011100 = -36 Sum of an integer and its 2's complement must be zero: 00100100 + 11011100 = 00000000 (8-bit sum) ⇒ Ignore Carry The easiest way to obtain the 2's complement of a binary number is by starting at the LSB, leaving all the 0s unchanged, look for the first occurrence of a 1. Leave this 1 unchanged and complement all the bits after it.
  • 43. Sign Bit Highest bit indicates the sign. 1 = negative, 0 = positive If highest digit of a hexadecimal is > 7, the value is negative Examples: 8A and C5 are negative bytes A21F and 9D03 are negative words B1C42A00 is a negative double-word
  • 44. Sign Extension Step 1: Move the number into the lower-significant bits Step 2: Fill all the remaining higher bits with the sign bit  This will ensure that both magnitude and sign are correct  Examples  Sign-Extend 10110011 to 16 bits 10110011 = -77 11111111 10110011 = -77  Sign-Extend 01100010 to 16 bits 01100010 = +98 00000000 01100010 = +98  Infinite 0s can be added to the left of a positive number  Infinite 1s can be added to the left of a negative number Sign ExtensionRequired when manipulating signed values of variable lengths (converting 8-bit signed 2’s comp value to 16-bit)
  • 45. Two's Complement of a Hexadecimal  To form the two's complement of a hexadecimal  Subtract each hexadecimal digit from 15  Add 1  Examples:  2's complement of 6A3D = 95C3  2's complement of 92F0 = 6D10  2's complement of FFFF = 0001  No need to convert hexadecimal to binary
  • 46. Two's Complement of a Hexadecimal  Start at the least significant digit, leaving all the 0s unchanged, look for the first occurrence of a non-zero digit.  Subtract this digit from 16.  Then subtract all remaining digits from 15.  Examples:  2's complement of 6A3D = 95C3 F F F 16 F F 16 - 6A3 D - 92 F0  2's complement of 92F0 = 6D10 -------------- -------------- 95C3 6D10  2's complement of FFFF = 0001
  • 47. Binary Subtraction  When subtracting A – B, convert B to its 2's complement  Add A to (–B) 00001100 00001100 – + 00000010 11111110 (2's complement) 00001010 00001010 (same result)  Carry is ignored, because  Negative number is sign-extended with 1's  You can imagine infinite 1's to the left of a negative number  Adding the carry to the extended 1's produces extended zeros Practice: Subtract 00100101 from 01101001.
  • 48. Hexadecimal Subtraction  When a borrow is required from the digit to the left, add 16 (decimal) to the current digit's value 16 + 5 = 21 -1 11 C675 C675 - + A247 5DB9 (2's complement) 242E 242E (same result)  Last Carry is ignored Practice: The address of var1 is 00400B20. The address of the next variable after var1 is 0040A06C. How many bytes are used by var1?
  • 49. Ranges of Signed Integers The unsigned range is divided into two signed ranges for positive and negative numbers Practice: What is the range of signed values that may be stored in 20 bits?
  • 50. Carry and Overflow  Carry is important when …  Adding or subtracting unsigned integers  Indicates that the unsigned sum is out of range  Either < 0 or > maximum unsigned n-bit value  Overflow is important when …  Adding or subtracting signed integers  Indicates that the signed sum is out of range  Overflow occurs when  Adding two positive numbers and the sum is negative  Adding two negative numbers and the sum is positive  Can happen because of the fixed number of sum bits
  • 51. Carry and Overflow Examples  We can have carry without overflow and vice-versa  Four cases are possible 1 1 1 1 1 1 0 0 0 0 1 1 1 1 15 0 0 0 0 1 1 1 1 15 + + 0 0 0 0 1 0 0 0 8 1 1 1 1 1 0 0 0 245 (-8) 0 0 0 1 0 1 1 1 23 0 0 0 0 0 1 1 1 7 Carry = 0 Overflow = 0 Carry = 1 Overflow = 0 1 1 1 1 0 1 0 0 1 1 1 1 79 1 1 0 1 1 0 1 0 218 (-38) + + 0 1 0 0 0 0 0 0 64 1 0 0 1 1 1 0 1 157 (-99) 1 0 0 0 1 1 1 1 143 0 1 1 1 0 1 1 1 119 (-113) Carry = 0 Overflow = 1 Carry = 1 Overflow = 1