# 《深入理解计算机系统/CSAPP》

isTmax(int x)
minusOne(void)
upperBits(int n)
copyLSB(int x)
anyOddBit(int x)
getByte(int x, int n)
implication(int x, int y)
mult3div2(int x)
isAsciiDigit(int x)
logicalAnd(int x, int y)
multFiveEighths(int x)
rotateRight(int x, int n)
satMul3(int x)
isNonZero(int x)
leftBitCount(int x)
float_f2i(unsigned uf)

``````/*
isTmax - 如果x是最大的二进制补码，返回1；否则返回0
*   允许的操作符: ! ~ & ^ | +
*   最多操作符数目: 10
*   分值: 2*/
int isTmax(int x) {
return !((x^(~(x+1))))&(!!(~x));
//return !((x^(~(x+1)))|(!(~x)));
}

int isTmax(int x) {
return (!(x+1+x+1))&(!!(x+1));
}
``````

``````/*
* minusOne - return a value of -1
*   Legal ops: ! ~ & ^ | + << >>
*   Max ops: 2
*   Rating: 1
*/
int minusOne(void) {
return ~0;
}

int minusOne(void){
return return (1 << 31) >> 31;
}
``````

``````/*
* upperBits - pads n upper bits with 1's
*  You may assume 0 <= n <= 32
*  Example: upperBits(4) = 0xF0000000
*  Legal ops: ! ~ & ^ | + << >>
*  Max ops: 10
*  Rating: 1
*/
int upperBits(int n) {
return ((!!n)<<31)>>(n+(~0));
}

``````

``````/*
* 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<<31)>>31;
}
``````

``````/*
* anyOddBit - return 1 if any odd-numbered bit in word set to 1
*   Examples anyOddBit(0x5) = 0, anyOddBit(0x7) = 1
*   Legal ops: ! ~ & ^ | + << >>
*   Max ops: 12
*   Rating: 2
*/
int anyOddBit(int x) {
int mask = 0xAA | 0xAA << 8;
}
``````

0xAA 位 1010,当中所有的1都在奇数位，所有的0都在偶数位，

``````/*
* 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) {
return ((x >> (n << 3)) & 0xFF);
}
``````

1byte=8bit，直接右移n << 3位 取字节 n & 0xFF

``````/*
* implication - return x -> y in propositional logic - 0 for false, 1
* for true
*   Example: implication(1,1) = 1
*            implication(1,0) = 0
*   Legal ops: ! ~ ^ |
*   Max ops: 5
*   Rating: 2
*/
int implication(int x, int y) {
return (!x)|(!!y);
}
``````

``````/*
* mult3div2 - multiplies by 3/2 rounding toward 0,
*   Should exactly duplicate effect of C expression (x*3/2),
*   including overflow behavior.
*   Examples: mult3div2(11) = 16
*             mult3div2(-9) = -13
*             mult3div2(1073741824) = -536870912(overflow)
*   Legal ops: ! ~ & ^ | + << >>
*   Max ops: 12
*   Rating: 2
*/
int mult3div2(int x) {
int y = (x << 1) + x;
return (y >> 1) + (((y >> 31)&1)&(((y << 31)>>31)&1));
}
``````

``````/*
* 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 (!((x+(~0x30+1))>>31)) & (!!((x+(~0x3a+1))>>31));
}
``````

&左边的值可能为0或者正数 最高为0
&右边的值肯定为负数 最高位只能1

``````/*
*   logicalAnd - x && y
*   Legal ops: ! ~ & ^ | + << >>
*   Max ops: 20
*   Rating: 3
*/
int logicalAnd(int x, int y) {
return !!x & !!y;
}
``````

``````/*
* multFiveEighths - multiplies by 5/8 rounding toward 0.
*   Should exactly duplicate effect of C expression (x*5/8),
*   including overflow behavior.
*   Examples: multFiveEighths(77) = 48
*             multFiveEighths(-22) = -13
*             multFiveEighths(1073741824) = 13421728 (overflow)
*   Legal ops: ! ~ & ^ | + << >>
*   Max ops: 12
*   Rating: 3
*/
int multFiveEighths(int x) {
int mult = (x << 2) + x; // 5*x
int sign = (mult >> 31); // 提取符号位
int neground = ((mult + 7) >> 3) & sign; //四舍五入
return neground + ((mult & ~sign) >> 3);
//取舍入结果，如果符号为1（负），则返回
//只是舍入的部分，如果不是，则仅返回
//return语句的后半部分，其中我们除以8。
}
``````

``````/*
* rotateRight - Rotate x to the right by n
*   Can assume that 0 <= n <= 31
*   Examples: rotateRight(0x87654321,4) = 0x18765432
*   Legal ops: ~ & ^ | + << >> !
*   Max ops: 25
*   Rating: 3
*/
int rotateRight(int x, int n) {
int a, x2, x3, negN;
negN = ~ n + 1;
a = ~ (~ 0 << (32 + negN));
x2 = x >> n;
x3 = x << (32 + negN);
return (x2 & a) | x3;
}
``````

``````/*
* satMul3 - multiplies by 3, saturating to Tmin or Tmax if overflow
*  Examples: satMul3(0x10000000) = 0x30000000
*            satMul3(0x30000000) = 0x7FFFFFFF (Saturate to TMax)
*            satMul3(0x70000000) = 0x7FFFFFFF (Saturate to TMax)
*            satMul3(0xD0000000) = 0x80000000 (Saturate to TMin)
*            satMul3(0xA0000000) = 0x80000000 (Saturate to TMin)
*  Legal ops: ! ~ & ^ | + << >>
*  Max ops: 25
*  Rating: 3
*/
int satMul3(int x) {
int x2 = x << 1;
int x3 = x2 + x;
int sign = x >> 31;
int tmin = 1 << 31;
int g = ((x^x2)|(x^x3))>>31;
return ((~g)&x3)+(g&(tmin+(~sign)));
}
``````

``````/*
* isNonZero - Check whether x is nonzero using
*              the legal operators except !
*   Examples: isNonZero(3) = 1, isNonZero(0) = 0
*   Legal ops: ~ & ^ | + << >>
*   Max ops: 10
*   Rating: 4
*/
int isNonZero(int x) {
return (x|(~x+1))>>31&1;
}
``````

``````/*
* leftBitCount - returns count of number of consective 1's in
*     left-hand (most significant) end of word.
*   Examples: leftBitCount(-1) = 32, leftBitCount(0xFFF0F0F0) = 12
*   Legal ops: ! ~ & ^ | + << >>
*   Max ops: 50
*   Rating: 4
*/
int leftBitCount(int x) {
int cnt = 0;
int off = 1&(!(~x));
cnt += (!!(~(x>>16)))<<4;
cnt += (!!(~(x>>(cnt+8))))<<3;
cnt += (!!(~(x>>(cnt+4))))<<2;
cnt += (!!(~(x>>(cnt+2))))<<1;
cnt += (!!(~(x>>(cnt+1))));
return 32+~cnt+off;
}
``````

``````/*
* float_f2i - 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 float_f2i(unsigned uf) {
int s_    = uf>>31;
int exp_  = ((uf&0x7f800000)>>23)-127;
int frac_ = (uf&0x007fffff)|0x00800000;
if(!(uf&0x7fffffff)) return 0;

if(exp_ > 31) return 0x80000000;
if(exp_ < 0) return 0;

if(exp_ > 23) frac_ <<= (exp_-23);
else frac_ >>= (23-exp_);

if(!((frac_>>31)^s_)) return frac_;
else if(frac_>>31) return 0x80000000;
else return ~frac_+1;
}

``````

1. 超哥说道：
Sogou Explorer Windows 10

牛逼