













Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
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)