class09 (1) - Representing and Manipulating Information...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Representing and Manipulating Information March 2 HW 6 is due 3/9/2010 PP1 is due 3/11/2010 CS 47 Natasha Khuri Shift Operations Bit Pattern char x x << 1 x << 2 x >> 3 x >> 5 00000001 00000010 00000010 00000100 00000100 00000000 00000000 00000000 00000000 Base-10 1 2 4 0 0 CS 47 Natasha Khuri Logical and Arithmetic Shifts A logical shift by k fills the left end with k zeroes: given x = 101000102, x >> 2, 001010002: a logical shift is implemented for unsigned integer types. In Java, this shift is >>> An arithmetic right shift fills the left end with k repetitions of the most significant bit: given x = 101000102, x >> 2, 11101000 for signed types, arithmetic shift is default 2 CS 47 Natasha Khuri C and Java Precedence Be careful when writing complex expressions: 1 << 2 + 3 << 4 is 1 << ( 2 + 3 ) << 4 which is ( 1 << ( 2 + 3 ) ) << 4 CS 47 Natasha Khuri Bitwise Operations Example Colors are often represented by 32-bit values, in which: the low byte (bits 0 through 7) is the value for blue, the next most significant byte is a value for green, then a byte for red, and finally, the high byte is an alpha (transparency) value. So the color dword looks like this in memory: AAAA AAAA RRRR RRRR GGGG GGGG BBBB BBBB Suppose you want to know the value for red. CS 47 Natasha Khuri Extracting Red Bits Remember, ANDing any bit with 0 yields 0, and ANDing any bit with 1 yields the original bit. So let us define a bit mask, which has 0s where we want to erase information, and 1s where we want to keep information. AAAA AAAA RRRR RRRR GGGG GGGG BBBB BBBB & 0000 0000 1111 1111 0000 0000 0000 0000 0000 0000 RRRR RRRR 0000 0000 0000 0000 CS 47 Natasha Khuri Are We Done? 0000 0000 RRRR RRRR 0000 0000 0000 0000 >> 16 0000 0000 0000 0000 0000 0000 RRRR RRRR Then convert to Base-10 CS 47 Natasha Khuri Multiplication Addition, subtraction, bit-wise operations on most machines require only 1 clock cycle Integer multiply instruction is fairly slow: requires 10 or more clock cycles compilers try to optimize the code by replacing multiplications by constant factors with combinations of shifts and addition operations For example, for both, signed and unsigned data types, the C expression x << k is equivalent to x * 2k CS 47 Natasha Khuri Multiplication by Constants Most compilers will attempt to replace cases, where an integer is multiplied by a constant with combinations of shifting, adding and subtracting: suppose, a program contains the expression x * 14 note that 14 = 23 + 22 + 21, so, the compiler can rewrite the multiplication as (x<<3) + (x<<2) + (x<<1) better yet: 14 = 24 21 (x<<4) (x<<1) CS 47 Natasha Khuri Exercise Write a C expression to perform the following multiplications: X * 10 X * 9 X * 6 X * (-6) (- CS 47 Natasha Khuri Division Integer division on most machines is even slower than integer multiplication: requires 30 or more clock cycles Dividing by a power of 2 can be performed using shift operations: integer division always rounds toward zero to divide an unsigned number by 2k, apply logical right shift: x >> k to divide a signed number by 2K: arithmetic right shift is applied when x is negative or when rounding is required, a bias is introduced: )x < 0 ? ( x+1 << k )-1 : x) >> k CS 47 Natasha Khuri Division Division by a power of 2 can be implemented using logical or arithmetic right shifts. This approach does not generalize to division by arbitrary constants. In addition, we will not study multiplication and division in form x * y and x/y covered in CS147 CS 47 Natasha Khuri Data Type Conversions from signed to unsigned: short int x = unsigned short short int y = unsigned short 15213; int ux = (unsigned short) x; -15213; int uy = (unsigned short) y; from unsigned to signed: unsigned short u = 65535; short tu = (short) u; between integers of different word sizes: short sx = -12345; sx; int x = sx; CS 47 Natasha Khuri %d printf("size of short int is %d\n", sizeof(short)); sizeof(short)); %d printf("size of unsigned short int is %d\n", sizeof(unsigned short)); short int x = 15213; unsigned short int ux = (unsigned short) x; %u ux); printf("x = %d, ux = %u\n", x, ux); short int y = -15213; unsigned short int uy = (unsigned short) y; %u uy); printf("y = %d, uy = %u\n", y, uy); //TMax TMax=32767, TMin= size of short int is 2 //TMax=32767, TMin=-32768 size of unsigned short int is 2 // UMax = 65535 x = 15213, ux = 15213 y = -15213, uy = 50323 CS 47 Natasha Khuri Signed to Unsigned Conversions short int x = 15213; //in binary: 0011 1011 0110 1101 unsigned short int ux = (unsigned short) x; //in binary: 0011 1011 0110 1101 short int y = -15213; //in binary: 1100 0100 1001 0011 unsigned short int uy = (unsigned short) y; //in binary: 1100 0100 1001 0011 Note that the binary representation did not change, but our interpretation of the bits changed CS 47 Natasha Khuri Signed to Unsigned Casting No change in bit representation Nonnegative values remain unchanged: ux is 15213 //x is 15213 Negative values change into (large) positive values: uy is 50323 //y is -15213 Mathematically, we can define: x + 2 , x < 0 T 2U w ( x) = x, x 0 w CS 47 Natasha Khuri Signed/Unsigned Relationship X 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111 CS 47 Natasha Khuri B2T(X) 0 1 2 3 4 5 6 7 8 7 6 5 4 3 2 1 X+16 or X+24 B2U(X) 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Exercise Assume w = 4 convert the following signed values to unsigned: -8 -3 -1 0 5 CS 47 Natasha Khuri Unsigned to Signed Casting unsigned short u = 65535; //UMax short tu = (short) u; printf("u = %u, tu = %d\n", u, tu); u = 65535, tu = -1 u, u < 2 w-1 U 2Tw (u ) = u - 2 w , u 2 w-1 CS 47 Natasha Khuri Signed/Unsigned Relationship X 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111 CS 47 Natasha Khuri B2T(X) 0 1 2 3 4 5 6 7 8 7 6 5 4 3 2 1 X-16 or X-24 B2U(X) 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Signed vs. Unsigned in C Most numbers are signed by default: constants: 12453 or 0x1A2B unsigned constants have "U" as suffix U 0U, 0U 4294967259U In C conversion can occur due to: Explicit casting between signed & unsigned: tx, ty; int tx, ty; unsigned ux, uy; ux, uy; (int ux; int) tx = (int) ux; uy = (unsigned) ty; ty; Implicit casting also occurs via assignments and procedure calls: tx = ux; ux; uy = ty; ty; CS 47 Natasha Khuri Casting Surprises Expression evaluation: If unsigned and signed are mixed in single expression, signed values implicitly cast to unsigned: Including comparison operations: <, >, ==, <=, >= CS 47 Natasha Khuri Examples for W=8 Expression 0 == 0U -1 < 0 -1 < 0U -1 > -2 -1U > -2 -127 - 1 == 128U Type Result (0x1 or 0x0 0x1 0x0) CS 47 Natasha Khuri main(void) int main(void) { float a = {0.0, 1.2, 3.4, 5.6}; float result; sum_elements(a, result = sum_elements(a, 3); %f printf("result = %f\n", result); return 0; } float sum_elements(float a, unsigned length) { int i; float result = 0; lengthfor (i=0; i <= length-1; i++) a[i]; result += a[i]; return result; $ ./a.exe ./a.exe } result = 4.600000 CS 47 Natasha Khuri main(void) int main(void) { float a = {0.0, 1.2, 3.4, 5.6}; float result; sum_elements(a, result = sum_elements(a, 0); %f printf("result = %f\n", result); return 0; } float sum_elements(float a, unsigned length) { int i; float result = 0; lengthfor (i=0; i <= length-1; i++) a[i]; result += a[i]; return result; } CS 47 Natasha Khuri ./a.exe $ ./a.exe Segmentation fault (core dumped) main(void) int main(void) { float a = {0.0, 1.2, 3.4, 5.6}; float result; sum_elements(a, result = sum_elements(a, 0); %f printf("result = %f\n", result); return 0; } length) float sum_elements(float a, unsigned length) { int i; float result = 0; 0U 1 or 0U + (-1 + 232) lengthfor (i=0; i <= length-1; i++) a[i]; result += a[i]; return result; } CS 47 Natasha Khuri ./a.exe $ ./a.exe Segmentation fault (core dumped) main(void) int main(void) { float a = {0.0, 1.2, 3.4, 5.6}; float result; sum_elements(a, result = sum_elements(a, 0); %f printf("result = %f\n", result); return 0; } length) float sum_elements(float a, unsigned length) { int i; float result = 0; very large unsigned lengthfor (i=0; i <= length-1; i++) a[i]; result += a[i]; return result; } CS 47 Natasha Khuri ./a.exe $ ./a.exe Segmentation fault (core dumped) main(void) int main(void) { float a = {0.0, 1.2, 3.4, 5.6}; float result; sum_elements(a, result = sum_elements(a, 0); %f printf("result = %f\n", result); return 0; } length) float sum_elements(float a, unsigned length) { int i; float result = 0; i promoted to unsigned lengthfor (i=0; i <= length-1; i++) a[i]; result += a[i]; return result; } CS 47 Natasha Khuri ./a.exe $ ./a.exe Segmentation fault (core dumped) main(void) int main(void) { float a = {0.0, 1.2, 3.4, 5.6}; float result; sum_elements(a, result = sum_elements(a, 0); %f printf("result = %f\n", result); return 0; } length) float sum_elements(float a, unsigned length) { int i; float result = 0; keep incrementing lengthi++) for (i=0; i <= length-1; i++) a[i]; result += a[i]; return result; } CS 47 Natasha Khuri ./a.exe $ ./a.exe Segmentation fault (core dumped) main(void) int main(void) { float a = {0.0, 1.2, 3.4, 5.6}; float result; sum_elements(a, result = sum_elements(a, 0); %f printf("result = %f\n", result); return 0; } length) float sum_elements(float a, unsigned length) { int i; float result = 0; But, will access invalid element lengthfor (i=0; i <= length-1; i++) a[i result += a[i]; return result; } CS 47 Natasha Khuri ./a.exe $ ./a.exe Segmentation fault (core dumped) main(void) int main(void) { float a = {0.0, 1.2, 3.4, 5.6}; float result; sum_elements(a, result = sum_elements(a, 0); %f printf("result = %f\n", result); return 0; } length) float sum_elements(float a, unsigned length) { int i; float result = 0; And result in memory error lengthfor (i=0; i <= length-1; i++) a[i]; result += a[i]; return result; } CS 47 Natasha Khuri ./a.exe $ ./a.exe Segmentation fault (core dumped) Problems with Unsigned Values Subtle features of unsigned arithmetic, and especially the implicit conversion of signed to unsigned, can lead to errors and vulnerabilities. One way to avoid such bugs is to never use unsigned numbers: Java only supports signed integers and requires that they are implemented with two's complement arithmetic. The right shift performs an arithmetic shift CS 47 Natasha Khuri Benefits of Unsigned Values Useful when we want to think of words as just collection of bits with no numeric interpretation: packing words with flags describing various boolean conditions packing RGB values memory addresses are unsigned useful in modular and multiprecision arithmetic, in which numbers are represented by arrays of words CS 47 Natasha Khuri Data Type Conversions In C/Java we can convert between data types of different sizes: For example: between integers of different word sizes: short sx = -12345; (int sx; int) int x = (int) sx; //cast not required int y = 124; short sy = (short) y; The first operation is called sign extension and the second is called truncation CS 47 Natasha Khuri Sign Extension Task: Given W-bit signed integer x Convert it to W+k W+k-bit integer with same value Rule: Make k copies of sign bit: X= xw1 ,..., xw1 , xw1 , xw2 ,..., xo = k copies of MSB X w X CS 47 Natasha Khuri k w Sign Extension Example Converting from smaller to larger integer data type C/Java automatically perform sign extension. short int x = 15213; int ix = (int) x; short int y = -15213; int iy = (int) y; x ix y iy Decimal Hex 3B 15213 15213 00 00 3B C4 -15213 -15213 FF FF C4 6D 6D 93 93 Binary 00111011 00000000 00000000 00111011 11000100 11111111 11111111 11000100 01101101 01101101 10010011 10010011 CS 47 Natasha Khuri Exercise Show that each of the following bit vectors is a two's complement representation of -5: 1011 11011 111011 CS 47 Natasha Khuri Sign Extension/Casting short int sx = -12345; sx; unsigned int uy = sx; (int int) //(unsigned) (int) sx sign//first: sign-extend //cast to unsigned CS 47 Natasha Khuri Truncating Numbers Task: Given W+k W+k-bit signed integer x Convert it to W-bit integer with same value Method: drop the high-order k bits can alter the value. 0000 0100 0100 0101 //1157 short 0100 0101 //69 char CS 47 Natasha Khuri Truncate 4-bit values to 3 bits Unsigned Original 0 8 10 15 Truncated Two's complement Original 0 -8 -6 -1 Truncated CS 47 Natasha Khuri Overflow in Addition/Subtraction Signed overflow: Overflow can only occur if the two numbers are both positive or both negative. Addition of two positive numbers overflows if: a carry into the sign bit of the result occurs, but carry out of the MSB does not occur. Addition of two negative numbers overflows if: carry into the MSB does not occur carry out of the MSB does occur Unsigned overflow: overflow can only occur if there is a carry out of MSB overflow can occur in subtraction when the bottom number is bigger than the top number. CS 47 Natasha Khuri Signed Overflow Conditions Operation A + B A + B A B A B Operand A 0 <0 0 <0 Operand B 0 <0 <0 0 Result (overflow) <0 0 <0 0 CS 47 Natasha Khuri Exercise Which of the following four-bit two's complement additions result in overflow: 1000 + 1011 1000 + 1000 1000 + 0101 0010 + 0101 0101 + 0101 CS 47 Natasha Khuri Detecting Overflow Most computers have either special 1-bit storage areas called flags or special circuits to detect overflow. In C/Java programs, overflows are not signaled as errors. Ada and Fortran require that the program be notified: the programmer must decide what to do when overflow occurs CS 47 Natasha Khuri Overflow Summary The word size of computers is finite: independent of the representation, the arithmetic operations can create results that are two large to fit in this fixed word size. Overflows are easy to detect in addition of unsigned numbers: see last paragraph p.67 Detecting overflows in two's complement addition is a greater challenge: see p.70 CS 47 Natasha Khuri ...
View Full Document

This note was uploaded on 09/08/2010 for the course CS 47 at San Jose State University .

Ask a homework question - tutors are online