《深入理解计算机系统》Lab1:Data Lab(一)

第1关:bitAnd

任务描述

本关任务:补充函数bitAnd(),只用~|实现x&y,将结果return返回。

  • 操作符限制:~|
  • 操作符使用数量限制:8

测试说明

平台会对你编写的代码进行测试:

测试输入:
6
5

预期输出:
4

int bitAnd(int x, int y) 
{ 
	/********* Begin *********/
	int ret;
	x=~x;
	y=~y;
	ret =x|y;
	ret=~ret;
	return ret;

	/********* End *********/	
}

int getByte(int x, int n)
{ 
	return 0;
}

int logicalShift(int x, int n) 
{
	return 0;
}

int bitCount(int x) 
{
	return 0;
}

int bang(int x) 
{
	return 0;
}

int tmin(void) 
{ 
	return 0;
}

int fitsBits(int x, int n) 
{
	return 0;
}

int divpwr2(int x, int n) 
{
	return 0;
}

int negate(int x) 
{
	return 0;
}

int isPositive(int x) 
{
	return 0;
}

int isLessOrEqual(int x, int y) 
{
	return 0;
}

int ilog2(int x) 
{
	return 0;
}

unsigned float_neg(unsigned uf)
{
	return 0;
}

unsigned float_i2f(int x) 
{
	return 0;
}

unsigned float_twice(unsigned uf) 
{
	return 0;
}

 第2关:getByte

 

任务描述

本关任务:补充函数getByte(),取出x中的n号字节,将结果return返回。

  • 编号从低位到高位从0开始
  • 操作符使用数量限制:6

测试说明

平台会对你编写的代码进行测试:

测试输入:
0x12345678
1

预期输出:
0x56

int bitAnd(int x, int y) 
{ 
	return 0;
}

int getByte(int x, int n)
{ 
	/********* Begin *********/
	n=n<<3;
	x=x>>n ;
	return x&0xFF;	
	/********* End *********/	
}

int logicalShift(int x, int n) 
{
	return 0;
}

int bitCount(int x) 
{
	return 0;
}

int bang(int x) 
{
	return 0;
}

int tmin(void) 
{ 
	return 0;
}

int fitsBits(int x, int n) 
{
	return 0;
}

int divpwr2(int x, int n) 
{
	return 0;
}

int negate(int x) 
{
	return 0;
}

int isPositive(int x) 
{
	return 0;
}

int isLessOrEqual(int x, int y) 
{
	return 0;
}

int ilog2(int x) 
{
	return 0;
}

unsigned float_neg(unsigned uf)
{
	return 0;
}

unsigned float_i2f(int x) 
{
	return 0;
}

unsigned float_twice(unsigned uf) 
{
	return 0;
}

 第3关:logicalShift

任务描述

本关任务:补充函数logicalShift(),将x逻辑右移n位(0<=n<=31) ,将结果return返回。

  • 操作符使用数量限制:20

测试说明

平台会对你编写的代码进行测试:

测试输入:
0x87654321
4

预期输出:
0x8765432

int bitAnd(int x, int y) 
{ 
	return 0;
}

int getByte(int x, int n)
{ 
	return 0;	
}

int logicalShift(int x, int n) 
{
	/********* Begin *********/
	unsigned int tmp = x; 
    tmp = tmp>>n; 
    return tmp;

	/********* End *********/
}

int bitCount(int x) 
{
	return 0;
}

int bang(int x) 
{
	return 0;
}

int tmin(void) 
{ 
	return 0;
}

int fitsBits(int x, int n) 
{
	return 0;
}

int divpwr2(int x, int n) 
{
	return 0;
}

int negate(int x) 
{
	return 0;
}

int isPositive(int x) 
{
	return 0;
}

int isLessOrEqual(int x, int y) 
{
	return 0;
}

int ilog2(int x) 
{
	return 0;
}

unsigned float_neg(unsigned uf)
{
	return 0;
}

unsigned float_i2f(int x) 
{
	return 0;
}

unsigned float_twice(unsigned uf) 
{
	return 0;
}

 

第4关:bitCount

任务描述

本关任务:补充函数bitCount(),统计x的二进制表示中1的数量,将结果return返回。

  • 操作符使用数量限制:40

测试说明

平台会对你编写的代码进行测试:

测试输入:5
预期输出:2

int bitAnd(int x, int y) 
{ 
	return 0;	
}

int getByte(int x, int n)
{ 
	return 0;
}

int logicalShift(int x, int n) 
{
	return 0;
}

int bitCount(int x) 
{
	/********* Begin *********/
	unsigned int d = x; 
     
    d = (d&0x55555555)+((d>>1)&0x55555555); 
   
    d = (d&0x33333333)+((d>>2)&0x33333333); 
    
    d = (d&0x0F0F0F0F)+((d>>4)&0x0F0F0F0F); 
     
    d = (d&0x00FF00FF)+((d>>8)&0x00FF00FF); 
     
    d = (d&0x0000FFFF)+((d>>16)&0x0000FFFF); 
     
    return  d;

	/********* End *********/
}

int bang(int x) 
{
	return 0;
}

int tmin(void) 
{ 
	return 0;
}

int fitsBits(int x, int n) 
{
	return 0;
}

int divpwr2(int x, int n) 
{
	return 0;
}

int negate(int x) 
{
	return 0;
}

int isPositive(int x) 
{
	return 0;
}

int isLessOrEqual(int x, int y) 
{
	return 0;
}

int ilog2(int x) 
{
	return 0;
}

unsigned float_neg(unsigned uf)
{
	return 0;
}

unsigned float_i2f(int x) 
{
	return 0;
}

unsigned float_twice(unsigned uf) 
{
	return 0;
}

 第5关:bang

任务描述

本关任务:补充函数bang(),不使用!实现!操作符,将结果return返回。

  • 操作符限制:~ & ^ | + << >>
  • 操作符使用数量限制:12

测试说明

平台会对你编写的代码进行测试:

测试输入:4
预期输出:0

int bitAnd(int x, int y) 
{ 
	return 0; 
}

int getByte(int x, int n)
{ 
	return 0; 
}

int logicalShift(int x, int n) 
{
    return 0;
}

int bitCount(int x) 
{
    return 0;
}

int bang(int x) 
{
	/********* Begin *********/
	x = x |(x>>16); 
    x = x |(x>>8); 
    x = x |(x>>4); 
    x = x |(x>>2);  
    x = x | (x>>1); 
    return (~x)&1;

	/********* End *********/	

}

int tmin(void) 
{ 
	return 0; 
}

int fitsBits(int x, int n) 
{
    return 0;
}

int divpwr2(int x, int n) 
{
    return 0;
}

int negate(int x) 
{
	return 0; 
}

int isPositive(int x) 
{
	return 0; 
}

int isLessOrEqual(int x, int y) 
{
    return 0;
}

int ilog2(int x) 
{
    return 0;
}

unsigned float_neg(unsigned uf)
{
    return 0;
}

unsigned float_i2f(int x) 
{
    return 0;
}

unsigned float_twice(unsigned uf) 
{
    return 0;
}

第6关:tmin

 

任务描述

本关任务:补充函数tmin(),返回补码表示的整型的最小值,将结果return返回。

  • 操作符使用数量限制:4

测试说明

平台会对你编写的代码进行测试:

测试输入:无
预期输出:整型的最小值

int bitAnd(int x, int y) 
{ 
	return 0;	
}

int getByte(int x, int n)
{ 
	return 0; 
}

int logicalShift(int x, int n) 
{
    return 0;
}

int bitCount(int x) 
{
    return 0;
}

int bang(int x) 
{
	return 0; 
}

int tmin(void) 
{ 
	/********* Begin *********/
	return 1<<31; 
	/********* End *********/ 
}

int fitsBits(int x, int n) 
{
    return 0;
}

int divpwr2(int x, int n) 
{
    return 0;
}

int negate(int x) 
{
	return 0; 
}

int isPositive(int x) 
{
	return 0; 
}

int isLessOrEqual(int x, int y) 
{
    return 0;
}

int ilog2(int x) 
{
    return 0;
}

unsigned float_neg(unsigned uf)
{
    return 0;
}

unsigned float_i2f(int x) 
{
    return 0;
}

unsigned float_twice(unsigned uf) 
{
    return 0;
}

 

 

  • 10
    点赞
  • 71
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值