Primitive Data Part 1 (integers, addition, hexadecimal, 2`s comp)

advertisement
cs3843
syllabus
lecture notes
programming
assignments
recitations
homework
set up
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
int
depends on the primitive data type.
A byte contains 8 bits. A character is a 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).
For 16 bits with a sign, there are 215 (32,768) non-negative values, but we
say the highest value is 32,768 - 1 (32,767). For negative values, the
lowest value is -32,768. Why 32,767, but -32,768?
In decimal, each digit position represents a power of
Integer Values as Binary
Our integer values are stored as binary (i.e., base 2). base 10. The decimal number 45,367 is 4x104 + 5 x 103 +
3x102 + 6x101 + 7 x 100
Below, the right side is the lower-order which
Power
4
3
2
1
0
represents smaller values than positions on the left.
Assuming a two byte 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
00000001 ?
300
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:
15 is 00000000 ?
7 is 00000000 ?
adding 00000000 ?
Add 30 and 20:
30 is 00000000 ?
20 is 00000000 ?
adding 00000000 ?
For
signed
magnitude, there are some issues:
Representing Negative Numbers
 There are two zeroes (positive and negative).
We need one bit for a sign. How could we represent
00000000 00000000
negative numbers?
10000000 00000000
 Signed Magnitude. Negative values would
 Adding a positive and a negative value is very difficult.
have a 1 in the sign bit. The rest of the value
would be a binary representation of the
integer.
12 is 00000000 00001100
-12 is 10000000 00001100
 Ones' Complement. Negative values are
represented by complementing the binary
representation for a positive integer (simply
change all 0s to 1s and all 1s to 0s).
12 is 00000000 00001100
-12 is 11111111 11110011
 Two's Complement. Negative values are
represented by taking the 1's Complement
and then adding 1.
Exercise: show -20 and -7
For 1's Complement, there are some issues:
 There are two zeroes (positive and negative)
00000000 00000000
11111111 11111111
 Adding a positive and a negative value is difficult.
For 2's complement, we have one zero (00000000 00000000) and we
can add them easily with hardware.
To get -12 in 2's comp:
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)
To get -7:
7 is
00000000 00000111
1's comp is
?
add 1
00000000 00000001
2's comp is
?
(this is -7)
To get -20
20 is
00000000 00010100
1's comp is
11111111 ?
2's comp is
11111111 ?
(this is -20)
Subtract 7 from 15
15 is
00000000
-7 is
11111111
result
?
Subtract 20 from 15
15 is
00000000
-20 is
11111111
result
? (what
Exercises:
subtract 7 from 15.
subtract 20 from 15.
?
?
?
?
number is that?)
00000000 ?
00000000 ?
Is there a quicker way to recognize the decimal equivalent
for a negative 2's comp value?
Consider the value 11111111 11101100
Starting from the left, what is the position of the last 1
before hitting a 0? 5 power position.
Add -25 + 8 + 4 = -32 + 12 = -20
Further Examination of Recognizing the decimal value
Assume our word size is w = 8 bits.
11101100 = -32 + 8 + 4 = -20
Suppose we use the previous sign bit position:
11101100 = -64 + 32 + 8 + 4 = -20
Suppose we use the first sign bit position:
11101100 = -128 + 64 + 32 + 8 + 4 = -20
Note for tests:
I will provide you with this table to help those that need it.
What is the decimal
11111111 11111001 =
11111111 11110100 =
11111100 00010000 =
11111111 00100010 =
(1's comp)
(so that was ? since this is ?)
equivalent to each of these?
-8 + 1 = -7
-16 + 4 = -12
-1024 + 16 = -1008
?
What is each of the following in decimal?
00000000 01010101
11111111 01010101
10
1024
9
512
8
256
7
128
6
64
5
32
4
16
3
8
2
4
1
2
0
1
Hexadecimal
Data is represented as hexadecimal (base 16) values to
simplify interpretation of bit patterns. An hexadecimal value
represents 4 bits.
Decimal
Value
0
1
2
3
4
Binary
Value
0000
0001
0010
0011
0100
Hexadecimal
0
1
2
3
4
What is each of the following bit patterns in hex?
1100 1001 is C9
1101 0010 is ?
0100 1010 is ?
5
6
7
8
9
10
11
12
13
14
15
0101
0110
0111
1000
1001
1010
1011
1100
1101
1110
1111
5
6
7
8
9
A
B
C
D
E
F
0101 0011 is ?
Hexadecimal (base 16) and Octal (base 8) Constants in C
Hexadecimal Constants: begin with 0x
0x1D = 1 x 161 + 13 x 160
= 16 + 13
= 29 (decimal)
0x2710 = 2 x 163 + 7 x 162 + 1 x 161 + 0
= 2 x 4096 + 7 x 256 + 1 x 16 + 0
= 8192 + 1792 + 16
= 10000 (decimal)
Octal (base 8) Constants: begin with 0
012 = 1 x 81 + 2 x 80 = 8 + 2 = 10 (decimal)
035 = 3 x 81 + 5 x 80 = 24 + 5 = 29 (decimal)
Printing Hexadecimal Integer Values in C
The %X format code is used to print hexadecimal values:
%X - print integer value in hexadecimal with uppercase
letters, suppressing leading zeros.
%nX - print at least n hex characters, replacing leading
zeros with spaces if within n characters.
%0nX - print at least n hex characters, but do not suppress
leading zeros within n hex characters.
Convert from hexadecimal to decimal
0xA3 = 10 x 16 + 3 = 163
0x231 = 2 x 256 + 3 x 16 + 1 = 512+48+1 = 561
Show the following decimal values as hexadecimal
constants
100 = 6 x 16 + 4
= 0x64 (hexadecimal)
1994 = 7 x 256 + 12 x 16 + 10
= 0x7CA (hexadecimal)
// Sample code
long lValueM[] = { 10, 100, 0xABC, 0x1234567, -1 };
int i;
printf("Decimal \t%%X\t%%4X\t%%04X\n");
for (i = 0; lValueM[i] > 0; i++)
{
printf("%9ld\t%X\t%4X\t%04X\n"
, lValueM[i], lValueM[i]
, lValueM[i], lValueM[i]);
}
// Output
Decimal
10
100
%X
A
64
%4X
A
64
%04X
000A
0064
Words and Endian Physical Implementations
Word size on a machine is dependent on the size which
works efficiently. For machines which use 32 bits for
addressing, the word size is 4 bytes (32 bits).
2748
ABC
ABC
0ABC
19088743
1234567 1234567 1234567
When storing 0x789ABCDE in memory
little endian: DE BC 9A 78
big endian:
78 9A BC DE
If you are only working on one type of machine, it usually
Suppose the 4 byte integer value 0x789ABCDE needs to be
doesn't matter.
stored in memory. There are two different well known
hardware-dependent representations for storing in memory:
When would it matter?
little endian
store the least significant digit
first. This is used by Intel chips.
big endian
store the least significant digit last.
Download