













Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
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
1 / 21
This page cannot be seen from the preview
Don't miss anything!














โ 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
โ The most common computer representation of signed integers is two's complement. B2T w ( x ) = โ x w -
w -
i โ Sign bit โthe MSB, 1: negative and 0: nonnegative. โ (^) TMin w
w - โ (^) TMax w
w -
w โ {โ w - , ..., 2 w -
Let w = 4. Hex Binary B2U w B2T w A [1010] 2 3
โ (^) 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
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
w ( x )). โ (^) Example (8-bit): [10101010], 170 unsigned, -86 signed โ How do these functions affect signed and unsigned 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
โ (^) 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
โ 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 -
โ 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
โ (^) 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
โ 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
โ (^) 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
โ 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; // -
โ 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)