CS 2130 - PowerPoint PPT Presentation

1 / 69
About This Presentation
Title:

CS 2130

Description:

CS 2130. Presentation 2a. Data Types. Kinds of Data? What ... Mag. 0. 1. 2. 2147483647 -0 -1 -2147483646 -2147483647. 1's. Comp. 0. 1. 2. 2147483647 -2147483647 ... – PowerPoint PPT presentation

Number of Views:55
Avg rating:3.0/5.0
Slides: 70
Provided by: ble87
Category:
Tags: mag

less

Transcript and Presenter's Notes

Title: CS 2130


1
CS 2130
  • Presentation 2a
  • Data Types

2
Kinds of Data?
  • What kinds of data do computers manipulate?

3
Internal Representation?
  • How is this data stored internally?

4
Datatype
  • A set of values from which a variable, constant,
    function, or other expression may take its value.
    A type is a classification of data that tells the
    compiler or interpreter how the programmer
    intends to use it. For example, the process and
    result of adding two variables differs greatly
    according to whether they are integers, floating
    point numbers, or strings.

5
Typical Scalar Types
  • int
  • float
  • char
  • long
  • short
  • boolean
  • enumeration

Any data type that stores a single value (e.g. a
number or Boolean), as opposed to an aggregate
data type that has many elements.
6
Scalar Types in C
  • int
  • float
  • char
  • long
  • short
  • double
  • boolean
  • enums
  • We'll come back to enumerated types later.

Integer Numbers
7
C Types
  • C "thinks" of 3 basic types char, int and float

in bytes, which are normally but not necessarily
8 bits
8
DON'T FORGET!
  • signed
  • unsigned

9
More Bits, More Precision
  • short (2)...int (4)
  • Twice as long, twice as precise
  • Given an N bit number, how many different numbers
    can be represented?
  • Your final answer?

10
Depends!
  • Unsigned
  • 2N
  • Numbers range from 0 to 2N-1
  • Signed
  • 2N-1 or 2N
  • Depends on representation
  • Signed Magnitude
  • 1's Complement
  • 2's Complement
  • Biased/Excess Notation

11
Ranges
  • Unsigned
  • Signed Magnitude
  • 1's Complement
  • 2's Complement
  • Biased/
  • Excess Notation
  • (Bias B)

From 0 -(2N-1 - 1) -(2N-1 - 1) -(2N-1) -B
To 2N - 1 (2N-1 - 1) (2N-1 - 1) (2N-1 -
1) 2N - 1 - B
12
What?
N 3
Unsigned 0 1 2 3 4 5 6 7
Binary 000 001 010 011 100 101 110 111
Signed Mag 0 1 2 3 -0 -1 -2 -3
1's Comp 0 1 2 3 -3 -2 -1 -0
2's Comp 0 1 2 3 -4 -3 -2 -1
Excess 3 -3 -2 -1 0 1 2 3 4
Excess 4 -4 -3 -2 -1 0 1 2 3
Number Stored
Number Represented
13
N 4
Number Represented
Unsigned 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Binary 0000 0001 0010 0011 0100 0101 0110 0111 1
000 1001 1010 1011 1100 1101 1110 1111
Signed Mag 0 1 2 3 4 5 6 7 -0 -1 -2 -3 -4 -5 -6 -
7
1's Comp 0 1 2 3 4 5 6 7 -7 -6 -5 -4 -3 -2 -1 -0
2's Comp 0 1 2 3 4 5 6 7 -8 -7 -6 -5 -4 -3 -2 -1
Excess 7 -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 8
Excess 8 -8 -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7
14
N 8
Unsigned 0 1 2 ... 127 128 129 ... 254 255
Binary 00000000 00000001 00000010 ... 01111111 1
0000000 10000001 ... 11111110 11111111
Signed Mag 0 1 2 ... 127 -0 -1 ... -126 -127
1's Comp 0 1 2 ... 127 -127 -126 ... -1 -0
2's Comp 0 1 2 ... 127 -128 -127 ... -2 -1
Excess 127 -127 -126 -125 ... 0 1 2 ... 127 128
Excess 128 -128 -127 -126 ... -1 0 1 ... 126 127
15
N 32
Unsigned 0 1 2 ... 2147483647 2147483648 2147483
649 ... 4294967294 4294967295
Binary 00000000000000000000000000000000 00000000
000000000000000000000001 0000000000000000000000000
0000010 ... 01111111111111111111111111111111 10000
000000000000000000000000000 1000000000000000000000
0000000001 ... 11111111111111111111111111111110 11
111111111111111111111111111111
Hex 0 1 2 ... 7FFFFFFF 80000000 80000001 ... FFF
FFFFE FFFFFFFF
16
N 32
Signed Mag 0 1 2 ... 2147483647 -0 -1 ... -214748
3646 -2147483647
1's Comp 0 1 2 ... 2147483647 -2147483647 -214748
3646 ... -1 -0
2's Comp 0 1 2 ... 2147483647 -2147483648 -214748
3647 ... -2 -1
Unsigned 0 1 2 ... 2147483647 2147483648 2147483
649 ... 4294967294 4294967295
Hex 0 1 2 ... 7FFFFFFF 80000000 80000001 ... FFF
FFFFE FFFFFFFF
17
Where do bits come from?
18
NOT
19
So what's the difference...
  • Between a char 1 and an int 1
  • about 48?
  • Between the ip address 128.43.56.102 and
    0x802B3866 and -2,144,978,842 and 2,149,988,454
  • The letter A and an int 65

20
Why two codes?
Why is this at the end?
What about these?
21
Paper Tape
22
Key Point
  • Data is stored internally as binary numbers with
    no meaning to the computer.
  • All meaning is assigned by programmer using
    programming languages and other tools.
  • Example
  • char c 65
  • printf(" d \n", c)
  • printf(" c \n", c)
  • Output
  • 65
  • A

23
Unsigned
  • Add 89 and 205
  • Step 1 Convert 89 and 205 to binary

24
Converting to Binary
  • Use dc
  • See man dc
  • Use some other calculator
  • Learn how to do it by hand !!!

25
Old Stuff
  • http//www.pgh.net/newcomer/hp16c.htm

26
Converting to Binary
  • 89 odd 1
  • 44 even 0
  • 22 even 0
  • 11 odd 1
  • 5 odd 1
  • 2 even 0
  • 1 odd 1
  • 0 even 0
  • 01011001

27
Converting to Binary
  • 205 odd 1
  • 102 even 0
  • 51 odd 1
  • 25 odd 1
  • 12 even 0
  • 6 even 0
  • 3 odd 1
  • 1 odd 1
  • 11001101

28
Why does this work?
  • 47 odd 1
  • 23 odd 1
  • 11 odd 1
  • 5 odd 1
  • 2 even 0
  • 1 odd 1
  • 0 even 0
  • 0 even 0
  • 00101111
  • 48 even 0
  • 24 even 0
  • 12 even 0
  • 6 even 0
  • 3 odd 1
  • 1 odd 1
  • 0 even 0
  • 0 even 0
  • 00110000

29
Unsigned
  • Add 89 and 205
  • 01011001
  • 11001101
  • 100100110
  • Result ???


0
1
0
0
1
1
1
10
30
What happened?
  • Overflow (carry out from high order bit) means
    that the result is bigger than the allowed number
    of bits
  • 89 205 294
  • Assumption for this discussion Two N bit numbers
    may be added together and overflow can be
    detected
  • BTW When adding two N bit numbers
    together...what is the maximum number of bits the
    result can have?
  • But what if we need signed numbers?

31
Signed Magnitude
  • Both positive and negative zero
  • Equal number of positives and negatives
  • Easy to interpret
  • First bit is the sign
  • Remaining bits are number
  • Sounds ideal?

32
Signed Magnitude?
  • Add these two numbers
  • 01011001
  • 11001101
  • What does this represent?

33
Signed Magnitude?
  • What does this represent?
  • 010110012 8910
  • 110011012 -7710
  • How do we do the problem in decimal arithmetic?
  • Note This is not subtraction. Addition of signed
    numbers
  • Recall 3rd Grade

34
Signed Magnitude?
  • What does this represent?
  • 010110012 8910
  • 110011012 -7710
  • 0

If signs are different sign of result will be
sign of larger operand
35
Signed Magnitude?
  • What does this represent?
  • 010110012 8910
  • 110011012 -7710
  • 000011002 1210
  • What are the implications of this operation?

36
1's Complement
  • Easy
  • Create binary representation of negative number,
    ignoring sign
  • Flip all bits
  • Example -94
  • 01011110
  • Flip Bits
  • 10100001

37
2's Complement
  • Calculate 1's Complement
  • Add 1
  • Example -94
  • 01011110
  • Produce 1's Complement
  • 10100001
  • Add 1
  • 10100010
  • Why do we do this?
  • Hardware Reasons

38
Why does it work?
  • Consider decimal numbers.
  • Suppose we have 3 digits available 000 - 999
  • We can use 10's complement!
  • Example -94
  • First form 9's complement of 094
  • 905
  • Add 1
  • 906
  • But what is this?
  • 906 - 1000 ?
  • So what?

39
Add 107 and -94
  • Decimal Form
  • 107
  • -94
  • Rewrite as
  • 107
  • 906 - 1000
  • Add
  • 1013 - 1000
  • 13

40
2's Complement?
  • What does this represent?
  • 010110012
  • 110011012

41
2's Complement?
  • What's the decimal answer?
  • 010110012 8910 89
  • 110011012 -5110 205 - 256

42
2's Complement?
  • What happens if we add the binary numbers?
  • 010110012 8910 89
  • 110011012 -5110 205 - 256
  • 3810

43
2's Complement?
  • The eight bit result is correct
  • 010110012 8910 89
  • 110011012 -5110 205 - 256
  • 1001001102 3810 294 - 256

3810
44
2's Complement?
  • Overflow?
  • 010110012 8910 89
  • 110011012 -5110 205 - 256
  • 1001001102 3810 294 - 256
  • What's the rule?

45
Consider
  • 1 digit decimal numbers
  • Positive numbers 0 1 2 3 4
  • Negative numbers 9 8 7 6 5 (-1 -2 -3 -4 -5)
  • 2 2 -2 2 -2 -2
  • 2 4 4 -4 -4 -2
  • 4 6 2 -2 -6 -4
  • 2 2 8 2 8 8
  • 2 4 4 6 6 8
  • 4 6 12 8 14 16
  • Ok Bad Ok Ok Bad Ok

46
Recall
N 3
Unsigned 0 1 2 3 4 5 6 7
Binary 000 001 010 011 100 101 110 111
Signed Mag 0 1 2 3 -0 -1 -2 -3
1's Comp 0 1 2 3 -3 -2 -1 -0
2's Comp 0 1 2 3 -4 -3 -2 -1
Excess 3 -3 -2 -1 0 1 2 3 4
Excess 4 -4 -3 -2 -1 0 1 2 3
Number Stored
Number Represented
47
Consider
  • 3 bit numbers
  • Max allowed 3 (011)
  • Min allowed -4 (100)
  • 2 -2 2 -2
  • 3 3 -3 -3
  • 5 1 -1 -5
  • 010 110 010 110
  • 011 011 101 101
  • 0101 1001 0111 1011
  • Bad Ok Ok Bad

If carry in to sign bit ! carry out of sign bit
Overflow
48
But why?
  • Back to decimal land...
  • Using complement notation to represent negative
    numbers looks like this
  • Assume 3 decimal digits available (no sign)
  • Want -42
  • Subtract 42 from 1000 958 (10's Complement)
  • So we get 958 - 1000
  • If we want to add 98 and -42 (Should get 56)
  • 98 - 0
  • 958 - 1000
  • 1056 - 1000 56

49
Think about forming the complement
  • 100000
  • 42653
  • 57347
  • Another way
  • 42653
  • Write down 9's complement
  • 57346
  • Then add 1
  • 57347

50
Binary works the same way!
  • What's the 2's complement of 56?
  • Assume we have an 8 bit machine
  • 56 111000
  • 100000000
  • -111000
  • 11001000
  • Easier way
  • Start with 00111000
  • Take 1's complement 11000111
  • Add 1 11001000

51
Binary works the same way!
  • 2 -2 2 -2
  • 3 3 -3 -3
  • 5 1 -1 -5
  • 010-0 110-1000 010- 0 110 -1000
  • 011-0 011- 0 101-1000 101 -1000
  • 101 1001-1000 111-1000 1011-10000
  • -3 1 -1 -5
  • Bad Ok Ok Bad

52
Questions?
53
Excess Notation
  • Need to define bias
  • Add bias to number to be represented
  • Example
  • Bias 127
  • Number to be represented 42
  • Calculate 42 127 169
  • Convert to binary unsigned magnitude 10101001
  • To decode, subtract bias
  • Calculate 169 - 127 42

We will see Excess Notation again with floating
point
54
Binary Coded Decimal
  • For certain applications such as representing
    currency amounts, numbers must be exact.
  • The problems of representing decimal floating
    point numbers with binary representations cannot
    be tolerated
  • Solution BCD or Binary Coded Decimal

55
BCD
  • Encode each decimal digit as a sequence of binary
    digits
  • 0 0000
  • 1 0001
  • 2 0010
  • 3 0011
  • 4 0100
  • 5 0101
  • 6 0110
  • 7 0111
  • 8 1000
  • 9 1001
  • Note 1010, 1011, 1100, 1101, 1110 and 1111 not
    used.

56
BCD
  • Example Decimal 4045
  • Binary 1111 1100 1101 (12 digits)
  • BCD 0100 0000 0100 0101 (16 digits)
  • BCD is less efficient from the standpoint of
    number of bits required to store a number.

57
BCD
  • Special routines are required to do math
  • Example 0011 3
  • 0100 4
  • 0111 7 (less 10...okay)
  • 0111 7
  • 1000 8
  • 1111 15 (gt9 add 6)
  • 0110
  • 1 0101 5 (with carry)

58
BCD
  • Typical approaches to negative numbers use 9's
    and 10's complement arithmetic
  • BCD numbers are implemented in COBOL

59
Questions?
60
Booleans
  • There is no boolean type in C but...
  • For the purpose of boolean operations
  • (and)
  • (or)
  • ! (not)
  • C can treat ints as boolean values
  • False ? 0
  • True ? Anything else
  • Note and use short circuit evaluation

61
Examples
  • int a, b, c, d
  • / Values assigned to a, b, c and d /
  • if((a gt b) (c lt d))
  • if((a ! b) (c d))
  • / Note the ! (unary negation operator) /
  • int x 43
  • int y 0
  • !x ? ?
  • !y ? ?

62
Examples
  • / Going to use this as a boolean /
  • int valid
  • Which do you prefer?
  • if(!valid)
  • or
  • if(valid 0)

Warning Since there is no true boolean type
there are lots of things that can go wrong! Be
careful!
63
Bitwise Operations
  • In addition to the preceding boolean operations
    which treated an int sort of as a boolean value C
    also features bit wise operations
  • - bitwise and
  • - bitwise or
  • - bitwise complement
  • - bitwise xor
  • ltlt - left shift
  • gtgt - right shift

64
Examples
  • short i 42 / 0000 0000 0010 1010 /
  • short j 19 / 0000 0000 0001 0011 /
  • i j ?
  • 2 0000 0000 0000 0010
  • i j ?
  • 59 0000 0000 0011 1011
  • i j ?
  • 57 0000 0000 0011 1001
  • i ?
  • -43 1111 1111 1101 0101

65
Examples
  • short i 42 / 0000 0000 0010 1010 /
  • short j 19 / 0000 0000 0001 0011 /
  • i ltlt 4 ?
  • 672 0000 0010 1010 0000
  • j gtgt 4 ?
  • 1 0000 0000 0000 0001

66
Questions?
67
Key Ideas
  • The computer just manipulates bits.
  • The same bit pattern can mean different things
    based on programmer and software
  • Different representations are used internally
    based on considerations such as
  • Cost
  • Speed
  • Typical time vs space tradeoffs.
  • YOU REALLY NEED TO UNDERSTAND THIS STUFF

68
Questions?
69
(No Transcript)
Write a Comment
User Comments (0)
About PowerShow.com