运用位运算实现以下函数,不得使用if,while,for等,不得使用乘除减等运算,不得调用任何函数。不得使用负数及大于255(0xff)的数,不得使用小数等。在每个函数前有对此函数的要求限制及说明。如下:Legal ops为在该函数中允许使用的操作符,Max ops表示这些操作符最多可使用的次数(各操作符使用的次数之和),当然你可以使用任意多个“=”号,你也可以定义任意多个变量,Rating为实现的难度(给定难度值仅供参考),下面有更详细的英文说明。
仅可能少地使用操作符!!我已经实现了所有的函数,希望网友能提供更好的方法,欢迎交流学习(QQ:121853868)
如下:
/*
* bang - Compute !x without using !
* Examples: bang(3) = 0, bang(0) = 1
* Legal ops: ~ & ^ | + << >>
* Max ops: 12
* Rating: 4
*/
int bang(int x) {
int a,b,c;
a = (~x+1);
b = a>>31;
c = x>>31;
return 1&~((b&1)|(c&1));
}
/*
* Read the following instructions carefully.
*/
You will provide your solution to the Data Lab by
editing the collection of functions in this source file.
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 &