/* *************** * BIT Riddles * *************** * * There will two book prices for the two contestants, who * * Solved all five bit riddles correctly by the end of the week (Friday morning 5am). If more than two, then * * * Used the lest number of operations (excluding assignments) in total, and * * * Submitted the earliest by email to info@soi.ch in case of ties. * */ #if 0 /* * Read the following instructions VERY carefully !!! */ Fill in the functions below while strictly(!!!) adhereing to the following rules. If you are unsure if something is legal under the codeing rules, please ask! CODING RULES: Replace the "return" statement in each function with one or more lines of C code that implements the function. Your code must conform to the following style: int Funct(arg1, arg2, ...) { int var1 = Expr1; ... int varM = ExprM; varJ = ExprJ; ... varN = ExprN; return ExprR; } Note that all the local variables need to be of type int. Each "Expr" is an expression using ONLY the following: 1. Integer constants 0 through 255 (0xFF), inclusive. You are NOT allowed to use big constants such as 0xffffffff. 2. Function arguments and local variables (no global variables). 3. Unary integer operations ! ~ 4. Binary integer operations & ^ | + << >> Some of the problems restrict the set of allowed operators even further. Each "Expr" may consist of multiple operators. You are not restricted to one operator per line. You are expressly forbidden to: 1. Use any control constructs such as if, do, while, for, switch, etc. 2. Define or use any macros. 3. Define any additional functions in this file. 4. Call any functions. 5. Use any other operations, such as &&, ||, -, or ?: 6. Use any form of casting. You may assume that your machine: 1. Uses 2s complement, 32-bit representations of integers. 2. Performs right shifts arithmetically. 3. Has unpredictable behavior when shifting an integer by more than the word size. EXAMPLES OF ACCEPTABLE CODING STYLE: /* * pow2plus1 - returns 2^x + 1, where 0 <= x <= 31 */ int pow2plus1(int x) { /* exploit ability of shifts to compute powers of 2 */ return (1 << x) + 1; } /* * pow2plus4 - returns 2^x + 4, where 0 <= x <= 31 */ int pow2plus4(int x) { /* exploit ability of shifts to compute powers of 2 */ int result = (1 << x); result += 4; return result; } NOTES: 1. For each function only use local variables, assignemnts, (1 byte) constants and the legal operators listed in the function. 2. Try to use as few operators as possible. Note that '=' is not counted; you may use as many of these as you want without penalty. #endif /* * bitXor - x^y using only ~ and & * Example: bitXor(4, 5) = 1 * Legal ops: ~ & */ int bitXor(int x, int y) { return 2; } /* * isNotEqual - return 0 if x == y, and 1 otherwise * Examples: isNotEqual(5,5) = 0, isNotEqual(4,5) = 1 * Legal ops: ! ~ & ^ | + << >> */ int isNotEqual(int x, int y) { return 2; } /* * copyLSB - set all bits of result to least significant ( = left-most = sign) bit of x * Example: copyLSB(5) = 0xFFFFFFFF, copyLSB(6) = 0x00000000 * Legal ops: ! ~ & ^ | + << >> */ int copyLSB(int x) { return 2; } /* * bitCount - returns count of number of 1's in word * Examples: bitCount(5) = 2, bitCount(7) = 3 * Legal ops: ! ~ & ^ | + << >> */ int bitCount(int x) { return 2; } /* * bang - Compute !x without using ! * Examples: bang(3) = 0, bang(0) = 1 * Legal ops: ~ & ^ | + << >> */ int bang(int x) { return 2; } int main(){}