Uploaded by Saad Mustafa

COSC201 Lecture04 DataRepresentation Part3 Spring2023

advertisement
COSC 201 Computer Systems Organization
Spring 2023
Lecture 4
Data Representation
Bits & Bytes (Part 3)
1
Last Lecture
§ Hexadecimal Number System
§ C Integer Types
§ Logic Functions
§ Bitwise Operations
§ Arithmetic Operations
2
Today’s Lecture
§ Signed Number Representation
§ Two’s Complement
§ Casting and Conversion
§ Signed Number Arithmetic
3
Subtraction in a 4-bit computer
9
1
-1 - 0
8
1
8
No borrowing
With binary borrowing
Borrow 1 from Position 2.
Position 2 now has zero.
Position 1 has now 2 (Yellow
cell). 2 in the yellow cell minus
1 in the blue cell gives 1 in the
green cell.
4
Position 2
0
0
0
4
0
2
0
-1 - 0
1
0
8
0
0
0
2
1
1
0
1
0
0
0
0
4
Decimal borrowing
0
9
9
10
0
9
10
0
0
10
0
0
1
0
0
0
0
0
0
1
0
9
9
9
1000
100
10
1
1000
-1
-
999
0
1
0
0
2
2
0
1
1
1
0
1
10
1
-7 - 0
3
0
8
1
0
0
1
0
4
2
0
1
1
1
2
2nd borrowing
2 1st borrowing
0
1
1
1
With more complex binary borrowing
Subtraction in 4-bit Computer Using the Number Circle
4-bit Number Wheel
No overflow
Overflow condition
Crossing of the 0/15 boundary
5
Subtraction moves counter-clockwise
Signed Number Representation
Signed Magnitude
Do you see a problem?
6
Signed Number Representation: Signed Magnitude
The Problem is that we now have two zeros!!
7
Signed Number Representation: Two’s Complement
8
Signed Number Representation: Two’s Complement
9
Signed Number Representation: Two’s Complement
The zero problem has disappeared!
10
Signed Integers in C
Unsigned
B2U(X )
=
w -1
å xi × 2
Two’s Complement
i
B2T (X )
=
- xw -1 × 2
i=0
å xi × 2
i
Sign
Bit
§ C “short int” data type is 2 bytes long
x
y
+
w-2
i=0
short int x = 15213;
short int y = -15213;
Decimal
15213
-15213
w -1
Hex
3B 6D
C4 93
Binary
00111011 01101101
11000100 10010011
§ Sign Bit
§ For 2’s complement, most significant bit indicates sign
§ 0 for nonnegative
§ 1 for negative
11
B2U: Binary Pattern to Unsigned
B2T: Binary Pattern to 2’s Complement
Two-complement Encoding Example: Positional Arithmetic
x =
y =
B2U(X ) =
w-1
å xi ×2
i=0
12
i
15213: 00111011 01101101
-15213: 11000100 10010011
Weight
1
2
4
8
16
32
64
128
256
512
1024
2048
4096
8192
16384
-32768
Sum
15213
1
0
1
1
0
1
1
0
1
1
0
1
1
1
0
0
w-2
1
0
4
8
0
32
64
0
256
512
0
2048
4096
8192
0
0
15213
w-1
i
-15213
B2T(X) = -xw-1 ×2 + å xi ×2
1
1
i=0
1
2
0
0
0
0
1
16
0
0
0
0
1
128
0
0
0
0
1
1024
0
0
0
0
0
0
1
16384
1
-32768
-15213
Numerical Ranges
§ Two’s
§ Unsigned Values (U for Unsigned)
§ UMin = 0
000…0
§ UMax = 2w – 1
111…1
B2U(X )
=
w -1
å xi × 2
i
i=0
Values for W = 16
UMax
TMax
TMin
-1
0
13
Decimal
65535
32767
-32768
-1
0
Complement Values (T
for Two’s complement)
§ TMin = –2w–1
100…0
§ TMax
= 2w–1 – 1
011…1
§ Other Values
§ Minus 1
i
111…1 B2T (X ) = - xw -1 × 2 w -1 + w-2
å xi × 2
i=0
Hex
FF FF
7F FF
80 00
FF FF
00 00
Binary
11111111 11111111
01111111 11111111
10000000 00000000
11111111 11111111
00000000 00000000
Values for Different Word Sizes
W
UMax
TMax
TMin
8
255
127
-128
16
65,535
32,767
-32,768
32
4,294,967,295
2,147,483,647
-2,147,483,648
¢
§ Observations
§ |TMin |
= TMax + 1
§ Asymmetric range
§ UMax = 2 * TMax + 1
14
64
18,446,744,073,709,551,615
9,223,372,036,854,775,807
-9,223,372,036,854,775,808
C Programming
§ #include <limits.h>
§ Declares constants, e.g.,
§ ULONG_MAX
§ LONG_MAX
§ LONG_MIN
§ Values platform specific
Unsigned & Signed Numeric Values
X
0000
0001
0010
0011
0100
0101
0110
0111
1000
1001
1010
1011
1100
1101
1110
1111
15
B2U(X)
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
B2T(X)
0
1
2
3
4
5
6
7
–8
–7
–6
–5
–4
–3
–2
–1
§ Equivalence
§ Same encodings for nonnegative
values
§ Uniqueness
§ Every bit pattern represents unique
integer value
§ Each representable integer has
unique bit encoding
§ Invertible Mappings
§ U2B(x) = B2U-1(x)
◦ Bit pattern for unsigned integer
§ T2B(x) = B2T-1(x)
◦ Bit pattern for two’s comp integer
Mapping Between Signed & Unsigned
Two’s Complement
x
T2U
T2B
X
B2U
Unsigned
ux
Maintain Same Bit Pattern
Unsigned
ux
U2T
U2B
X
B2T
Two’s Complement
x
Maintain Same Bit Pattern
§ Mappings between unsigned and two’s complement numbers:
Keep bit representations and reinterpret
16
Mapping Signed « Unsigned
17
Bits
Signed
Unsigned
0000
0
0
0001
1
1
0010
2
2
0011
3
0100
4
0101
5
5
0110
6
6
0111
7
7
1000
-8
8
1001
-7
9
1010
-6
10
1011
-5
1100
-4
12
1101
-3
13
1110
-2
14
1111
-1
15
=
+/- 16
3
4
11
Relation between Signed & Unsigned
Two’s Complement
Unsigned
T2U
x
T2B
X
ux
B2U
Maintain Same Bit Pattern
w–1
ux
+ + +
•••
0
+ + +
x
•••
+ + +
- + +
Large negative weight
becomes
Large positive weight
18
Conversion Visualized
§ 2’s Comp. ® Unsigned
§ Ordering Inversion
§ Negative ® Big Positive
UMax
UMax – 1
TMax
2’s Complement
Range
19
0
–1
–2
TMin
TMax + 1
TMax
0
Unsigned
Range
Signed vs. Unsigned in C
§ Constants
§ By default are considered to be signed integers
§ Unsigned if have “U” as suffix: 0U, 4294967259U
§ Casting (changing the data type of the variable)
§ Explicit casting between signed & unsigned same as U2T and T2U
int tx, ty;
unsigned ux, uy;
tx = (int) ux;
uy = (unsigned) ty;
§ Implicit casting also occurs via assignments and procedure calls
tx = ux;
uy = ty;
20
Casting Rules
§ Expression Evaluation:
§ If there is a mix of unsigned and signed in single expression,
signed values implicitly cast to unsigned
§ Including comparison operations <, >, ==, <=, >=
21
Unsigned Addition
Operands: w bits
True Sum: w+1 bits
Discard Carry: w bits
u
+ v
u+v
UAddw(u , v)
§ Standard Addition Function
§ Ignores carry output
§ Implements Modular Arithmetic (clock arithmetic)
s = UAddw(u , v)= (u + v) mod 2w
Example w = 4; 2w = 16; 7+10 = 17 = (1 mod 16)
Remainder of dividing the correct mathematical result by 16
22
•••
•••
•••
•••
Two’s Complement Addition
Operands: w bits
True Sum: w+1 bits
Discard Carry: w bits
u
+ v
u+v
TAddw(u , v)
•••
•••
•••
•••
§ TAdd and UAdd have Identical Bit-Level Behavior
§ Signed vs. unsigned addition in C:
int s, t, u, v; /* signed numbers */
s = (int) ((unsigned) u + (unsigned) v); /* cast unsigned addition */
t = u + v; /* signed addition */
§ Will give s == t
23
Mapping Signed « Unsigned
The add
circuit
operates
on the
same
bits
24
Bits
Signed
Unsigned
0000
0
0
0001
1
1
0010
2
2
0011
3
3
0100
4
4
0101
5
0110
6
0111
7
1000
-8
8
1001
-7
9
1010
-6
10
1011
-5
11
1100
-4
12
1101
-3
13
1110
-2
14
1111
-1
15
T2U
U2T
5
6
7
Signed Arithmetic and Overflow Conditions
25
TAdd Overflow
§ Functionality
Mathematical Sum (w + 1 bits)
0 111…1
§ Math sum requires w+1 bits
§ Discard MSB
§ Treat remaining w bits as 2’s
complement integer
26
2w–1
PosOver
Computer sum(w bits)
0 100…0
2w –1–1
011…1
0 000…0
0
000…0
1 011…1
–2w –1
1 000…0
–2w
100…0
NegOver
2w –1–1
–2w –1
Summary
§ Signed Number Representation
§ Two’s Complement
§ Casting and Conversion
§ Signed Number Arithmetic
27
Download