Question: I need to simulate the negative symbol using bitwise operators, limited to ! ~ & ^ | + < < >> . For example, if
I need to simulate the negative symbol using bitwise operators, limited to ! ~ & ^ | + << >>. For example, if x is given, the function should return -x. (i.e. If 1 is given, the function should return -1.) You can only use a total of 5 operators to get the solution.
=======CODE BELOW============
/*
* negate - return -x
* Example: negate(1) = -1.
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 5
* Rating: 2
*/
int negate(int x) {
// CODE GOES HERE. The "return 2" line should be changed to return the correct result.
return 2;
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
