- 没有解释思路,因为基本都是看题目要求用几个符号凑凑就好了,除了leftBitCount这个写了好久。虽然是上学期的实验,但是我依然记得这个leftBitCount,好不容易测试对了吧结果符号数超了一个,但是看来看去也是最简版本了,为了减一个符号磕了好久。
- bitXor
/* * bitXor - x^y using only ~ and & * Example: bitXor(4, 5) = 1 * Legal ops: ~ & * Max ops: 14 * Rating: 1 */ int bitXor(int x, int y) { int a,b; a=~x&y; b=x&~y; a=~(~a&~b); return a; }
-
minusOne
/* * minusOne - return a value of -1 * Legal ops: ! ~ & ^ | + << >> * Max ops: 2 * Rating: 1 */ int minusOne(void) { int a=0; a=~a; return a; }
-
leastBitPos
/* * 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: 6 * Rating: 2 */ int leastBitPos(int x) { int y=~x; y=y+0x1; y=x&y; return y; }
-
allEvenBits
/* * allEvenBits - return 1 if all even-numbered bits in word set to 1 * Examples allEvenBits(0xFFFFFFFE) = 0, allEvenBits(0x55555555) = 1 * Legal ops: ! ~ & ^ | + << >> * Max ops: 12 * Rating: 2 */ int allEvenBits(int x) { int a=0x55; a+=a<<8; a+=a<<16; a=a+~(a&x)+1; return !a; }
-
byteSwap
/* * byteSwap - swaps the nth byte and the mth byte * Examples: byteSwap(0x12345678, 1, 3) = 0x56341278 * byteSwap(0xDEADBEEF, 0, 2) = 0xDEEFBEAD * You may assume that 0 <= n <= 3, 0 <= m <= 3
lab1
最新推荐文章于 2024-10-07 14:59:56 发布