CMPT 295

Unit - Data Representation

Lecture 3 – Representing integral numbers in memory - unsigned and signed

Last Lecture

NOTE: C logical operators and C bitwise (bit-level) operators behave differently! Watch out for && versus &, || versus |, …

Today’s Menu

Warm up exercise!

As a warm up exercise, fill in the blanks!

Unsigned integral numbers

Remember:

Address M[]
size-1  
 
0x0003 01000010
0x0002 01101001
0x0001 01110100
0x0000 01110011

B2U(X)=i=0w1Xi×2i \it \text{B2U}(X) = \sum_{i=0}^{w-1} X_{i} \times 2^i

Example: 0×27+1×26+1×25+0×24+1×23+0×22+1×20=0 \times 2^7 + 1 \times 2^6 + 1 \times 2^5 + 0 \times 2^4 + 1 \times 2^3 + 0 \times 2^2 + 1 \times 2^0 =

B2U(X) Conversion (Encoding scheme)

Decimal:

did_{i} 10i\text{10}^{i}
di1d_{i-1} 10i1\text{10}_{i-1}
d2d_{2} 100
d1d_{1} 10
d0d_{0} 1

Binary:

bib_{i} 2i2^{i}
bi1b_{i-1} 2i12^{i-1}
b2b_{2} 4
b1b_{1} 2
b0b_{0} 1

Remember: B2U(X)=i=0w1Xi×2i \it \text{B2U}(X) = \sum_{i=0}^{w-1} X_{i} \times 2^i

Example: 24610=2×102+4×101+6×100\text{246}_{10} = 2 \times \text{10}^{2} + 4 \times \text{10}^{1} + 6 \times \text{10}^{0}

Range of possible values?

Examples of “Show your work”

U2B(X) Conversion (into 8-bit binary # => w = 8)

Method 1 - Using subtraction: subtracting decreasing power of 2 until reach 0:

Starting number: 246

Method 2 - Using division: dividing by 2 until reach 0

Start with 246

U2B(X) Conversion – A few tricks

Signed integral numbers

Remember:

Address M[]
size-1  
 
0x0003 01000010
0x0002 01101001
0x0001 01110100
0x0000 01110011

B2T(X)=xw1×2w1+i=0w2xi×2i \it {B2T}(X) = -x_{w-1} \times 2^{w-1} + \sum_{i=0}^{w-2} x_{i} \times 2^{i}

Example: 1×27+1×26+1×25+1×24+0×23+1×22+0×21+0×20=?-1 \times 2^{7} + 1 \times 2^{6} + 1 \times 2^{5} + 1 \times 2^{4} + 0 \times 2^{3} + 1 \times 2^{2} + 0 \times 2^{1} + 0 \times 2^{0} = ?

Examples of “Show your work”

T2B(X) Conversion -> Two’s Complement

Annotation: w = 8

Method 1: If X < 0, (~(U2B(|X|)))+1

If X = -14 (and 8 bit binary #s)

  1. X=>14=\lvert X\rvert => \lvert -14 \vert =
  2. U2B(14)\text{U2B}(14) =>
  3. (first symbol is a tilde) (000011102)\sim(\text{00001110}_{2}) =>
  4. (111100012)+1(\text{11110001}_{2})+1 =>

Binary addition:

  11110001
+ 00000001
= ????????

Method 2: If X = -14 (and 8 bit binary #s)

  1. X+2w=>14+X + 2^{w} => -14 +
  2. U2B(242)\text{U2B}(242) =>

Using subtraction:

  1. 242128=114242 - 128 = 114 -> 1×271 \times 2^{7}
  2. 11464=50114 - 64 = 50 -> 1×261 \times 2^{6}
  3. 5032=1850 – 32 = 18 -> 1×251 \times 2^{5}
  4. 1816=218 – 16 = 2 -> 1×241 \times 2^{4}
  5. 28=nop!2 – 8 = \text{nop!} -> 0×230 \times 2^{3}
  6. 24=nop!2 – 4 = \text{nop!} -> 0×220 \times 2^{2}
  7. 22=02 – 2 = 0 -> 1×211 \times 2^{1}
  8. 01=nop!0 – 1 = \text{nop!} -> 0×200 \times 2^{0}

Properties of unsigned & signed conversions

Annotation: w = 4

X B2U(X) B2T(X)
0000 0 0
0001 1 1
0010 2 2
0011 3 3
0100 4 4
0101 5 5
0110 6 6
0111 7 7
1000 8 -8
1001 9 -7
1010 10 -6
1011 11 -5
1100 12 -4
1101 13 -3
1110 14 -2
1111 15 -1

Converting between signed & unsigned of same size (same data type)

Converting signed to unsigned (and back) with w=4w = 4

Signed Bits Unsigned Note
0 0000 0 All rows from 0-7 inclusive can be converted from signed to unsigned with T2U(X), and unsigned to signed with U2T(X).
1 0001 1  
2 0010 2  
3 0011 3  
4 0100 4  
5 0101 5  
6 0110 6  
7 0111 7  
-8 1000 8 All rows from here to 15 inclusive can be converted to the other like so: T2U(signed + 16/242^{4}) -> unsigned, U2T(unsigned - 16/242^{4}) -> signed.
-7 1001 9  
-6 1010 10  
-5 1011 11  
-4 1100 12  
-3 1101 13  
-2 1110 14  
-1 1111 15  

Visualizing the relationship between signed & unsigned

If w=4,24=16w = 4, 2^{4} = 16

Sign extension

Truncation

Summary

Next Lecture