热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

有关位运算_ICS的一次Lab

运用位运算实现以下函数,不得使用if,while,for等,不得使用乘除减等运算,不得调用任何函数。不得使用负数及大于255(0xff)的数,不得使用小数等。在每个函数前有对此函数的要求限制及说

运用位运算实现以下函数,不得使用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 & ^ | + <<>>
   
  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 <= x <= 31
   */
  int pow2plus1(int x) {
     /* exploit ability of shifts to compute powers of 2 */
     return (1 <  }

  /*
   * pow2plus4 - returns 2^x + 4, where 0 <= x <= 31
   */
  int pow2plus4(int x) {
     /* exploit ability of shifts to compute powers of 2 */
     int result = (1 <     result += 4;
     return result;
  }


NOTES:
  1. Use the dlc (data lab checker) compiler (described in the handout) to
     check the legality of your solutions.
  2. Each function has a maximum number of operators (! ~ & ^ | + <<>>)
     that you are allowed to use for your implementation of the function.
     The max operator count is checked by dlc. Note that '=' is not
     counted; you may use as many of these as you want without penalty.
  3. Use the btest test harness to check your functions for correctness.
  4. The maximum number of ops for each function is given in the
     header comment for each function. If there are any inconsistencies
     between the maximum ops in the writeup and in this file, consider
     this file the authoritative source.

/*
 * Modify the following functions according the coding rules.
 *
 *   IMPORTANT. TO AVOID GRADING SURPRISES:
 *   1. Use the dlc compiler to check that your solutions conform
 *      to the coding rules.
 *   2. Use the btest test harness to check that your solutions produce
 *      the correct answers. Watch out for corner cases around Tmin and Tmax.
 */

以上为说明。

以下为需要实现的函数:
/*
 * 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));

}
/*
 * bitCount - returns count of number of 1's in word
 *   Examples: bitCount(5) = 2, bitCount(7) = 3
 *   Legal ops: ! ~ & ^ | + <<>>
 *   Max ops: 40
 *   Rating: 4
 */
int bitCount(int x) {
    int a = 255+(255<<8);
    int b = a^(a<<8);
    int c = b^(b<<4);
    int d = c^(c<<2);
    int e = d^(d<<1);
    x = (x&e)+((x>>1)&e);
    x = (x&d)+((x>>2)&d);
    x = (x&c)+((x>>4)&c);
    x = (x&b)+((x>>8)&b);
    x = (x&a)+((x>>16)&a); 
  return x;
}
/*
 * copyLSB - set all bits of result to least significant bit of x
 *   Example: copyLSB(5) = 0xFFFFFFFF, copyLSB(6) = 0x00000000
 *   Legal ops: ! ~ & ^ | + <<>>
 *   Max ops: 5
 *   Rating: 2
 */
int copyLSB(int x) {
  return ~(x&1)+1;
}
/*
 * divpwr2 - Compute x/(2^n), for 0 <= n <= 30
 *  Round toward zero
 *   Examples: divpwr2(15,1) = 7, divpwr2(-33,4) = -2
 *   Legal ops: ! ~ & ^ | + <<>>
 *   Max ops: 15
 *   Rating: 2
 */
int divpwr2(int x, int n) {
    int a=(x>>31)&1;
    return (x+(a<>n;
}
/*
 * evenBits - return word with all even-numbered bits set to 1
 *   Legal ops: ! ~ & ^ | + <<>>
 *   Max ops: 8
 *   Rating: 2
 */
int evenBits(void) {

  return (85<<24)+(85<<16)+(85<<8)+85;
  /*or (85<<12)|(85<<8)|(85<<4)|85); */

}
/*
 * fitsBits - return 1 if x can be represented as an
 *  n-bit, two's complement integer.
 *   1 <= n <= 32
 *   Examples: fitsBits(5,3) = 0, fitsBits(-4,3) = 1
 *   Legal ops: ! ~ & ^ | + <<>>
 *   Max ops: 15
 *   Rating: 2
 */
int fitsBits(int x, int n) {
    int y = x>>(n+~1+1);
    return !y^!(~y);
}
/*
 * getByte - Extract byte n from word x
 *   Bytes numbered from 0 (LSB) to 3 (MSB)
 *   Examples: getByte(0x12345678,1) = 0x56
 *   Legal ops: ! ~ & ^ | + <<>>
 *   Max ops: 6
 *   Rating: 2
 */
int getByte(int x, int n) {
  int y = x>>(n<<3);
  return y&255;

}
/*
 * isGreater - if x > y  then return 1, else return 0
 *   Example: isGreater(4,5) = 0, isGreater(5,4) = 1
 *   Legal ops: ! ~ & ^ | + <<>>
 *   Max ops: 24
 *   Rating: 3
 */
int isGreater(int x, int y) {

    int z = x+~y+1;
    int x1 = (x>>31)&1;
    int y1 = (y>>31)&1;
    int z1 = (z>>31)&1;
    int k = (x1^y1)&(z1^x1);
  return (!k&(!z1+~(!z)+1))|(z1&k);

}
/*
 * isNonNegative - return 1 if x >= 0, return 0 otherwise
 *   Example: isNonNegative(-1) = 0.  isNonNegative(0) = 1.
 *   Legal ops: ! ~ & ^ | + <<>>
 *   Max ops: 6
 *   Rating: 3
 */
int isNonNegative(int x){
    int a = x>>31;
    return !(a&1);
}
/*
 * isNotEqual - return 0 if x == y, and 1 otherwise
 *   Examples: isNotEqual(5,5) = 0, isNotEqual(4,5) = 1
 *   Legal ops: ! ~ & ^ | + <<>>
 *   Max ops: 6
 *   Rating: 2
 */
int isNotEqual(int x, int y) {
  int z = x+~y+1;
  return !!z;
}
/*
 * isPower2 - returns 1 if x is a power of 2, and 0 otherwise
 *   Examples: isPower2(5) = 0, isPower2(8) = 1, isPower2(0) = 0
 *   Note that no negative number is a power of 2.
 *   Legal ops: ! ~ & ^ | + <<>>
 *   Max ops: 60
 *   Rating: 4
 */
int isPower2(int x) {
    int y =x>>31;
    return (!!x)&!(1&y)&!(x&(x+(~1)+1));
}
/*
 * log2 - return floor(log base 2 of x), where x > 0
 *   Example: log2(16) = 4
 *   Legal ops: ! ~ & ^ | + <<>>
 *   Max ops: 90
 *   Rating: 4
 */
int log2(int x) {
 
  int y = x;
  int j = !!y;
  int h = (j<<31)>>31;
  int n = 1;

  int k = !(x>>16);
  int s = (k<<31)>>31;
  x = ((~s)&x)+(s&(x<<16));
  n = n+(s&16);

  k = !(x>>24);
  s = (k<<31)>>31;
  x = (~s&x)+(s&(x<<8));
  n = n+(s&8);

  k = !(x>>28);
  s = (k<<31)>>31;
  x = (~s&x)+(s&(x<<4));
  n = n+(s&4);

  k = !(x>>30);
  s = (k<<31)>>31;
  x = (~s&x)+(s&(x<<2));
  n = n+(s&2);
  return h&(31+!!(x>>31)+(~n+1));
}
/*
 * logicalShift - shift x to the right by n, using a logical shift
 *   Can assume that 1 <= n <= 31
 *   Examples: logicalShift(0x87654321,4) = 0x08765432
 *   Legal ops: ~ & ^ | + <<>>
 *   Max ops: 16
 *   Rating: 3
 */
int logicalShift(int x, int n) {
    int y = 1<<(31+~n+1);
    int z =  y|(y+~1+1);
    int a = x>>n;
    return a&z;

}
/*
 * satAdd - adds two numbers but when positive overflow occurs, returns
 *          maximum possible value, and when negative overflow occurs,
 *          it returns minimum positive value.
 *   Examples: satAdd(0x40000000,0x40000000) = 0x7fffffff
 *             satAdd(0x80000000,0xffffffff) = 0x80000000
 *   Legal ops: ! ~ & ^ | + <<>>
 *   Max ops: 30
 *   Rating: 4
 */
int satAdd(int x, int y){
    int z = x+y;
    int x1 = (x>>31)&1;
    int y1 = (y>>31)&1;
    int z1 = (z>>31)&1;
    int xy = !(x1^y1);
    int k = xy&(z1^x1);
    int a = k<<31;
    int b =a|(a+~k+1);
    int maxP = a+~1+1;
    return (~b&z)|(b&(maxP+!z1));

}
/*
 * tc2sm - Convert from two's complement to sign-magnitude
 *   where the MSB is the sign bit
 *   You can assume that x > TMin
 *   Example: tc2sm(-5) = 0x80000005.
 *   Legal ops: ! ~ & ^ | + <<>>
 *   Max ops: 15
 *   Rating: 4
 */
int tc2sm(int x) {
  int y = x >> 31 ;
  int sign = y&1;
  int mag = (x+y)^y;
 return (sign <<31) | mag;

}


推荐阅读
  • 自动调整列的宽度functionDBGridRecordSize(mColumn:TColumnEh):Boolean;{返回记录数据网格列显示最大宽度是否成功}beginResu ... [详细]
  • Java序列化对象传给PHP的方法及原理解析
    本文介绍了Java序列化对象传给PHP的方法及原理,包括Java对象传递的方式、序列化的方式、PHP中的序列化用法介绍、Java是否能反序列化PHP的数据、Java序列化的原理以及解决Java序列化中的问题。同时还解释了序列化的概念和作用,以及代码执行序列化所需要的权限。最后指出,序列化会将对象实例的所有字段都进行序列化,使得数据能够被表示为实例的序列化数据,但只有能够解释该格式的代码才能够确定数据的内容。 ... [详细]
  • 浏览器中的异常检测算法及其在深度学习中的应用
    本文介绍了在浏览器中进行异常检测的算法,包括统计学方法和机器学习方法,并探讨了异常检测在深度学习中的应用。异常检测在金融领域的信用卡欺诈、企业安全领域的非法入侵、IT运维中的设备维护时间点预测等方面具有广泛的应用。通过使用TensorFlow.js进行异常检测,可以实现对单变量和多变量异常的检测。统计学方法通过估计数据的分布概率来计算数据点的异常概率,而机器学习方法则通过训练数据来建立异常检测模型。 ... [详细]
  • 一、MyEclipse中的一些常用的快捷键:ctrl+shift+x大写ctrl+shift+y小写alt+内容提示写住方法的时候可以先写main然后按alt+就可以了ctrl+1 ... [详细]
  • PrivateConstLF_FACESIZE32PrivateConstCF_PRINTERFONTS&H2PrivateConstCF_SCREENFONTS ... [详细]
  • 编程题目是:定义一个整数计算类Integer,实现短整数+,-,*,基本算术运算。要求可以进行数据范围检查(-32768~32767,或自行设定),且在数据溢出时显示错误信息并中断程序执行。 ... [详细]
  • 在实际项目中有时候需要判断输入的值是否全为数字,然而直接用判断数字的一些函数如Val()和Isnumeric()等对"+"号,"-"号,还有小数点不能直接过滤 ... [详细]
  • 《GOF设计模式》—命令(COMMAND)—Delphi源码示例:支持取消和重做(多次取消1)
    示例:多次取消1说明:      若要支持多级的取消和重做,就需要有一个已被执行命令的历史列表(historylist),该列表的最大长度决定了取消和重做的级数。历史列表存储 ... [详细]
  • Java太阳系小游戏分析和源码详解
    本文介绍了一个基于Java的太阳系小游戏的分析和源码详解。通过对面向对象的知识的学习和实践,作者实现了太阳系各行星绕太阳转的效果。文章详细介绍了游戏的设计思路和源码结构,包括工具类、常量、图片加载、面板等。通过这个小游戏的制作,读者可以巩固和应用所学的知识,如类的继承、方法的重载与重写、多态和封装等。 ... [详细]
  • 向QTextEdit拖放文件的方法及实现步骤
    本文介绍了在使用QTextEdit时如何实现拖放文件的功能,包括相关的方法和实现步骤。通过重写dragEnterEvent和dropEvent函数,并结合QMimeData和QUrl等类,可以轻松实现向QTextEdit拖放文件的功能。详细的代码实现和说明可以参考本文提供的示例代码。 ... [详细]
  • 如何使用Java获取服务器硬件信息和磁盘负载率
    本文介绍了使用Java编程语言获取服务器硬件信息和磁盘负载率的方法。首先在远程服务器上搭建一个支持服务端语言的HTTP服务,并获取服务器的磁盘信息,并将结果输出。然后在本地使用JS编写一个AJAX脚本,远程请求服务端的程序,得到结果并展示给用户。其中还介绍了如何提取硬盘序列号的方法。 ... [详细]
  • 多数java程序员都非常清楚使用jar文件将组成java解决方案的各种资源(即.class文件、声音和图像)打包的优点。刚开始使用jar文件的人常问的一个问题是:&ldquo ... [详细]
  • Greetings,Imtryingtovalidatewhethermyintegerisnull.Ifitis,Ineedtoprompttheusert ... [详细]
  • 题目连接:http:poj.orgproblem?id2891题目大意:有一种表示非负整数的方法:选择k个不同的正整数a1,a2,,ak,对于某个整数m分别对ai求余,对应余数为ri,如 ... [详细]
  • java给iphone应用实现推送
    根据公司项目所需,需要对iphone应用进行消息推送,一开始选的是php,但是php语言知识略懂,开发起来比较麻烦,所有就用比较熟悉的java语言进行消息推送。需要依赖的jar包: ... [详细]
author-avatar
陌城花开2010
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有