Question: WRITE THESE C FUNCTIONS USING ONLY THE BITWISE OPERATORS SHOWN (no if, while, for, or any other loops or conditionals) /* * numOddBytes(int x) -
WRITE THESE C FUNCTIONS USING ONLY THE BITWISE OPERATORS SHOWN (no if, while, for, or any other loops or conditionals)
/*
* numOddBytes(int x) - returns number of bytes whose least significant bit is 1
* Examples: numOddBytes(1) = 1
* numOddBytes(128) = 0
* numOddBytes(-127) = 4
* Legal ops: ~ & ^ | + << >>
* Max ops: 30
* Rating: 2
*/
int numOddBytes(int x) {
return 2;
}
/*
* subOK - Determine if can compute x-y without overflow
* Example: subOK(0x80000000,0x80000000) = 1,
* subOK(0x80000000,0x70000000) = 0,
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 20
* Rating: 3
*/
int subOK(int x, int y) {
return 2;
}
/*
* squaredOK - Return 1 if x*x can fit in a 32-bit integer, and 0 otherwise.
* Examples: squaredOK(10) = 1
* squaredOK(1000000) = 0
* Legal ops: ! ~ & ^ | + - << >>
* Max ops: 20
* Rating: 3
*/
int squaredOK(int x) {
return 2;
}
/*
* byteParity - compute high bit of bytes for even parity.
*
* Consider an error detection scheme in which the the most
* significant bit of each byte is chosen to be 1 if an odd number
* of the other 7 bits in the byte are 1, and 0 otherwise. In other
* words, when the parity bit is included, each byte will have an
* even number of bits set to 1. Given four bytes in an int,
* transform x so that the most significant bits are set as parity
* bits for the other 7 bits, which should be unchanged.
*
* Examples: byteParity(0x01020304) = 0x81820384
* byteParity(0xAC) = 0xAC
* byteParity(0xFFFFFFFF) = 0xFFFFFFFF
* byteParity(0x67676767) = 0xE7E7E7E7
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 50
* Rating: 4
*/
int byteParity(int x) {
return 2;
}
/*
* floatTimesTwo - Return bit-level equivalent of expression f * 2 for
* floating point argument f represented as bits uf.
*
* Both the argument and result are passed as unsigned int's, but
* they are to be interpreted as the bit-level representations of
* single-precision floating point values.
* When argument is NaN, return argument.
*
* Legal ops: Any integer/unsigned operations incl. ||, &&. also if, while
* Max ops: 50
* Rating: 2
*
*/
unsigned floatTimesTwo(unsigned uf) {
return 2;
}
/*
* roundFloat - Return bit-level equivalent of expression round(f) for
* floating point argument f.
*
* Both the argument and result are passed as unsigned int's, but
* they are to be interpreted as the bit-level representations of
* single-precision floating point values.
* When argument is NaN, +/- infinity, or +/- 0, return argument.
*
* roundFloat should round values to the nearest integer, with ties
* broken by rounding away from zero. In other words if the
* fractional part of a number is less than 0.5, the fraction is
* removed, i.e. the value is rounded towards zero. If the fraction
* part is 0.5 or more, the value is rounded up to the next number
* further away from 0.
*
* Example: roundFloat(3.5) -> 4
* Example: roundFloat(3.3) -> 3
* Example: roundFloat(2.5) -> 3
* Example: roundFloat(2.25) -> 2
* Example: roundFloat(-2.25)-> -2
* Example: roundFloat(-2.5) -> -3
* Example: roundFloat(-3.3) -> -3
* Example: roundFloat(-3.5) -> -4
*
* Example: roundFloat(75.67) -> 76
* Example: roundFloat(-128.59) -> -129
* Example: roundFloat(42.389) -> 42
*
* Legal ops: Any integer/unsigned operations incl. ||, &&. also if, while
* Max ops: 50
* Rating: 4
*
*/
unsigned roundFloat(unsigned uf) {
return 2;
}
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
