cs33-integers

# cs33-integers - Integers Chapter 2 of B&O Some notes...

This preview shows pages 1–9. Sign up to view the full content.

Some notes adopted from Bryant and O’Hallaron Integers Chapter 2 of B&O

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Encoding Integers short int x = 15213; short int y = -15213; – C short 2 bytes long • Sign Bit – For 2’s complement, most significant bit indicates sign • 0 for nonnegative • 1 for negative B 2 T ( X ) = - x w - 1 2 w - 1 + x i 2 i i = 0 w - 2 B 2 U ( X ) = x i 2 i i = 0 w - 1 Unsigned Two’s Complement Sign Bit Decimal Hex Binary x 15213 3B 6D 00111011 01101101 y -15213 C4 93 11000100 10010011
Encoding Example (Cont.) x = 15213: 00111011 01101101 y = -15213: 11000100 10010011 Weight 15213 -15213 1 1 1 1 1 2 0 0 1 2 4 1 4 0 0 8 1 8 0 0 16 0 0 1 16 32 1 32 0 0 64 1 64 0 0 128 0 0 1 128 256 1 256 0 0 512 1 512 0 0 1024 0 0 1 1024 2048 1 2048 0 0 4096 1 4096 0 0 8192 1 8192 0 0 16384 0 0 1 16384 -32768 0 0 1 -32768 Sum 15213 -15213 X B2T( X ) B2U( X ) 0000 0 0001 1 0010 2 0011 3 0100 4 0101 5 0110 6 0111 7 –8 8 –7 9 –6 10 –5 11 –4 12 –3 13 –2 14 –1 15 1000 1001 1010 1011 1100 1101 1110 1111 0 1 2 3 4 5 6 7

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Numeric Ranges • Unsigned Values UMin = 0 000…0 UMax = 2 w – 1 111…1 • Two’s Complement Values TMin = –2 w –1 100…0 TMax = 2 w –1 – 1 011…1 • Other Values Minus 1 111…1 Decimal Hex Binary UMax 65535 FF FF 11111111 11111111 TMax 32767 7F FF 01111111 11111111 TMin -32768 80 00 10000000 00000000 -1 -1 FF FF 11111111 11111111 0 0 00 00 00000000 00000000 Values for W = 16
Values for Different Word Sizes • Observations | TMin | = TMax + 1 Asymmetric range UMax = 2 * TMax + 1 • C Programming #include <limits.h> – Declares constants, e.g., ULONG_MAX LONG_MAX LONG_MIN – Values platform-specific W 8 16 32 64 UMax 255 65,535 4,294,967,295 18,446,744,073,709,551,615 TMax 127 32,767 2,147,483,647 9,223,372,036,854,775,807 TMin -128 -32,768 -2,147,483,648 -9,223,372,036,854,775,808

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
short int x = 15213; unsigned short int ux = (unsigned short) x; short int y = -15213; unsigned short int uy = (unsigned short) y; Casting Signed to Unsigned • C Allows Conversions from Signed to Unsigned • Resulting Value – No change in bit representation – Nonnegative values unchanged ux = 15213 – Negative values change into (large) positive values uy = 50323
T2U T2B B2U Two’s Complement Unsigned Maintain Same Bit Pattern x ux X + + + + + + • • • - + + + + + • • • ux x - w –1 0 +2 w –1 – –2 w –1 = 2*2 w –1 = 2 w ux = x x 0 x + 2 w x < 0 Relation between Signed & Unsigned

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
TMax TMin –1 –2 0 UMax UMax – 1 TMax TMax + 1 2’s Comp. Range
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 05/11/2011 for the course CS 33 taught by Professor Rohr during the Spring '08 term at UCLA.

### Page1 / 32

cs33-integers - Integers Chapter 2 of B&O Some notes...

This preview shows document pages 1 - 9. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online