Representing Integers: Unsigned and Signed Encodings in Computer Systems, Lab Reports of Computer Science

This document from cs 4400 lecture 3 discusses the representation of integers in computer systems, focusing on unsigned and signed encodings. It covers the concepts of two's complement, bitwise operations, and integer arithmetic. The document also includes exercises and examples.

Typology: Lab Reports

Pre 2010

Uploaded on 08/30/2009

koofers-user-18q
koofers-user-18q ๐Ÿ‡บ๐Ÿ‡ธ

10 documents

1 / 21

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS 4400
Computer Systems
August 31, 2009 โ€” LECTURE 3
Representing integers
Integer arithmetic
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15

Partial preview of the text

Download Representing Integers: Unsigned and Signed Encodings in Computer Systems and more Lab Reports Computer Science in PDF only on Docsity!

CS 4400

Computer Systems

August 31, 2009 โ€” LECTURE 3

Representing integers

Integer arithmetic

Encoding Integers

โ— Two different ways bits can be used to encode integers: โ— (^) unsigned โ€“ only nonnegative numbers represented โ— (^) signed โ€“ negative, zero, and positive values represented โ— (^) Both encodings represent a finite range of integers. type declaration (C) min max char -128 127 unsigned char 0 255 short -32768 32767 unsigned short 0 65535 int -2147483648 2147483647 unsigned int 0 4294967295 generated using ranges.c in lab

Signed Integers

โ— The most common computer representation of signed integers is two's complement. B2T w ( x ) = โ€“ x w -

w -

  • โˆ‘ x i

i โ— Sign bit โ€”the MSB, 1: negative and 0: nonnegative. โ— (^) TMin w

= [10...00] = โ€“

w - โ— (^) TMax w

= [01...11] = 2

w -

  • 1 โ— (^) B2T w

w โ†’ {โ€“ w - , ..., 2 w -

  • 1} โ— (^) Is B2T w a bijection? w- i =

Exercises : Encoding Integers

Let w = 4. Hex Binary B2U w B2T w A [1010] 2 3

  • 2 1 = 10 โ€“ 2 3
  • 2 1 = โ€“ 6 B C D E F

Signed-Unsigned Conversions

โ— (^) Since both B2U w and B2T w are bijections, they have well-defined inverses, U2B w and T2B w

โ— (^) Consider U2T w ( x ) = B2T w

(U2B

w ( x )). โ— (^) Takes number between 0 and 2 w -1, yields number between โ€“ 2 w - and 2 w- โ€“1. โ— (^) Both numbers have identical bit representations. โ— (^) Conversely, consider T2U w ( x ) = B2U w

(T2B

w ( x )). โ— (^) Example (8-bit): [10101010], 170 unsigned, -86 signed โ— How do these functions affect signed and unsigned in C?

Unsigned and Signed in C

โ— In C, values are signed unless indicated with type (e.g., unsigned short) or U in constant (e.g., 1234U). โ— (^) Use conversion codes %d, %u to print signed and unsigned decimal values, respectively. โ— When signed and unsigned values are mixed in expressions, the signed values are promoted to unsigned. int x = -1; unsigned ux = (unsigned) x; // ux is UMax_w

Expanding Bit Representations

โ— (^) A common operation is to convert between integers of different word sizes, retaining the same numeric value. โ— (^) To convert from smaller word size to larger: โ— (^) for unsigned, simply add leading 0s โ€“ zero extension โ— (^) for signed, add leading Xs such that X=MSB โ€“ sign extension โ— Example: short sx = 12345; // 0x short sy = -12345; // 0xCFC int x = sx; // 0x int y = sy; // 0xFFFFCFC

Truncating Bit Representations

โ— To convert from larger word size to smaller ( w -bit to k -bit, where w > k ): โ— drop high-order w - k bits โ€“ truncation โ— Truncation of a number can alter its value, a form of overflow. โ— For unsigned x , truncation to k -bit equivalent to x mod 2 k . For signed x? short x = (int) 12345; // 0x00003039, x is 12345 short y = (int) 53191; // 0x0000CFC7, y is -

Unsigned Addition

โ— Consider w -bit unsigned values x and y , 0 โ‰ค x , y โ‰ค 2 w -1. โ— (^) Representing the sum could require w +1 bits, 0 โ‰ค x + y โ‰ค 2 w +^1 - โ— In math, we cannot place any bound on the word size required to fully represent the results of arithmetic ops. โ— Unsigned arithmetic is a form of modular arithmetic. โ— (^) Unsigned addition is equivalent to ( x + y ) mod 2 w. โ— (^) unsigned_add( x , y ) = x + y , if x + y < 2 w โ— (^) unsigned_add( x , y ) = x + y - 2 w, if 2 w^ โ‰ค x + y < 2 w +^1 โ— Example: unsigned short x =^ 65530 + 6;^ //^ x is 0

Overflow

โ— (^) An arithmetic operation is said to overflow when the full integer result cannot fit within the limits of the data type. โ— (^) In C, overflow is not signaled as an error. โ— (^) Some types of overflow may be signaled with a warning. โ— We know that overflow has occurred during unsigned integer addition s = x + y , if s < x (equivalently, if s < y ). โ— Example: unsigned x = ~0; unsigned y = 2; unsigned s = x + y; if(s < x) { ... } // overflow

Cases of Overflow

โ— Negative overflow โ€”if - w โ‰ค x + y < - w - . โ— (^) both x and y must be negative โ— (^) a nonnegative integer is the result (counter to usual math) โ— (^) twoscomp_add( x , y ) = x + y + 2 w โ— No overflow โ€”if - w - โ‰ค x + y < 2 w - . โ— (^) twoscomp_add( x , y ) = x + y โ— Positive overflow โ€”if 2 w - โ‰ค x + y < 2 w . โ— (^) both x and y must be positive โ— (^) a negative integer is the result (counter to usual math) โ— (^) twoscomp_add( x , y ) = x + y - 2 w

Unsigned Multiplication

โ— (^) Consider w -bit unsigned values x and y , 0 โ‰ค x , y โ‰ค 2 w -1. โ— (^) The product could require 2 w bits, 0 โ‰ค x * y โ‰ค (2 w -1)^2 โ— We must truncate the result to w bits. โ— (^) In C, the low-order w bits are retained as the result. โ— (^) Equivalent to computing the product mod 2 w. โ— Example : unsigned short x = 1 << 15; // 32768 x *= 3; // 32768

Multiplication by Powers of Two

โ— Integer multiplication is slow (โ‰ฅ 12 cycles) compared to other integer operations. โ— (^) addition, subtraction, bit-level ops, and shiftsโ€”1 cycle each โ— An important (compiler) optimization is to replace multiplications by constant factors with shifts and adds. โ— Let x be an integer. For any k โ‰ฅ 0, x * 2 k is equivalent to adding k 0's to the right of the bit representation of x. โ— Example : (^) unsigned int = 11 << 3; // 88 int = -11 << 3; // -

Division by Powers of Two

โ— Integer division is also slow (โ‰ฅ 30 cycles). โ— Let x be an unsigned integer. For any k โ‰ฅ 0, x / 2 k is equivalent to adding k 0's to the left of the bit rep for x. โ— (^) logical shift โ— (^) Let x be an signed integer. For any k โ‰ฅ 0, x / 2 k^ is equivalent to adding k b 's to the left of the bit rep for x. โ— (^) b is the value of x 's MSB, arithmetic shift โ— (^) What if x < 0? โ— Example: int x = 55 >> 3; // 6 int y = -55 >> 3; // -7 (should be -6)