有关位运算_ICS的一次Lab

运用位运算实现以下函数,不得使用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 &

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值