主要是位操作还有二进制补码的理解。
参考如下文章:
https://wenku.baidu.com/view/584fee14af1ffc4ffe47acf2.html (非常好的实验报告)
http://blog.163.com/miss_littleli/blog/static/24500302320151123115441194/ (log2这一题讲的非常清楚)
/* CS 213 Fall '99. Lab L1 */
#include "btest.h"
/*******************************************************************
* IMPORTANT: Fill in the following struct with your identifying info
******************************************************************/
team_struct team =
{
/* Team name: Replace with either:
Your login ID if working as a one person team
or, ID1+ID2 where ID1 is the login ID of the first team member
and ID2 is the login ID of the second team member */
"",
/* Student name 1: Replace with the full name of first team member */
"",
/* Login ID 1: Replace with the login ID of first team member */
"",
/* The following should only be changed if there are two team members */
/* Student name 2: Full name of the second team member */
"",
/* Login ID 2: Login ID of the second team member */
""
};
#if 0
LAB L1 INSTRUCTIONS:
#endif
/********************************
* Part I - Bit-level operations
*******************************/
/*
* bitOr - x|y using only ~ and &
* Example: bitOr(4, 5) = 5
* Legal ops: ~ &
* Max ops: 12
* Rating: 2
*/
int bitOr(int x, int y) {
return ~((~x)&(~y));
}
/*
* bitXor - x^y using only ~ and &
* Example: bitXor(4, 5) = 1
* Legal ops: ~ &
* Max ops: 21
* Rating: 2
*/
int bitXor(int x, int y) {
long int x_and_y = x&y;
long int x_or_y = ~(~x & ~y);
return x_or_y & ~x_and_y;
}
/*
* isZero - returns 1 if x == 0, and 0 otherwise
* Examples: isZero(5) = 0, isZero(0) = 1
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 3
* Rating: 1
*/
int isZero(int x) {
return !x;
}
/*
* isEqual - return 1 if x == y, and 0 otherwise
* Examples: isEqual(5,5) = 1, isNotEqual(4,5) = 0
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 6
* Rating: 2
*/
int isEqual(int x, int y) {
return !(x ^ y);
}
/*
* reverseBytes - reverse the bytes of x
* Example: reverseBytes(0x01020304) = 0x04030201
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 39
* Rating: 3
*/
int reverseBytes(int x) {
int result = (x & 0xff) << 24;
result |= ((x >> 8) & 0xff) << 16;
result |= ((x >> 16) & 0xff) << 8;
result |= ((x >> 24) & 0xff);
return result;
}
/****
Part II:
Two's complement arithmetic
****/
/*
* TMin - return minimum two's complement integer
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 3
* Rating: 1
*/
int TMin(void) {
//1000 0000 0000 0000 0000 0000 0000 0000
return 0x01<<31;
}
/*
* minusOne - return a value of -1
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 3
* Rating: 1
*/
int minusOne(void) {
//1111 1111 1111 1111 1111 1111 1111 1111
return ~0;
}
/*
* TMax - return maximum two's complement integer
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 6
* Rating: 1
*/
int TMax(void) {
//0111 1111 1111 1111 1111 1111 1111 1111
return ~(0x01<<31);
}
/*
* isPositive - return 1 if x > 0, return 0 otherwise
* Example: isPositive(-1) = 0.
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 12
* Rating: 3
*/
int isPositive(int x) {
/* 是正数:符号位是0且值不为0
* int s=!(x>>31) s判断符号位
* return s&(!!x) s为1且x不为0
*/
return !(!x | x >> 31);
}
/*
* negate - return -x
* Example: negate(1) = -1.
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 6
* Rating: 2
*/
int negate(int x) {
return ~x+1;
}
/*
* logicalShift - logical right shift of x by y bits, 1 <= y <= 31
* Example: logicalShift(-1, 1) = TMax.
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 24
* Rating: 3
*/
int logicalShift(int x, int y)
{
/* 逻辑右移最高位全部补0;就是为了得到最高y位全部是0,其余位全部是1的串
* ① 将1左移31位
* ② 右移y位,得到左边y+1位全部是1;左移1位,得到左边y位全部是1.
* (由于y可能为0,所以第二步不能直接右移y-1位)
* ③ 将第二步得到结果取反,得到左边y位全部是0,右边全部是1
* ④ 将x右移y位,与第三步结果&操作
*/
return ~(((1<<31)>>y)<<1)&(x>>y);
}
/*
* isLessOrEqual - if x <= y then return 1, else return 0
* Example: isLessOrEqual(4,5) = 1.
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 39
* Rating: 4
*/
int isLessOrEqual(int x, int y) {
/* x<=y 也即y-x>=0
* ① 当x和y同号,只需验证y-x>=0是否成立。-x就是~x+1。验证y+~x+1的符号位就行
* ② 当x和y异号,只要保证x是负数,y是正数就行
*/
int sign_x = x>>31;
int sign_y = y>>31;
int signSame = (!(sign_x^sign_y))&(!((y+~x+1)>>31));
int signDiff = sign_x&(!sign_y);
return signSame|signDiff;
}
/*
* abs - absolute value of x (except returns TMin for TMin)
* Example: abs(-1) = 1.
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 15
* Rating: 4
*/
int abs(int x) {
//x为正,mask=0;x为负,mask=-1(也即0xffffffff);
int mask = x>>31;
//x为正,xx=x;x为负,xx=~x;
int xx = x^mask;
//x为正,addition=0;x为负,addition=1;
int addition = (x>>31)&0x1;
return xx+addition;
}
/*
* logicalNeg - implement the ! operator, using all of
* the legal operators except !
* Examples: logicalNeg(3) = 0, logicalNeg(0) = 1
* Legal ops: ~ & ^ | + << >>
* Max ops: 18
* Rating: 4
*/
int logicalNeg(int x) {
/* 0的二进制补码表示是 所有位都是0
* +0 -0
* 原码:00000000 100000000
* 反码:00000000 111111111
* 补码:00000000 000000000
* 本题求~x,设t=~x+1,只有当x为0时,t和x的符号位都为0;当x不为0,t和x符号位总有一个为1
* 取x和t的符号位异或保留至最低位,再取反后最低位即为!x
*/
int minus_x = ~x+1;
return ~((minus_x|x) >> 31) & 0x01;
}
/*
* 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 s1 = !!(x>>16)<<4;
x >>= s1;
int s2 = !!(x>>8)<<3;
x >>= s2;
int s3 = !!(x>>4)<<2;
x >>= s3;
int s4 = !!(x>>2)<<1;
x >>= s4;
int s5 = !!(x>>1);
return s1+s2+s3+s4+s5;
}
/*
* leastBitPos - return a mask that marks the position of the
* least significant 1 bit. If x == 0, return 0
* Example: leastBitPos(96) = 0x20
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 45
* Rating: 4
*/
int leastBitPos(int x) {
int mask1, mask2, result;
/* x = 0000 0000 0000 0000 0000 0000 0110 0000 */
/* special case for x==0. All 0's iff x == 0, otherwise all 1's */
int zeromask = ~(((!x) << 31)>>31);
/* zeromask = 1111 1111 1111 1111 1111 1111 1111 1111 */
/* sets lsb, clears bits to left of lsb and sets bits to the right */
mask1 = x ^ (x + ~0);
/* mask1 = 0000 0000 0000 0000 0000 0000 0011 1111 */
/* negate the result of a logical right shift by 1 */
/* this clears the bits to the right of the lsb */
mask2 = ~((mask1 >> 1) & (~(1 << 31)));
/* mask2 = 1111 1111 1111 1111 1111 1111 1110 0000 */
result = zeromask & mask1 & mask2;
return result;
}