clark / cs2123 / Course Outline / lecture notes / programming assignments / homework / set up

Last Updated:1/7/2014 (subject to change within 24 hours of lecture)

Primitive Data
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 primitive data type.
A byte contains 8 bits. A character is a byte. / In C, we have these data types:
charone byte, can represent printable characters. Although there are 28 possible values, we usually only represent 128 values.
inttwo 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).
floatfour bytes. The IEEE 754 standard representation contains a sign bit, an 8-bit exponent, and a 23-bit significand (mantissa).
double8 bytes. The IEEE 754 standard representation contains a sign bit, an 11-bit exponent, and a 52-bit significand (mantissa).
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 representation of an integer:
Decimal Value / Two Byte Binary Representation
1 / 00000000 00000001
2 / 00000000 00000010
3 / 00000000 00000011
4 / 00000000 00000100
5 / 00000000 00000101
8 / 00000000 00001000
9 / 00000000 00001001
10 / 00000000 00001010
16 / 00000000 00010000
32 / 00000000 00100000
64 / 00000000 01000000
100 / 00000000 ?
128 / 00000000 10000000
256 / 00000001 00000000
/ 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
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
Binary / 1 / 0 / 1 / 0 / 1
Decimal / 16 / 4 / 1
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. / 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.
Exercise: Add 15 and 7; Add 30 and 20. / Add 15 and 7:
15 is 00000000 00001111
7 is 00000000 00000111
adding 00000000 00010110
Add 30 and 20:
30 is 00000000 00011110
20 is 00000000 00010100
adding 00000000 00110010
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.
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.
/ We already know what 12 is
12 is 00000000 00001100
1's comp is 11111111 11110011
Add 1 00000000 00000001
2's comp is 11111111 11110100 (this is -12)
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 11111000
add 1 00000000 00000001
2's comp is 11111111 11111001 (this is -7)
To get -20
20 is 00000000 00010100
1's comp is 11111111 11101011
2's comp is 11111111 11101100 (this is -20)
Exercises:
subtract 7 from 15.
subtract 20 from 15. / Subtract 7 from 15
15 is 00000000 00001111
-7 is 11111111 11111001
result 00000000 00001000 (this is 8)
Subtract 20 from 15
15 is00000000 00001111
-20 is11111111 11101100
11111111 11111011 (what number is that?
00000000 00000100
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 Value / Binary Value / Hexadecimal
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
/ What is each of the following bit patterns in hex?
1100 1001 is C9
1101 0010 is ?
0100 1010 is ?
0101 0011 is ?
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 become the more dominant encoding.
Do you notice anything that might be annoying about the EBCDIC value sequence? / Letter / ASCII Decimal / ASCII (Hex) / EBCDIC (HEX)
A / 65 / 41 / C1
B / 66 / 42 / C2
C / 67 / 43 / C3
D / 68 / 44 / C4
E / 69 / 45 / C5
F / 70 / 46 / C6
G / 71 / 47 / C7
H / 72 / 48 / C8
I / 73 / 49 / C9
J / 74 / 4A / D1
K / 75 / 4B / D2
L / 76 / 4C / D3
M / 77 / 4D / D4
N / 78 / 4E / D5
O / 79 / 4F / D6
P / 80 / 50 / D7
Q / 81 / 51 / D8
R / 82 / 52 / D9
S / 83 / 53 / E2
T / 84 / 54 / E3
U / 85 / 55 / E4
V / 86 / 56 / E5
W / 87 / 57 / E6
X / 88 / 58 / E7
Y / 89 / 59 / E8
Z / 90 / 5A / E9
Printable Character Representations of Numbers / Number / ASCII (Hex) / EBCDIC (Hex)
0 / 30 / F0
1 / 31 / F1
2 / 32 / F2
3 / 33 / F3
4 / 34 / F4
5 / 35 / F5
6 / 36 / F6
7 / 37 / F7
8 / 38 / F8
9 / 39 / F9
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';