第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;
}