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