目录
前言:
0.0
前言:
整个实验的环境搭建,参考的是其他博主的博客环境搭建以及B站博主的题目思路。
这篇文章的不同点就是解题思路的讲解上是自己所消化吸收而得,以及一些自己当做日志所记录的问题。
1.0
题目:
//1
/*
* bitXor - x^y using only ~ and &
* Example: bitXor(4, 5) = 1
* Legal ops: ~ &
* Max ops: 14
* Rating: 1
*/
int bitXor(int x, int y) {
return 2;
}
/*
* tmin - return minimum two's complement integer
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 4
* Rating: 1
*/
int tmin(void) {
return 2;
}
//2
/*
* isTmax - returns 1 if x is the maximum, two's complement number,
* and 0 otherwise
* Legal ops: ! ~ & ^ | +
* Max ops: 10
* Rating: 1
*/
int isTmax(int x) {
return 2;
}
/*
* allOddBits - return 1 if all odd-numbered bits in word set to 1
* where bits are numbered from 0 (least significant) to 31 (most significant)
* Examples allOddBits(0xFFFFFFFD) = 0, allOddBits(0xAAAAAAAA) = 1
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 12
* Rating: 2
*/
int allOddBits(int x) {
int a=0xAA;
int b=a<<8|a;//0xAAA
int c=b<<16|b;//0xAAAA
return 2;
}
/*
* negate - return -x
* Example: negate(1) = -1.
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 5
* Rating: 2
*/
int negate(int x) {
return 2;
}
//3
/*
* isAsciiDigit - return 1 if 0x30 <= x <= 0x39 (ASCII codes for characters '0' to '9')
* Example: isAsciiDigit(0x35) = 1.
* isAsciiDigit(0x3a) = 0.
* isAsciiDigit(0x05) = 0.
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 15
* Rating: 3
*/
int isAsciiDigit(int x) {
return 2;
}
/*
* conditional - same as x ? y : z
* Example: conditional(2,4,5) = 4
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 16
* Rating: 3
*/
int conditional(int x, int y, int z) {
return 2;
}
/*
* 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) {
return 2;
}
//4
/*
* logicalNeg - implement the ! operator, using all of
* the legal operators except !
* Examples: logicalNeg(3) = 0, logicalNeg(0) = 1
* Legal ops: ~ & ^ | + << >>
* Max ops: 12
* Rating: 4
*/
int logicalNeg(int x) {
return 2;
}
/* howManyBits - return the minimum number of bits required to represent x in
* two's complement
* Examples: howManyBits(12) = 5
* howManyBits(298) = 10
* howManyBits(-5) = 4
* howManyBits(0) = 1
* howManyBits(-1) = 1
* howManyBits(0x80000000) = 32
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 90
* Rating: 4
*/
int howManyBits(int x) {
return 0;
}
//float
/*
* floatScale2 - 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 floatScale2(unsigned uf) {
return 2;
}
/*
* floatFloat2Int - Return bit-level equivalent of expression (int) f
* for floating point argument f.
* Argument is passed as unsigned int, but
* it is to be interpreted as the bit-level representation of a
* single-precision floating point value.
* Anything out of range (including NaN and infinity) should return
* 0x80000000u.
* Legal ops: Any integer/unsigned operations incl. ||, &&. also if, while
* Max ops: 30
* Rating: 4
*/
int floatFloat2Int(unsigned uf) {
return 2;
}
/*
* floatPower2 - Return bit-level equivalent of the expression 2.0^x
* (2.0 raised to the power x) for any 32-bit integer x.
*
* The unsigned value that is returned should have the identical bit
* representation as the single-precision floating-point number 2.0^x.
* If the result is too small to be represented as a denorm, return
* 0. If too large, return +INF.
*
* Legal ops: Any integer/unsigned operations incl. ||, &&. Also if, while
* Max ops: 30
* Rating: 4
*/
unsigned floatPower2(int x) {
return 2;
}
题解:
1.1bitXor:
但是应该逃不掉的
(return ~(x&y)&~(~x&~y);)
1.2tmin:
int32位 :-2147483648~2147483647
对应补码:1000……0~111……1
通过对1的左移运算(抛弃左端的值,右端补0)——>1000……
(return 1<<31;)
2.1isTmax:
2.2 allOddBits:
2.3negate:
唯一想起来的是,a-b=a+(-b的补码);负数的补码等于各位取反再末位加一
==>定义:一个数的相反数为其二进制位表示的按位取反,再+1.
3.1isAsciiDigit:
( return !((x-29)>>31)&!!((x-40)>>31)&!!(x^29); 不明白为啥通过不了)
(减号在此题中是违规应用,-b=(~b)+1 )
3.2conditional:
3.3isLessOrEqual:
有关符号判断,回顾3.1,正号的判断直接包括等于时的情况。
(这里不可以只运用isAsciiDigit(x)的思路,用减法和判断符号位来实现
因为isAsciiDigit(x)中,0x30 <= x <= 0x39,相减判断不会有溢出的情况)
当y<0,x>0时,可能出现 y-x>0 负溢出,而理论上用于判断y,x大小关系时应该是y-x<0,所以这种情况返回值取反
当y>0,x<0时,可能出现 y-x<0 正溢出,而理论上用于判断y,x大小关系时应该是y-x>0,所以这种情况返回值取反)
4.1logicalNeg:
4.2howManyBits:
(......)
5.1floatScale2:
unsigned floatScale2(unsigned uf) {
unsigned s=(uf>>31)&(0x1);
unsigned exper=(uf>>23)&(0xff);
unsigned frac=(uf&0x7fffff);
//0
if(exper==0&&frac==0)
return uf;
//
if(exper==0xff)
return uf;
if(exper==0)
{
frac<<=1; //
return (s<<31)|frac;
}
return (s<<31)|((exper+1)<<23)|(frac);
}
5.2floatFloat2Int:
int floatFloat2Int(unsigned uf) {
unsigned s=(uf>>31)&(0x1);
unsigned exper=(uf>>23)&(0xff);
unsigned frac=(uf&0x7fffff);
//0
if (exper==0&&frac==0)
return 0;
//
if(exper==0xff)
return 1<<31;//
if(exper==0)
return 0; //
//
int E=exper-127;
frac=frac|(1<<31);
if(E>31) //yichu
return 1<<31;
if(E<0)
return 0;
if(E>=23)
frac<<=(E-23);
else
frac>>=(23-E);
if(s) //+/-
return ~frac+1;
else return frac;
}
5.3floatPower2:
unsigned floatPower2(int x) {
if(x<-149)
return 0;
else if(x<-126)
{
int shift=23+(x+126);
return 1<<shift;
}
else if(x<=127)
{
int expr=x+127;
return expr<<23;
}
else
{
return (0xff)<<23;
}
}
一点记录:
0.00
(所需要的前置知识)
1. [~ 、&、^]
(^ :异或就是当参与运算的两个二进制数不同时结果才为1,其他情况为0。)
0.01 环境准备
1.Linux环境搭建
Ubuntu系统(乌班图)【听见博主这样说我是有点迷惑的】或者centos系统
可以选择 虚拟机 or docker(虚拟机已经有了,这个就没尝试)
2.题目资源:(重点readme的阅读)CS:APP3e, Bryant and O'Hallaron (cmu.edu)
3.Linux 命令
(vim)
一点琐碎:
00:遗留的问题:镜像源的更新,vim未下载成功(有博主说把网络改为桥接,不适用)
Ubuntu 中文输入法(英语……)
01:主要学到的还是一些琐碎的常识性问题(……),第一个实验原本难度不大。
02:关于记录习惯问题,真不太会(手写改文本功能再强大一点就好了)。多写多练对吧
03:【其实真正着手开始之后,很多东西就开始顺畅了】
【主要还是,我到底想要什么吧】
04:(......)
、