Primitive Data

advertisement
clark
cs2123
lecture notes
programming assignments
homework
set up
Last Updated: 8/15/2015 (subject to change within 24 hours of lecture)
In C, we
Primitive Data
char
Primitive data includes characters, booleans,
integers, and floating point. Since each of those
consists of one or more bytes, what is the meaning
of the bits within those bytes? It depends on the
int
primitive data type.
A byte contains 8 bits. A character is a byte.
Integer Values as Binary
Our integer values are stored as binary (i.e., base 2). The
right side is the lower-order which represents smaller
values than positions on the left. Assuming a two byte
have these data types:
one byte, can represent printable
characters. Although there are 28 possible
values, we usually only represent 128
values.
two bytes or four bytes depending on
implementation. For two bytes, the values
range from -32,768 to 32,767. For four
bytes, -2,147,483,648 (approximately -2
billion) to 2,147,483,647 (approximately +2
billion).
float four bytes. The IEEE 754 standard
representation contains a sign bit, an 8-bit
exponent, and a 23-bit significand
(mantissa).
double 8 bytes. The IEEE 754 standard
representation contains a sign bit, an 11bit exponent, and a 52-bit significand
(mantissa).
In decimal, each digit position represents a power of
base 10. The decimal number 45,367 is 4x104 + 5 x 103 +
3x102 + 6x101 + 7 x 100
Power
4
3
2
1
0
representation of an integer:
Decimal Value Two Byte Binary
Representation
00000000 00000001
1
00000000 00000010
2
00000000 00000011
3
00000000 00000100
4
00000000 00000101
5
00000000 00001000
8
00000000 00001001
9
00000000 00001010
10
00000000 00010000
16
00000000 00100000
32
00000000 01000000
64
00000000 ?
100
00000000 10000000
128
00000001 00000000
256
Adding Positive Binary Values
In decimal, we add values in the 1s position (zero power), possibly
carry, and then add values in the tens position (1 power), possibly
carry, and so on.
In binary, we add the bits in the zero power position, possibly carry,
and then add the bits in the one power, possibly carry, and so on.
Exercise: Add 15 and 7; Add 30 and 20.
Digit
4
5
3
6
7
In binary, each bit position also represents a power,
but of base 2. The value 21 is 16 + 4 + 1. Shown
with powers of base 2, that is
1x24 + 0x23 +1x22 + 0x21 + 1x20.
Power
4
3
2
1
0
Binar
1
0
1
0
1
y
Decim
16
4
1
al
Add 21 and 12.
21 is 00000000 00010101
12 is 00000000 00001100
adding 00000000 00100001
Note that it carried in the powers 2, 3, and 4.
Add 15 and 7:
Representing Negative Numbers
Instead of simply using one bit for a sign, negative
numbers are represented using two's complement. This
makes it easier to add or subtract values.
15 is 00000000
7 is 00000000
adding 00000000
Add 30 and 20:
30 is 00000000
20 is 00000000
adding 00000000
We already know what
12 is
1's comp is
Add 1
2's comp is
?
?
?
?
?
?
12 is
00000000
11111111
00000000
11111111
00001100
11110011
00000001
11110100
(this is -12)
We can get the two's complement of a binary value by
1. Take its one's complement (simply change all 0s to
1s and all 1s to 0s).
2. Add 1 to the result.
If we add 23 and -12 (i.e., subtract 12 from 23):
23 is 00000000 00010111
-12 is 11111111 11110100
adding 00000000 00001011 (the last carry falls off)
Exercise: show -20 and -7
To get -7:
7 is
00000000 00000111
1's comp is
11111111 ?
add 1
00000000 00000001
2's comp is
?
(this is -7)
To get -20
20 is
00000000 ?
1's comp is
11111111 ?
2's comp is
?
(this is -20)
Subtract 7 from 15
15 is
00000000 ?
-7 is
11111111 11111001
result
? (this is 8)
Subtract 20 from 15
15 is
00000000 ?
Exercises:
subtract 7 from 15.
subtract 20 from 15.
-20 is
?
?
(what number is that?
00000000 00000100 (1's comp)
00000000 00000101 (so that was -5 since this is 5)
Hexadecimal
Data is represented as hexadecimal (base 16) values to simplify
interpretation of bit patterns. An hexadecimal value represents 4 bits.
Decimal
Binary
Hexadecimal
Value
Value
0
0000
0
1
0001
1
2
0010
2
3
0011
3
4
0100
4
5
0101
5
6
0110
6
7
0111
7
8
1000
8
9
1001
9
10
1010
A
11
1011
B
12
1100
C
13
1101
D
14
1110
E
15
1111
F
Characters
For the USA, there are two standard encoding of 8 bit characters:
EBCDIC - Extended Binary Coded Decimal Interchange Code is
mainly used on IBM mainframe computers. It descended
from punch card 6 bit representations.
ASCII - American Standard Code for Information Interchange has
What is each of the following bit patterns in hex?
1100 1001 is C9
1101 0010 is ?
0100 1010 is ?
0101 0011 is ?
Letter ASCII
Decima
l
A
65
B
66
C
67
ASCII
(Hex)
EBCDIC
(HEX)
41
42
43
C1
C2
C3
become the more dominant encoding.
Do you notice anything that might be annoying about the EBCDIC value
sequence?
Printable Character Representations of Numbers
D
E
F
G
H
I
J
K
L
M
N
O
P
Q
R
S
T
U
V
W
X
Y
Z
Number
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
ASCII
(Hex)
0
1
2
3
4
5
30
31
32
33
34
35
44
45
46
47
48
49
4A
4B
4C
4D
4E
4F
50
51
52
53
54
55
56
57
58
59
5A
EBCDI
C
(Hex)
F0
F1
F2
F3
F4
F5
C4
C5
C6
C7
C8
C9
D1
D2
D3
D4
D5
D6
D7
D8
D9
E2
E3
E4
E5
E6
E7
E8
E9
6
7
8
9
Converting a Single Numeric Character to a Number
We can simply subtract the character '0' from it.
int ivalue;
char cvalue;
// suppose cvalue contains a printable numeric character
ivalue = cvalue - '0';
36
37
38
39
F6
F7
F8
F9
Download