Title: CS 2130
1CS 2130
- Presentation 2a
- Data Types
2Kinds of Data?
- What kinds of data do computers manipulate?
3Internal Representation?
- How is this data stored internally?
4Datatype
- 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.
5Typical 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.
6Scalar Types in C
- int
- float
- char
- long
- short
- double
- boolean
- enums
- We'll come back to enumerated types later.
Integer Numbers
7C Types
- C "thinks" of 3 basic types char, int and float
in bytes, which are normally but not necessarily
8 bits
8DON'T FORGET!
9More 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?
10Depends!
- 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
11Ranges
- 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
12What?
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
13N 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
14N 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
15N 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
16N 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
17Where do bits come from?
18NOT
19So 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
20Why two codes?
Why is this at the end?
What about these?
21Paper Tape
22Key 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
23Unsigned
- Add 89 and 205
- Step 1 Convert 89 and 205 to binary
-
24Converting to Binary
- Use dc
- See man dc
- Use some other calculator
- Learn how to do it by hand !!!
25Old Stuff
- http//www.pgh.net/newcomer/hp16c.htm
26Converting 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
27Converting 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
28Why 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
29Unsigned
- Add 89 and 205
- 01011001
- 11001101
- 100100110
- Result ???
0
1
0
0
1
1
1
10
30What 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?
31Signed 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?
32Signed Magnitude?
- Add these two numbers
- 01011001
- 11001101
-
- What does this represent?
33Signed 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
34Signed Magnitude?
- What does this represent?
- 010110012 8910
- 110011012 -7710
- 0
If signs are different sign of result will be
sign of larger operand
35Signed Magnitude?
- What does this represent?
- 010110012 8910
- 110011012 -7710
- 000011002 1210
- What are the implications of this operation?
361's Complement
- Easy
- Create binary representation of negative number,
ignoring sign - Flip all bits
- Example -94
- 01011110
- Flip Bits
- 10100001
372'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
38Why 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?
39Add 107 and -94
- Decimal Form
- 107
- -94
- Rewrite as
- 107
- 906 - 1000
- Add
- 1013 - 1000
- 13
402's Complement?
- What does this represent?
- 010110012
- 110011012
-
412's Complement?
- What's the decimal answer?
- 010110012 8910 89
- 110011012 -5110 205 - 256
-
422's Complement?
- What happens if we add the binary numbers?
- 010110012 8910 89
- 110011012 -5110 205 - 256
- 3810
432's Complement?
- The eight bit result is correct
- 010110012 8910 89
- 110011012 -5110 205 - 256
- 1001001102 3810 294 - 256
3810
442's Complement?
- Overflow?
- 010110012 8910 89
- 110011012 -5110 205 - 256
- 1001001102 3810 294 - 256
- What's the rule?
45Consider
- 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
46Recall
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
47Consider
- 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
48But 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
49Think about forming the complement
- 100000
- 42653
- 57347
- Another way
- 42653
- Write down 9's complement
- 57346
- Then add 1
- 57347
50Binary 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
51Binary 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
52Questions?
53Excess 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
54Binary 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
55BCD
- 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.
56BCD
- 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.
57BCD
- 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)
58BCD
- Typical approaches to negative numbers use 9's
and 10's complement arithmetic - BCD numbers are implemented in COBOL
59Questions?
60Booleans
- 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
61Examples
- 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 ? ?
62Examples
- / 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!
63Bitwise 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
64Examples
- 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
65Examples
- 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
66Questions?
67Key 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
68Questions?
69(No Transcript)