Integer Representations
1 Integer Representations
Outline Encodings Unsigned and two' s complement Conversions Signed vs unsigned Long vs. short Suggested reading Chap 2.2
2 Outline • Encodings – Unsigned and two’s complement • Conversions – Signed vs. unsigned – Long vs. short • Suggested reading – Chap 2.2
Integral Data Types P51 Figure 2.8 C supports a variety of integral data types Represent a finite range of integers c declaration guaranteed Typical 32-bit minimum maxImum minimum maximum char 127 127 128 127 unsigned char 255 0 255 short [int] -32,767327673276832767 unsigned short 0 65535 0 65535 int -32767327672474836482147483647 unsigned [int] 0 65535 04294967295 long [int] 21474836472147483.647-2147483.6482147483647 unsigned long 0 04294967,295
3 Integral Data Types P51 Figure 2.8 • C supports a variety of integral data types – Represent a finite range of integers C declaration guaranteed Typical 32-bit minimum maximum minimum maximum char unsigned char -127 0 127 255 -128 0 127 255 short [int] unsigned short -32,767 0 32,767 65,535 -32,768 0 32,767 65,535 int unsigned [int] -32,767 0 32,767 65,535 -2,147,483,648 0 2,147,483,647 4,294,967,295 long [int] unsigned long -2,147,483,647 0 2,147,483,647 0 -2,147,483,648 0 2,147,483,647 4,294,967,295
Twos Complement Binary Bit vector [xw-1.xw-2,Xw-3,.xo Using 2's complement to represent integer Unsigned Two's Complement B2(X)=∑x,2B7(X)=-x22∑x2 0 Sign P52Eq.(2.1) P52 Eq (2.2)Bit
4 Two’s Complement • Binary – Bit vector [xw-1 ,xw-2 ,xw-3 ,x0 ] • Using 2’s complement to represent integer B2T(X) = −x w−1 2 w−1 + xi 2 i i=0 w−2 B2U(X) = xi 2 i i=0 w−1 Unsigned Two’s Complement Sign P52 Eq. (2.1) P52 Eq. (2.2) Bit
From Two's Complement to Binary If nonnegative Nothing changes 工 f negative 2 ∑21+1 i=0 2 2 2 ∑x12=∑(1-x1)2+1 i=0
5 From Two’s Complement to Binary • If nonnegative – Nothing changes • If negative − = − = + 2 0 1 2 2 1 w i w i − = − = − − = − + 2 0 2 0 1 2 2 (1 )2 1 w i i i w i i i w x x
Twos Complement Two's Complement 5 o101(raw binary) 1010 (after complement) 1011(2s complement)
6 Two’s Complement • Two’s Complement -5 0101 (raw binary) 1010 (after complement) 1011 (2’s complement)
Twos Complement Encoding Examples Binary /Hexadecimal Representation for 12345 Binary:0011000000111001 Hex: 3039 Binary/Hexadecimal Representation for -12345 Binary:1100111100111 Hex C F C 7
7 Two’s Complement Encoding Examples Binary/Hexadecimal Representation for 12345 Binary: 0011 0000 0011 1001 Hex: 3 0 3 9 Binary/Hexadecimal Representation for –12345 Binary: 1100 1111 1100 0111 Hex: C F C 7
P55 Fiqure 2.10 Weight l2.345 1234553,191 Bit value Bit Value bit Value 1248 1008 1240 1110 1110001111110011 2400 16 16 321 32 64 64 64 128 000000 l28 o1111110 128 2560 256 256 5120 512 512 1.O24 1,O24 1,024 2,0480 2,048 2.048 4.096 4096 8,1921 8192 16.384 16,384116.384 ±32,7680 32,768132,768 Total 12.345 12.345 53.191
8 P55 Figure 2.10
Numeric Range ∪nsgl ned values Umin=o Umax=2W-1 Two's Complement values Tmin =-2W-1 Tmax=2W-1-1
9 Numeric Range • Unsigned Values – Umin=0 – Umax=2w-1 • Two’s Complement Values – Tmin = -2 w-1 – Tmax = 2w-1 -1
Interesting Numbers P53 Figure 2.9 Q )uantity Word Size w 16 32 64 UMEm OxFF OXFFFF OXFFFFFFFF OxFFFFFFFFFFFFFFFF 255655354.294967,295184144.073,709,55615 TMom 0x7F 0x7FFF 0x7FFFFFFF0x7FFFFFFFFFFFFFFF 12732,7672,147483,6479,23,372036,854,75.807 TM0x800x80000x80000000x800000000000 128-32,768-2,147,483.648-9,223,372,036,854,775,808 OxFF OXFFFF OXFFFFFFFF OXFFFFFFFFFFFFFFFF 0x000x00000x000000000x000000000000000
10 Interesting Numbers P53 Figure 2.9