CS 213 Class 03: Numeric Encodings and Integer Representations, Slides of Software Engineering

This document from carnegie mellon university's cs 213 course covers various topics related to numeric encodings, including unsigned and two's complement, programming implications, and integer representations. It also discusses the differences between signed and unsigned integers in c and the concept of sign extension.

Typology: Slides

2011/2012

Uploaded on 04/16/2012

shyrman
shyrman 🇺🇸

4.2

(6)

239 documents

1 / 20

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
15-213
“The course that gives CMU its Zip!”
Topics
Numeric Encodings
Unsigned & Two’s complement
Programming Implications
C promotion rules
CS 213 F’98
class03.ppt
Integer Representations
Sept. 1, 1998
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14

Partial preview of the text

Download CS 213 Class 03: Numeric Encodings and Integer Representations and more Slides Software Engineering in PDF only on Docsity!

“The course that gives CMU its Zip!”

Topics

  • Numeric Encodings
    • Unsigned & Two’s complement
  • Programming Implications
    • C promotion rules

class03.ppt CS 213 F’

Integer Representations

Sept. 1, 1998

Notation

W : Number of Bits in “Word”

C Data Type Sun, etc. Alpha long int 32 64 int 32 32 short 16 16 char 8 8

Integers

  • Lower case
  • E.g., x , y , z

Bit Vectors

  • Upper Case
  • E.g., X , Y , Z
  • Write individual bits as integers with value 0 or 1
  • E.g., X = xw –1 , xw –2 ,… x 0
    • Most significant bit on left

Encoding Example (Cont.)

Weight 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 - Sum 15213 -

x = 15213: 00111011 01101101 y = -15213: 11000100 10010011

Numeric Ranges

Unsigned Values

  • UMin = 0 000…
  • UMax = 2 w^ – 1 111…

Two’s Complement Values

  • TMin = –2 w – 100…
  • TMax = 2 w –1^ – 1 011…

Other Values

  • Minus 1 111…

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

Unsigned & Signed Numeric Values

Example Values

• W = 4

Equivalence

  • Same encodings for nonnegative values

Uniqueness

  • Every bit pattern represents unique integer value
  • Each representable integer has unique bit encoding

⇒ 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

X B2U( X ) B2T( X )

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

Relation Between Signed & Unsigned

  • uy = y + 2 * 32768 = y + 65536

Weight -15213 50323 1 1 1 1 1 2 1 2 1 2 4 0 0 0 0 8 0 0 0 0 16 1 16 1 16 32 0 0 0 0 64 0 0 0 0 128 1 128 1 128 256 0 0 0 0 512 0 0 0 0 1024 1 1024 1 1024 2048 0 0 0 0 4096 0 0 0 0 8192 0 0 0 0 16384 1 16384 1 16384 32768 1 -32768 1 32768 Sum -15213^50323

From Two’s Complement to Unsigned

  • T2U( x ) = B2U(T2B( x )) = x + xw –1 2 w
  • What you get in C: int t2u(int x) { return (unsigned) x; }

+ 16

X B2U( X ) B2T( X )

T2U( x )

0000

0001 0010

0011 0100

0101 0110

0111 1000

1001 1010

1011 1100

1101 1110

1111

2 w

2 w

–2 w –1^0 2 w

0

Identity

Signed vs. Unsigned in C

Constants

  • By default are considered to be signed integers
  • Unsigned if have “U” as suffix 0U, 4294967259U

Casting

  • Explicit casting between signed & unsigned same as U2T and T2U int tx, ty; unsigned ux, uy; tx = (int) ux; uy = (unsigned) ty;
  • Implicit casting also occurs via assignments and procedure calls tx = ux; uy = ty;

Casting Surprises

Expression Evaluation

  • If mix unsigned and signed in single expression, signed values implicitly cast to unsigned
  • Including comparison operations <, >, ==, <=, >=
  • Examples for W = 32

Constant 1 Constant 2 Relation Evaluation

0 0U == unsigned -1 0 < signed -1 0U > unsigned 2147483647 -2147483648 > signed 2147483647U -2147483648 < unsigned -1 -2 > signed (unsigned) -1 -2 > unsigned 2147483647 2147483648U < unsigned 2147483647 (int) 2147483648U > signed

Sign Extension

Task:

  • Given w -bit signed integer x
  • Convert it to w + k -bit integer with same value

Rule:

  • Make k copies of sign bit:
  • X= xw –1 ,…, xw –1 , xw –1 , xw –2 ,…, x 0

k copies of MSB

X • • •

X ′ • • • • • •

  • • •

w

k w

Sign Extension Example

  • Converting from smaller to larger integer data type
  • C automatically performs sign extension

Decimal Hex Binary x 15213 3B 6D 00111011 01101101

ix 15213 00 00 C4 92 00000000 00000000 00111011 01101101

y (^) -15213 C4 93 11000100 10010011

iy (^) -15213 FF FF C4 93 11111111 11111111 11000100 10010011

short int x = 15213; int ix = (int) x; short int y = -15213; int iy = (int) y;

Casting Order Dependencies

iux = 15213: 00000000 00000000 00111011 01101101

iuy = 50323: 00000000 00000000 11000100 10010011

uix = 15213: 00000000 00000000 00111011 01101101 uiy = 4294952083: 11111111 11111111 11000100 10010011

uuy = 4294952083: 11111111 11111111 11000100 10010011

short int x = 15213; short int y = -15213; unsigned iux = (unsigned)(unsigned short) x; unsigned iuy = (unsigned)(unsigned short) y; unsigned uix = (unsigned) (int) x; unsigned uiy = (unsigned) (int) y; unsigned uuy = y;

y

unsigned Fill 0’s

Sign Extend unsigned

iuy

uiy

(unsigned short) (unsigned)

(int) (unsigned)

Why Should I Use Unsigned?

Don’t Use Just Because Number Nonzero

  • C compiler on Alpha generates less efficient code unsigned i; for (i = 1; i < cnt; i++) a[i] += a[i-1];
  • Easy to make mistakes for (i = cnt-2; i >= 0; i--) a[i] += a[i+1];

Do Use When Performing Modular Arithmetic

  • Multiprecision arithmetic
  • Other esoteric stuff

Do Use When Need Extra Bit’s Worth of Range

  • Working right up to limit of word size