Rules 1. Integer constants 0 through 255 (0xFF), inclusive. You are not allowed
ID: 3628441 • Letter: R
Question
Rules1. 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.
7. Use any data type other than int. This implies that you
cannot use arrays, structs, or unions.
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.
<code>/*
* anyOddBitIs1 - return 1 if any odd-numbered bit in x set to 1,
* and return 0 otherwise.
* Examples anyOddBitIs1(0x5) = 0, anyOddBitIs1(0x7) = 1
* Note that LSB is numbered at bit 0, which is even.
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 12
* Rating: 2
*/
int anyOddBitIs1(int x) {
return 2;
}
/*
* everyEvenBitIs0 - return 1 if every even-numbered bit in x is set to 0, and
* return 0 otherwise.
* Examples everyEvenBitIs0(0xAAAAAAAA) = 1, everyEvenBitIs0(0xAAAAAAAF) = 0
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 12
* Rating: 2
*/
int everyEvenBitIs0(int x) {
return 2;
}
/*
* isTMax - return 1 if x is the maximum, two's complement number,
* and 0 return otherwise.
* Legal ops: ! ~ & ^ | +
* Max ops: 10
* Rating: 1
*/
int isTMax(int x) {
return 2;
}
/*
* multiplyByThreeEighths - multiply by 3/8 rounding toward 0, which
* should exactly duplicate effect of C expression (x*3/8),
* including overflow behavior.
* Examples: multiplyByThreeEighths(77) = 28
* multiplyByThreeEighths(-22) = -8
* multiplyByThreeEighths(1073741824) = -134217728 (overflow)
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 12
* Rating: 3
*/
int multiplyByThreeEighths(int x) {
return 2;
}
</code>
Explanation / Answer
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, ...) { /* brief description of how your implementation works */ int var1 = Expr1; ... int varM = ExprM; varJ = ExprJ; ... varN = ExprN; return ExprR; } 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 * Max ops: 15 * Rating: 4 */ int sm2tc(int x) { return 2; }Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.