首先声明datalab本人未完成,有4道题目没有做出来。本文博客记录下自己的解析,以便以后回忆。如果能帮助到你就更好了,如果觉得本文没啥技术含量,也望多多包涵。

/*
* bitAnd - x&y using only ~ and |
* Example: bitAnd(6, 5) = 4
* Legal ops: ~ |
* Max ops: 8
* Rating: 1
*/
int bitAnd(int x, int y) {
return ~(~x | ~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 offsetValue = 0xff;
int offsetIndex = n << 3;
int value = (x & (offsetValue << offsetIndex)) >> offsetIndex;
return value & offsetValue;
}
/*
* logicalShift - shift x to the right by n, using a logical shift
* Can assume that 0 <= n <= 31
* Examples: logicalShift(0x87654321,4) = 0x08765432
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 20
* Rating: 3
*/
int logicalShift(int x, int n) {
int offset = 0x1 << 31;
int offsetValue = ~(offset >> n << 1);
return (x >> n) & offsetValue;
}
/*
* 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) {
return 2;
}
/*
* bang - Compute !x without using !
* Examples: bang(3) = 0, bang(0) = 1
* Legal ops: ~ & ^ | + << >>
* Max ops: 12
* Rating: 4
*/
int bang(int x) {
return 2;
}
/*
* tmin - return minimum two's complement integer
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 4
* Rating: 1
*/
int tmin(void) {
return (0x1 << 31);
}
/*
* 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 offsetValue = 0x1 << n;
int addValue = (offsetValue >> 1) & (~offsetValue);//2^(n-1)
int value1 = x + addValue;//x - {-[2^(n-1)]}
int value2 = addValue + (~x);//[2^(n-1)-1] - x
int maxValue = 0x1 << 31;
return (n >> 5) | ((!(value1 & maxValue)) & (!(value2 & maxValue)));
}
/*
* 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 maxValue = 0x1 << 31;
int offsetValue = ~(0x1 << 31 >> (32 + ~n));
int andValue = offsetValue & x;
return (x >> n) + ((!!(x & maxValue)) & (!!(andValue)));
}
/*
* negate - return -x
* Example: negate(1) = -1.
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 5
* Rating: 2
*/
int negate(int x) {
return ~x + 1;
}
/*
* isPositive - return 1 if x > 0, return 0 otherwise
* Example: isPositive(-1) = 0.
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 8
* Rating: 3
*/
int isPositive(int x) {
return (!(x >> 31)) ^ (!x);
}
/*
* isLessOrEqual - if x <= y then return 1, else return 0
* Example: isLessOrEqual(4,5) = 1.
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 24
* Rating: 3
*/
int isLessOrEqual(int x, int y) {
int offsetValue = 0x1;
int offsetIndex = 31;
int offsetSign = offsetValue << offsetIndex;
int signX = !(x & offsetSign);
int signY = !(y & offsetSign);
int value1 = ((!signX) & signY )^ 0x0;
int value2 = (signX & (!signY)) ^ 0x1;
int value3 = (!((y + ~x + 1) & offsetSign)) ^ 0x0;
return value1 | (value2 & value3);
}
/*
* ilog2 - return floor(log base 2 of x), where x > 0
* Example: ilog2(16) = 4
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 90
* Rating: 4
*/
int ilog2(int x) {
return 2;
}
/*
* float_neg - Return bit-level equivalent of expression -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, return argument.
* Legal ops: Any integer/unsigned operations incl. ||, &&. also if, while
* Max ops: 10
* Rating: 2
*/
unsigned float_neg(unsigned uf) {
int offsetValue = 0x1;
int offsetIndex = 0;
int andValue = 0;
int signValue;
while (offsetIndex < 31)
{
signValue = (uf & offsetValue) >> offsetIndex;
if (offsetIndex < 23)
{
andValue = andValue | signValue;
}
else
{
andValue = andValue & signValue;
}
offsetIndex += 1;
offsetValue <<= 1;
}
if (andValue)
{
return uf;//NaN
}
return uf ^ offsetValue;
}
/*
* float_i2f - Return bit-level equivalent of expression (float) x
* Result is returned as unsigned int, but
* it is to be interpreted as the bit-level representation of a
* single-precision floating point values.
* Legal ops: Any integer/unsigned operations incl. ||, &&. also if, while
* Max ops: 30
* Rating: 4
*/
unsigned float_i2f(int x) {
return 2;
}
/*
* float_twice - Return bit-level equivalent of expression 2*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 representation of
* single-precision floating point values.
* When argument is NaN, return argument
* Legal ops: Any integer/unsigned operations incl. ||, &&. also if, while
* Max ops: 30
* Rating: 4
*/
unsigned float_twice(unsigned uf) {
int signIndex = 31;
int expIndex = 23;
int offsetValue = 0x1;
int offsetSign = offsetValue << signIndex;
int andValue = 1;
int orValue = 0;
int signValue;
int offsetIndex = expIndex;
while (offsetIndex < signIndex)
{
signValue = (uf & (offsetValue << offsetIndex)) >> offsetIndex;
andValue = andValue & signValue;
orValue = orValue | signValue;
offsetIndex += 1;
}
if (andValue == 1)//exp==255
{
return uf;
}
else if (orValue == 0)//非规格化
{
signValue = !!(uf & offsetSign);
uf <<= 1;
if (signValue == 0)
{
return uf & (~offsetSign);
}
return uf | offsetSign;
}
else
{
signValue = ((uf >> expIndex) + 1) << expIndex;
offsetIndex = expIndex;
while (offsetIndex < signIndex)
{
uf &= ~(offsetValue << offsetIndex);
offsetIndex += 1;
}
return uf | signValue;
}
}

datalab (原发布 csdn 2018年09月21日 20:42:54)的更多相关文章

  1. c# Equal函数 and 运算符'==' (原发布 csdn 2017年10月15日 20:39:26)

    1.==.!=.<.>.<= 和>= 运算符为比较运算符(comparison operator).C#语言规范5.0中文版中比较运算符的描述如下: 2.通用类型系统 3.值类 ...

  2. 关于“关于C#装箱的疑问”帖子的个人看法 (原发布csdn 2017年10月07日 10:21:10)

    前言 昨天晚上闲着无事,就上csdn逛了一下,突然发现一个帖子很有意思,就点进去看了一下. 问题很精辟 int a = 1; object b=a; object c = b; c = 2; 为什么b ...

  3. getDate() 获取时间 如2018年09月21日 11:32:11

    function p(s) { return s < 10 ? '0' + s: s;} function getDate() { var myDate = new Date(); //获取当前 ...

  4. 2018年1月21日--2月4日 NAS

    二十号去比赛时,与同事闲聊时说起家庭服务器,后来搜到nas(网络附着存储器),找到freenas,突然觉得很有用,手机拍了大量的照片视频,存储在电脑,已经换过几次硬盘了,对于这些珍贵的资料,万一硬盘坏 ...

  5. java自动化测试开发环境搭建(更新至2018年10月8日 11:42:15)

    1.安装JDK的1.8版本 官网下载地址:https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151 ...

  6. 2014年12月23日00:42:54——PS4

    http://tieba.baidu.com/p/3415598015?see_lz=1&pn=1 http://tieba.baidu.com/p/3188981817 http://tie ...

  7. 【12月21日】A股滚动市盈率PE历史新低排名

    2010年01月01日 到 2018年12月21日 之间,滚动市盈率历史新低排名.上市三年以上的公司,2018年12月21日市盈率在300以下的公司. 1 - 厦门象屿(SH600057) - 历史新 ...

  8. RxJava2.0学习笔记2 2018年7月3日 周二

    摘记: 1.map -- 转换  有些服务端的接口设计,会在返回的数据外层包裹一些额外信息,这些信息对于调试很有用,但本地显示是用不到的.使用 map() 可以把外层的格式剥掉,只留下本地会用到的核心 ...

  9. 导航狗IT周报-2018年05月27日

    原文链接:https://www.daohanggou.cn/2018/05/27/it-weekly-9/ 摘要: “灰袍技能圈子”将闭圈:物理安全:为什么我们现在的生活节奏越来越快? 技术干货 1 ...

随机推荐

  1. Java内存模型以及happens-before规则

    本人免费整理了Java高级资料,涵盖了Java.Redis.MongoDB.MySQL.Zookeeper.Spring Cloud.Dubbo高并发分布式等教程,一共30G,需要自己领取.传送门:h ...

  2. 适合初学者的Python爬取链家网教程

    前言 文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 作者: TinaLY PS:如有需要Python学习资料的小伙伴可以加点击下 ...

  3. element-admin中echarts图标宽度无法修改

    默认示例 <template> <div> <el-row :gutter="0"> <el-col :xs="24" ...

  4. CAD画三维图怎么渲染?一分钟教你快速操作

    从事过CAD相关工作的都知道,CAD绘制的方式有二维平面图以及三维图形,三维图形,画三维图方式也是比较简单的.那当然三维图画完后一般还需要进行渲染操作,步骤也是比较简洁的.下面就来给大家操作一下CAD ...

  5. H5 移动端 键盘遮挡焦点元素解决方案

    前言 最近在做 webapp,遇到了很多移动端兼容的问题,其中一个问题就是:输入框触发 focus 后,键盘弹出,然后遮住了输入框. 然后在Android和IOS上,这个问题的表现形式不一样,而原生键 ...

  6. JDK8日常开发系列:Consumer详解

    java.util.function中 Function, Supplier, Consumer, Predicate和其他函数式接口广泛用在支持lambda表达式的API中.这些接口有一个抽象方法, ...

  7. 大数据分析的下一代架构--IOTA架构设计实践[下]

    大数据分析的下一代架构--IOTA架构设计实践[下] 原创置顶 代立冬 发布于2018-12-31 20:59:53 阅读数 2151  收藏 展开 IOTA架构提出背景 大数据3.0时代以前,Lam ...

  8. UGUI Manual

    以Unity 5.5 的官方文档为例 Canvas UI元素的前后顺序:SetAsFirstSibling, SetAsLastSibling, and SetSiblingIndex BasicLa ...

  9. 爬虫 xpath 获取方式

    回顾 bs4 实例化bs对象,将页面源码数据加载到该对象中 定位标签:find('name',class_='xxx') findall() select() 将标签中的文本内容获取 string t ...

  10. day71_10_16多表断关联

    ---恢复内容开始--- 本次环境: 配置settings INSTALLED_APPS = [ # ... 'rest_framework', ] DATABASES = { 'default': ...