Lecture 03 Data Representation Integers
Lecture 03 Data Representation Integers
1
Last Lecture
von Neumann architecture
Architecture of most computers
Its components: CPU, memory, input and ouput, bus
One of its characteristics: Data and instructions (code/programs) both stored in memory
A look at memory: defined byte-addressable memory, (compressed) diagram of memory
A look at memory content
Why we abstract the content of a memory cell using the two values of the binary numeral
system -> ‘0’ and ‘1’
Algorithm for converting binary to hexadecimal (hex)
1. Partition bit vector into groups of 4 bits, starting from right, i.e., least significant byte (LSB)
If most significant byte (MSB) does not have 8 bits, pad it: add 0’s to its left
Determined by a 2. Translate each group of 4 bits into its hex value
microprocessor.
Describes how a Encoding scheme gives meaning to bit vectors (memory content)
NOTE:
microprocessor little endian versus big endian: Order in which bytes are stored in memory C logical
orders the bytes Bit manipulation – regardless of what a bit vector represents operators and
when it reads them C bitwise (bit-
from and writes Boolean algebra: bitwise operations => AND (&), OR (|), XOR (^), NOT (~) level) operators
them to memory. Shift operations: left shift x << y, logical and arithmetic right shift x >> y behave
differently!
Right logical shift: Fill x with y 0’s on the left of x
2 Watch out for
Right arithmetic shift: Fill x with y copies of x‘s sign bit on the left of x && versus &,
Sign bit is most significant bit (MSb) before shifting occurred || versus |, …
If memory size is 26 bytes, then size = 64 bytes. In order to access each
From Lecture 2 of the 64 bytes of memory, we need 64 distinct memory addresses. To
express each of these 64 memory addresses, we require 6 bits.
size-1
size-1
= 6310
= 0x003F
leading zeros
= 0x3F
= 001111112
6 bits
leading zeros
63
3
Today’s Menu
Representing data in memory
“Under the Hood” - von Neumann architecture
Bits and bytes in memory
How to diagram memory -> used in this course and other references
How to represent memory content (series of bits) -> in binary, in hexadecimal and conversion
What kind of information (data) do series of bits represent -> encoding scheme
Order of bytes in memory -> endian
Bit manipulation – bitwise operations
Boolean algebra + shifting
Representing integral numbers in memory
Unsigned and signed
Converting, expanding and truncating
Arithmetic operations
Representing real numbers in memory
4 IEEE floating point representation
Floating point in C – casting, rounding, addition, …
Warm up exercise!
w−1
xi 2
i
B2U(X) =
Let’s apply the encoding scheme: i=0
0 x 27 + 1 x 2 6 + 1 x 2 5 + 0 x 2 4 + 1 x 2 3 + 0 x 2 2 + 0 x 2 1 + 1 x 2 0 =
100 4
••• 10 ••• 2
1 1
di di-1 ••• d2 d1 d0
w−1
xi 2
i
Example: 24610 = 2 x 102 + 4 x 101 + 6 x 100 B2U(X ) =
i=0
1’s = 100
7 10’s = 101
100’s = 102
Homework 1
8
Examples of “Show your work”
U2B(X) Conversion (into 8-bit binary numbers => w = 8)
If X = -14 If X = -14
1. |X| => |-14| = 1. X + 2w => -14 +
3. ~(000011102) =>
Using subtraction:
242 – 128 = 114 -> 1 x 27
4. (111100012)+1 => 114 – 64 = 50 -> 1 x 26
50 – 32 = 18 -> 1 x 25
Binary addition: 18 – 16 = 2 -> 1 x 24
11110001 2 – 8 -> nop! -> 0 x 23
+ 00000001 2 – 4 -> nop! -> 0 x 22
2–2=0 -> 1 x 21
0 – 1 -> nop! -> 0 x 20
12
Always check your answer! How?
Properties of unsigned & signed conversions
w=4
X B2U(X) B2T(X)
0000 0 0
0001 1 1 Equivalence
0010 2 2
0011 3 3 Both encoding schemes (B2U
0100 4 4 and B2T ) produce the same bit
0101 5 5 patterns for nonnegative values
0110 6 6
0111 7 7 Uniqueness
1000 8 –8 Every bit pattern produced by
1001 9 –7
these encoding schemes (B2U
1010 10 –6
1011 11 –5 and B2T ) represents a unique
1100 12 –4 (and exact) integer value
13
1101 13 –3 Each representable integer has
1110 14 –2
unique bit pattern
1111 15 –1
Converting unsigned signed – Method 1
of same size (same data type)
TMax + 1 Unsigned
TMax TMax Range
+16
Signed 0 0
(Two’s Complement) –1
Range –2
16
TMin
Converting unsigned (or signed) of different
sizes (i.e., data types) in C
X •••
1. Small data type -> larger data type
Sign extension (may require padding)
Unsigned: zero extension X 0 0 • • • 0 0 •••
Sign bit
Signed: sign bit extension X •••
X •••
Let’s try:
Going from a data type that has a width of 5 bits (w = 5) to a data
type that has a width of 3 bits (w = 3)
Unsigned: X = 27 => 110112 w = 5
new X = <= w=3
Signed: X = -15 => 100012 w = 5 X = -1 => 111112 w = 5
18 new X = <= w=3 new X = <= w=3
Summary
Interpretation of bit pattern B into either unsigned value U or signed value T
B2U(X) and U2B(X) encoding schemes (conversion)
B2T(X) and T2B(X) encoding schemes (conversion)
Signed value expressed as two’s complement => T