data representation in computer

Upload: throx

Post on 04-Apr-2018

224 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/31/2019 Data Representation in Computer

    1/22

    Data Representation in

    Computer

    CSC2101 COMPUTERORGANISATION

  • 7/31/2019 Data Representation in Computer

    2/22

    CSC2101 2

    Storing Data in Computer

    Computer hardware can only store and process inbinary.

    Data (text, number, audio, image, video, etc) has to be

    converted to some form of binary representation

    understand by the computer. For example, text isnormally represented in ACSII or Unicode, integer

    numbers are represented in twos complement binary.

    A 01000001 (ASCII code)

    65 01000001 (8-bit twos complement)Both data was represented in the same binary code, so

    it is important for computer to know the data type

    (remember the variable declaration in C++ ?)

  • 7/31/2019 Data Representation in Computer

    3/22

    CSC2101 3

    Storing Data in Computer

    And also due to computer storage (memory) isorganized in terms of 8-bit per unit (called 1 byte), data

    must be represented in terms of 1 byte, 2 bytes, 4

    bytes, and etc.

    So an integer of 5 is represented as 00000101 in 8-bitsystem or 00000000 00000101 in 16-bit system rather

    than 101.

  • 7/31/2019 Data Representation in Computer

    4/22

    CSC2101 4

    Signed Number Representation

    The sign of a number is specified by the sign bit. Positive number is specified by a 0 bit and negative

    number is specified by a 1 bit.

    Two representations

    Sign Magnitude easy for human but cannot be computed efficiently in

    computer hardware.

    Twos Complement Use by all computer systems nowadays

  • 7/31/2019 Data Representation in Computer

    5/22

    CSC2101 5

    Twos Complement (1)

    Represents negative signed binary numbers in a formthat can be processed by a computer

    The twos complement form is given by the definition

    [N]2 = 2n

    (N)2

    where n is the number of bits in (N)2

  • 7/31/2019 Data Representation in Computer

    6/22

    CSC2101 6

    Twos Complement (2)

    Example 1: Determine twos complement of (01100101)2

    [N]2 = 28 (01100101)2

    = (10000000)2 (01100101)2

    = (10011011)2

    Another method :( 01100101 )2 + 1 = 100110102 + 1

    = 100110112

  • 7/31/2019 Data Representation in Computer

    7/22

    CSC2101 7

    Twos Complement (3)

    Example 2: Determine signed binary representation of610in twos complement

    Representing 610 with a byte 0000 0110

    Twos complement 0000 0110 + 1= 1111 1001 + 1

    = 1111 1010

    To show that (1111 1010) is the negative of (0000 0110)

    1111 1010 + 0000 0110 = 1 0000 0000

    carry bit

  • 7/31/2019 Data Representation in Computer

    8/22

    CSC2101 8

    Twos Complement (4)

    Example 3: Determine the decimal value of the following

    (0101 0000)2

    Sign bit is 0, therefore, it is a positive number

    = 2

    4

    + 2

    6

    = 8010

    (1000 1111 0101 1101)2

    Sign bit is 1, therefore, it is a negative number

    = - (1000 1111 0101 1101 + 1)

    = - (0111 0000 1010 0011)

    = - (20 + 21 + 25 + 27 + 212 + 213 + 214)= - 2883510

  • 7/31/2019 Data Representation in Computer

    9/22

    CSC2101 9

    Twos Complement Arithmetic

    Example : 1410 2010 = ?Can be rewritten as 1410 + (-2010)

    1410 = (0000 1110)2

    2010 = (0001 0100)2

    2010 = 0001 0100 + 1 = 1110 1100

    0000 1110

    +1110 1100

    1111 1010

    to verify :

    1111 1010 + 1 = - (0000 0110) = 610

  • 7/31/2019 Data Representation in Computer

    10/22

    CSC2101 10

    Overflow(1)

    Data/numbers are stored in fixed size insidecomputers memory. For example, integer numbers is

    usually stored as 8-bit or 16-bit (2 byte).

    Overflow occurs when the result of the calculation

    can not be represented by the fixed number of bitsused.

    Example : Suppose we try to add the following two 8-

    bit numbers 0110 11002 + 0101 11102 in an 8-bit

    system. (continue next page)

  • 7/31/2019 Data Representation in Computer

    11/22

    CSC2101 11

    Overflow(2)

    0110 1100 108+ 0101 1110 + 94-------------------------- ----------

    1100 1010 -54

    127-128

    -ve +ve

    0

    21

    126-127

    -1-2

    108

    -54

    Overflow !!

    This is due to the sum is toobig to

    be fitted (represented) into 8-bit

    twos complement binary system.

  • 7/31/2019 Data Representation in Computer

    12/22

    CSC2101 12

    Overflow(3)

    Overflow may happen when

    Arithmetic Operation Overflow

    positive + positive integer Possible

    positive + negative integer No

    negative + negative integer Possible

    negative + positive integer No

    positive - positive integer No

    positive - negative integer Possible

    negative - negative integer No

    negative positive integer Posibble

  • 7/31/2019 Data Representation in Computer

    13/22

    CSC2101 13

    Carry

    When the result of the calculation produces an extrabit out from the most significant bit position, we call

    this bit as carry bit.

    Example :

    1110 1001

    + 0110 1100-----------------

    1 0101 0101

    Carry bit

    msb lsb

    msb = most significant bit

    lsb = least significant bit

  • 7/31/2019 Data Representation in Computer

    14/22

    CSC2101 14

    8-bit Unsigned Number

    Binary Hexadecimal Decimal

    0000 0000 00 0

    0000 0001 01 1

    0000 0010 02 2

    .

    .

    .

    .

    .

    .

    .

    .

    .

    0111 1111 7F 127

    1000 0000 80 128

    1000 0001 81 129

    1000 0010 82 130

    .

    .

    .

    .

    .

    .

    .

    .

    .

    1111 1110 FE 254

    1111 1111 FF 255

  • 7/31/2019 Data Representation in Computer

    15/22

    CSC2101 15

    8-bit Signed Number (2s complement)

    Binary Hexadecimal Decimal

    0000 0000 00 0

    0000 0001 01 1

    0000 0010 02 2

    .

    .

    .

    .

    .

    .

    .

    .

    .

    0111 1111 7F 127

    1000 0000 80 -128

    1000 0001 81 -127

    1000 0010 82 -126

    .

    .

    .

    .

    .

    .

    .

    .

    .

    1111 1110 FE -2

    1111 1111 FF -1

  • 7/31/2019 Data Representation in Computer

    16/22

    CSC2101 16

    Binary Coded Decimal (1)

    Binary Coded Decimal (BCD) is used for representing

    decimal digits 0 through 9

    Each bit position in the code has a fixed numerical value

    or weight associated with it

    Digits represented by a given word can be found by

    summing up the weighted bits

    BCD uses 4 bits, with weights chosen to be the same as

    those of a 4-bit binary integer

  • 7/31/2019 Data Representation in Computer

    17/22

    CSC2101 17

    Binary Coded Decimal (2)

    Decimal Binary Hexadecimal BCD

    0 0000 0 0000

    1 0001 1 0001

    2 0010 2 0010

    3 0011 3 0011

    4 0100 4 0100

    5 0101 5 0101

    6 0110 6 0110

    7 0111 7 0111

    8 1000 8 1000

    9 1001 9 1001

    10 1010 A 0001 0000

    11 1011 B 0001 0001

    12 1100 C 0001 0010

    13 1101 D 0001 0011

    14 1110 E 0001 0100

    15 1111 F 0001 0101

  • 7/31/2019 Data Representation in Computer

    18/22

    CSC2101 18

    Binary Coded Decimal (3)

    Example : Encode the decimal number 9750 in BCD

    Individual digits are encoded91001, 70111, 50101, 00000

    Individual codes are concatenated to1001 0111 0101 0000BCD

  • 7/31/2019 Data Representation in Computer

    19/22

    CSC2101 19

    Binary Coded Decimal (4)

    BCD is used to encode numbers for output tonumerical displays, numerical inputs and for

    representing numbers in processors that perform

    decimal arithmetic

    Applications Digital voltmeters, digital clocks (output) Electronic calculators (input)

  • 7/31/2019 Data Representation in Computer

    20/22

    CSC2101 20

    ASCII Code

    ASCII stands for American Standard Code forInformation Interchange.

    Computers can only understand numbers (binary), so an

    ASCII code is the numerical representation of Englishalphabets and characters such as +' or'@.

  • 7/31/2019 Data Representation in Computer

    21/22

    ASCII Table

    21CSC2101

  • 7/31/2019 Data Representation in Computer

    22/22

    ASCII Code Example

    If you store a string (such as How are you ?) into atext file, and then use a hexadecimal code editor to

    open the text file, you will see the ASCII code (in

    hexadecimal) like below

    48 6F 77 20 61 72 65 20 79 6F 75 3F

    H o w a r e y o u ?

    CSC2101 22

    (space)(space)