Top Banner
1 Integer Representations
29

1 Integer Representations. 2 Outline Encodings –Unsigned and two’s complement Conversions –Signed vs. unsigned –Long vs. short Suggested reading –Chap.

Dec 30, 2015

Download

Documents

Jesse Marshall
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 1 Integer Representations. 2 Outline Encodings –Unsigned and two’s complement Conversions –Signed vs. unsigned –Long vs. short Suggested reading –Chap.

1

Integer Representations

Page 2: 1 Integer Representations. 2 Outline Encodings –Unsigned and two’s complement Conversions –Signed vs. unsigned –Long vs. short Suggested reading –Chap.

2

Outline

• Encodings– Unsigned and two’s complement

• Conversions– Signed vs. unsigned– Long vs. short

• Suggested reading

– Chap 2.2

Page 3: 1 Integer Representations. 2 Outline Encodings –Unsigned and two’s complement Conversions –Signed vs. unsigned –Long vs. short Suggested reading –Chap.

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

charunsigned char

-1270

127255

-1280

127255

short [int]unsigned short

-32,7670

32,76765,535

-32,7680

32,76765,535

intunsigned [int]

-32,7670

32,76765,535

-

2,147,483,648

0

2,147,483,64

7

4,294,967,29

5

long [int]unsigned long

-

2,147,483,647

0

2,147,483,647

0

-

2,147,483,648

0

2,147,483,64

7

4,294,967,29

5

Page 4: 1 Integer Representations. 2 Outline Encodings –Unsigned and two’s complement Conversions –Signed vs. unsigned –Long vs. short Suggested reading –Chap.

4

Two’s Complement

• Binary– Bit vector [xw-1,xw-2,xw-3,x0]

• Using 2’s complement to represent integer

B2T (X ) xw 1 2w 1 xi 2i

i0

w 2

B2U(X ) xi 2i

i0

w 1

Unsigned Two’s Complement

SignBitP52 Eq. (2.1) P52 Eq. (2.2)

Page 5: 1 Integer Representations. 2 Outline Encodings –Unsigned and two’s complement Conversions –Signed vs. unsigned –Long vs. short Suggested reading –Chap.

5

From Two’s Complement to Binary

• If nonnegative– Nothing changes

• If negative

2

0

1 122w

i

iw

2

0

2

0

1 12)1(22w

i

ii

w

i

ii

w xx

Page 6: 1 Integer Representations. 2 Outline Encodings –Unsigned and two’s complement Conversions –Signed vs. unsigned –Long vs. short Suggested reading –Chap.

6

Two’s Complement

• Two’s Complement-5 0101 (raw binary)

1010 (after complement)1011 (2’s complement)

Page 7: 1 Integer Representations. 2 Outline Encodings –Unsigned and two’s complement Conversions –Signed vs. unsigned –Long vs. short Suggested reading –Chap.

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

Page 8: 1 Integer Representations. 2 Outline Encodings –Unsigned and two’s complement Conversions –Signed vs. unsigned –Long vs. short Suggested reading –Chap.

8

P55 Figure 2.10

Page 9: 1 Integer Representations. 2 Outline Encodings –Unsigned and two’s complement Conversions –Signed vs. unsigned –Long vs. short Suggested reading –Chap.

9

Numeric Range

• Unsigned Values– Umin=0– Umax=2w-1

• Two’s Complement Values– Tmin = -2w-1

– Tmax = 2w-1-1

Page 10: 1 Integer Representations. 2 Outline Encodings –Unsigned and two’s complement Conversions –Signed vs. unsigned –Long vs. short Suggested reading –Chap.

10

Interesting NumbersP53 Figure 2.9

Page 11: 1 Integer Representations. 2 Outline Encodings –Unsigned and two’s complement Conversions –Signed vs. unsigned –Long vs. short Suggested reading –Chap.

11

Numeric Range

• Relationship– |TMin| = TMax + 1– Umax = 2*TMax + 1– -1 has the same bit representation as Umax,

• a string of all 1s– Numeric value 0 is represented as

• a string of all 0s in both representations

Page 12: 1 Integer Representations. 2 Outline Encodings –Unsigned and two’s complement Conversions –Signed vs. unsigned –Long vs. short Suggested reading –Chap.

12

X B2T(X)B2U(X)

0000 00001 10010 20011 30100 40101 50110 60111 7

–88–79–610–511–412–313–214–115

1000

1001

1010

1011

1100

1101

1110

1111

01234567

Page 13: 1 Integer Representations. 2 Outline Encodings –Unsigned and two’s complement Conversions –Signed vs. unsigned –Long vs. short Suggested reading –Chap.

13

Unsigned & Signed Numeric Values

• Equivalence

– Same encodings for nonnegative values

• Uniqueness

– Every bit pattern represents unique integer

value

– Each representable integer has unique bit

encoding

Page 14: 1 Integer Representations. 2 Outline Encodings –Unsigned and two’s complement Conversions –Signed vs. unsigned –Long vs. short Suggested reading –Chap.

14

Unsigned & Signed Numeric Values

Can Invert Mappings

– U2B(x) = B2U-1(x)

• Bit pattern for unsigned integer

– T2B(x) = B2T-1(x)

• Bit pattern for two’s comp integer

Page 15: 1 Integer Representations. 2 Outline Encodings –Unsigned and two’s complement Conversions –Signed vs. unsigned –Long vs. short Suggested reading –Chap.

15

Alternative representations of signed numbers P54

• One’s Complement:

– The most significant bit has weight -(2w-1-1)

• Sign-Magnitude

– The most significant bit is a sign bit

• that determines whether the remaining

bits should be given negative or positive

weight

Page 16: 1 Integer Representations. 2 Outline Encodings –Unsigned and two’s complement Conversions –Signed vs. unsigned –Long vs. short Suggested reading –Chap.

16

Casting Signed to Unsigned

• C Allows Conversions from Signed to Unsigned

• Resulting Value– No change in bit representation– Nonnegative values unchanged

• ux = 12345– Negative values change into (large) positive

values• uy = 53191

short int x = 12345; unsigned short int ux = (unsigned short) x; short int y = -12345; unsigned short int uy = (unsigned short) y;

Page 17: 1 Integer Representations. 2 Outline Encodings –Unsigned and two’s complement Conversions –Signed vs. unsigned –Long vs. short Suggested reading –Chap.

17

Relation Between 2’s Comp. & Unsigned P57

T2U

T2B B2U

Two’s Complement Unsigned

Maintain Same Bit Pattern

x uxX

+ + + + + +• • •

- + + + + +• • •

ux

x-

w–1 0

+2w–1 – –2w–1 = 2*2w–1 = 2w

ux x x 0

x 2w x 0

P57 Eq. (2.3)

P57 Eq. (2.4)

Page 18: 1 Integer Representations. 2 Outline Encodings –Unsigned and two’s complement Conversions –Signed vs. unsigned –Long vs. short Suggested reading –Chap.

18

Conversion between two Representations

0

TMax

TMin

–1–2

0

UMaxUMax – 1

TMaxTMax + 1

2’s Comp.Range

UnsignedRange

P57 Figure 2.11

Page 19: 1 Integer Representations. 2 Outline Encodings –Unsigned and two’s complement Conversions –Signed vs. unsigned –Long vs. short Suggested reading –Chap.

19

Signed vs. Unsigned in C

• Constants

– By default are considered to be signed

integers

– Unsigned if have “U” as suffix

• 0U, 4294967259U

Page 20: 1 Integer Representations. 2 Outline Encodings –Unsigned and two’s complement Conversions –Signed vs. unsigned –Long vs. short Suggested reading –Chap.

20

Signed vs. Unsigned in C P59

• Casting– Explicit casting between signed & unsigned

same as U2T and T2U• int tx, ty;• unsigned ux, uy;• tx = (int) ux;• uy = (unsigned) ty;

Page 21: 1 Integer Representations. 2 Outline Encodings –Unsigned and two’s complement Conversions –Signed vs. unsigned –Long vs. short Suggested reading –Chap.

21

Signed vs. Unsigned in C

• Casting– Implicit casting also occurs via assignments

and procedure calls• int tx, ty;• unsigned ux, uy;• tx = ux; /* Cast to signed */• uy = ty; /* Cast to unsigned */

Page 22: 1 Integer Representations. 2 Outline Encodings –Unsigned and two’s complement Conversions –Signed vs. unsigned –Long vs. short Suggested reading –Chap.

22

Casting Convention

• Expression Evaluation

– If mix unsigned and signed in single

expression

• signed values implicitly cast to unsigned

– Including comparison operations <, >, ==,

<=, >=

– Examples for W = 32

Page 23: 1 Integer Representations. 2 Outline Encodings –Unsigned and two’s complement Conversions –Signed vs. unsigned –Long vs. short Suggested reading –Chap.

23

Casting Convention P60 Figure 2.13

Constant1 Constant2Relation Evaluation

0 0U == unsigned-1 0 < signed-1 0U > unsigned2147483647 -2147483648 > signed2147483647U -2147483648 < unsigned-1 -2 > signed(unsigned)-1 -2 > unsigned

Page 24: 1 Integer Representations. 2 Outline Encodings –Unsigned and two’s complement Conversions –Signed vs. unsigned –Long vs. short Suggested reading –Chap.

24

Expanding the Bit Representation P61

• Zero extension– Add leading 0s to the representation

• Sign extension– [xw-1,xw-2,xw-3,x0]

• • •X

X • • • • • •

• • •

- • • •X

X - + • • •

w+1

w

Page 25: 1 Integer Representations. 2 Outline Encodings –Unsigned and two’s complement Conversions –Signed vs. unsigned –Long vs. short Suggested reading –Chap.

25

Sign Extension Example

short int x = 12345;

int ix = (int) x;

short int y = -12345;

int iy = (int) y;

Decimal Hex Binaryx 12345 30 39 00110000 00111001ix 12345 00 00 30 39 00000000 00000000 00110000 00111001y -12345 CF C7 11001111 11000111iy -12345 FF FF CF C7 11111111 11111111 11001111 11000111

Page 26: 1 Integer Representations. 2 Outline Encodings –Unsigned and two’s complement Conversions –Signed vs. unsigned –Long vs. short Suggested reading –Chap.

26

Truncating Numbers P63

int x = 53191;

short int sx = -12345;

int y = -12345;

Decimal Hex Binaryx 53191 00 00 CF C7 00000000 00000000 11001111 11000111sx -12345 CF C7 11001111 11000111y -12345 FF FF CF C7 11111111 11111111 11001111 11000111

• • •X

X • • • • • •

Page 27: 1 Integer Representations. 2 Outline Encodings –Unsigned and two’s complement Conversions –Signed vs. unsigned –Long vs. short Suggested reading –Chap.

27

Truncating Numbers

• Unsigned Truncating

• Signed Truncating

]),,([22mod]),,,([2 0101 xxxUBxxxUB kkkk

www

)2mod]),,,([2(2]),,([2 0101k

wwwkkkk xxxUBTUxxxTB

P64 Eq. (2.7)

P64 Eq. (2.8)

Page 28: 1 Integer Representations. 2 Outline Encodings –Unsigned and two’s complement Conversions –Signed vs. unsigned –Long vs. short Suggested reading –Chap.

28

Advice on Signed vs. Unsigned P65 Practice Problem 2.23 [Solution P115]

Nonintuitive Featuresunsigned length ;int i ;for ( i = 0; i <= length – 1; i++)

result += a[i] ;

Page 29: 1 Integer Representations. 2 Outline Encodings –Unsigned and two’s complement Conversions –Signed vs. unsigned –Long vs. short Suggested reading –Chap.

29

Advice on Signed vs. Unsigned

• Collections of bits– Bit vectors

– Masks

• Addresses

• Multiprecision Arithmetic– Numbers are represented by arrays of words