计算机基础实验_二进制bits

/*

*  lsbZero - set 0 to the least significant bit of x

*  Example: lsbZero(0x87654321) = 0x87654320

*  Legal ops: ! ~ & ^ | + << >>

*  Max ops: 5

*  Rating: 1

*/

int lsbZero(int x) {

	//x右移一位再左移一位实现把最低有效位置0

	x = x >> 1;

	x = x << 1;

	return x;

}

/*

* byteNot - bit-inversion to byte n from word x

*  Bytes numbered from 0 (LSB) to 3 (MSB)

*  Examples: getByteNot(0x12345678,1) = 0x1234A978

*  Legal ops: ! ~ & ^ | + << >>

*  Max ops: 6

*  Rating: 2

*/

int byteNot(int x, int n) {

	//x第n个字节每位都和1异或实现取反

	int y = 0xff;

	n = n << 3;//n=n*8

	y = y << n;

	x = (x ^ y);//1异或任何值,任何值取反

	return x;

}

/*

*  byteXor - compare the nth byte of x and y, if it is same, return 0, if not, return 1

*  example: byteXor(0x12345678, 0x87654321, 1) = 1

*              byteXor(0x12345678, 0x87344321, 2) = 0

*  Legal ops: ! ~ & ^ | + << >>

*  Max ops: 20

*  Rating: 2

*/

int byteXor(int x, int y, int n) {

	//把x和y的第n个字节取出来异或,再转换为逻辑的0和1

	n = n << 3;

	x = x >> n;

	y = y >> n;

	x = x & (0xff);

	y = y & (0xff);

	return !!(x ^ y);

}

/*

*  logicalAnd - x && y

*  Legal ops: ! ~ & ^ | + << >>

*  Max ops: 20

*  Rating: 3

*/

int logicalAnd(int x, int y) {

	//把x和y分别转化为逻辑的0和1,再相与

	x = (!(!x)) & (!(!y));

	return x;

}

/*

*  logicalOr - x || y

*  Legal ops: ! ~ & ^ | + << >>

*  Max ops: 20

*  Rating: 3

*/

int logicalOr(int x, int y) {

	//把x和y分别转化为逻辑的0和1,再相或

	x = (!(!x)) | (!(!y));

	return x;

}

/*

* rotateLeft - Rotate x to the left by n

*  Can assume that 0 <= n <= 31

*  Examples: rotateLeft(0x87654321,4) = 0x76543218

*  Legal ops: ~ & ^ | + << >> !

*  Max ops: 25

*  Rating: 3

*/

int rotateLeft(int x, int n) {

	//先构造低n位为1,高(32-n)位为0的数z,x左移n位后的数加上x右移(32-n)位的数&z即可

	int z;

	z = ~(((1 << 31) >> 31) << n);

	int y = x;

	y = y >> (32 - n);

	y = y & z;

	x = x << n;

	x += y;

	return x;

}

/*

* parityCheck - returns 1 if x contains an odd number of 1's

*  Examples: parityCheck(5) = 0, parityCheck(7) = 1

*  Legal ops: ! ~ & ^ | + << >>

*  Max ops: 20

*  Rating: 4

*/

int parityCheck(int x) {

	//每次将数的低半数位与高半数位比较,再把y右移31位,最后把y转化为逻辑的0和1

	//只在乎前一半的计算情况,最后将计算结果右移31位即可

	int y;

	y = x << 16;

	y = y ^ x;

	y = y ^ (y << 8);

	y = y ^ (y << 4);

	y = y ^ (y << 2);

	y = y ^ (y << 1);

	y = y >> 31;

	return !(!y);

}

/*

* mul2OK - Determine if can compute 2*x without overflow

*  Examples: mul2OK(0x30000000) = 1

*            mul2OK(0x40000000) = 0

*

*  Legal ops: ~ & ^ | + << >>

*  Max ops: 20

*  Rating: 2

*/

int mul2OK(int x) {

	//把x第31位和30位分别和1做按位与,再异或,再和1异或

	int y;
	y = x;
	y = y << 1;
	y = y ^ x;
	y = y >> 31;
	return !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) {//***向0舍入,正数不用考虑,除不尽的负数要+1

	//左移一位再+x即x*3,右移一位即/2

	int y = (x << 1) + x;

	y = (y >> 1) + (((y >> 31) & 1) & (y & 1));//判断是否为负数且最后一位是否为1,1则表示除不尽

	return y;

}

/*

* subOK - Determine if can compute x-y without overflow

*  Example: subOK(0x80000000,0x80000000) = 1,

*            subOK(0x80000000,0x70000000) = 0,

*  Legal ops: ! ~ & ^ | + << >>

*  Max ops: 20

*  Rating: 3

*/

int subOK(int x, int y) {

	//只有(负数-正数)和(正数-负数)的情况下才会有溢出情况
	//溢出情况(最高位的机器码):1,0->0 和 0,1->1

	int z = x - y;//得到机器码相减的结果
	//先判断x,y的最高位是否不同,再判断x,z的最高位是否不同,都符合则溢出
	int m = (((x ^ y) >> 31) & 1) & (((x ^ z) >> 31) & 1);
	return !(m);
}

/*

* absVal - absolute value of x

*  Example: absVal(-1) = 1.

*  You may assume -TMax <= x <= TMax

*  Legal ops: ! ~ & ^ | + << >>

*  Max ops: 10

*  Rating: 4

*/

int absVal(int x) {
	//***补码是~x+1,由补码转换为正数依旧是~x+1
	//x最高位为0时就是x,最高位为1时是~x+1

	int y = x >> 31;
	x = (y & (~x + 1)) + ((~y) & x);
	return x;

}

/*

* float_abs - Return bit-level equivalent of absolute value of 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_abs(unsigned uf) {

	int x = uf & (~(1 << 31));

	if (x > 0x7f800000)

	{

		return uf;

	}

	else return x;

}

/*

* 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) {

	unsigned num = 0x80000000;

	int x = (uf & 0x007fffff) ^ 0x00800000;

	int order = 0;

	order = (uf & 0x7f800000) >> 23;

	if (order > 158) {

		return num;

	}

	if (order < 127) return 0;

	else if (((uf >> 31) & 1) == 1) {

		if (order > 150) {

			return ~(x << (order - 150)) + 1;

		}

		else return ~(x >> (150 - order)) + 1;

	}

	else {

		if (order > 150) return x << (order - 150);

		else return x >> (150 - order);

	}

}
  • 4
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值