位操作求解

nwpu033916 2006-04-12 04:02:08
*
* STEP 3: Modify the following functions according the coding rules.
*/



/*
* bitAnd - x&y using only ~ and |
* Example: bitAnd(6, 5) = 4
* Legal ops: ~ |
* Max ops: 8
* Rating: 1
*/
int bitAnd(int x, int y) {

return 2;

}






/*
* bitOr - x|y using only ~ and &
* Example: bitOr(6, 5) = 7
* Legal ops: ~ &
* Max ops: 8
* Rating: 1
*/
int bitOr(int x, int y) {

return 2;

}






/*
* isZero - returns 1 if x == 0, and 0 otherwise
* Examples: isZero(5) = 0, isZero(0) = 1
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 2
* Rating: 1
*/
int isZero(int x) {

return 2;

}






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

return 2;

}






/*
* TMax - return maximum two's complement integer
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 4
* Rating: 1
*/
int tmax(void) {

return 2;

}







/*
* bitXor - x^y using only ~ and &
* Example: bitXor(4, 5) = 1
* Legal ops: ~ &
* Max ops: 14
* Rating: 2
*/
int bitXor(int x, int y) {

return 2;

}






/*
* 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 2;

}






/*
* isEqual - return 1 if x == y, and 0 otherwise
* Examples: isEqual(5,5) = 1, isEqual(4,5) = 0
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 5
* Rating: 2
*/
int isEqual(int x, int y) {

return 2;

}






/*
* negate - return -x
* Example: negate(1) = -1.
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 5
* Rating: 2
*/
int negate(int x) {

return 2;

}






/*
* isPositive - return 1 if x > 0, return 0 otherwise
* Example: isPositive(-1) = 0.
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 8
* Rating: 3
*/
int isPositive(int x) {

return 2;

}
...全文
244 11 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
nwpu033916 2006-04-12
  • 打赏
  • 举报
回复
分数是怎么得啊? 我是新手.
zCheng 2006-04-12
  • 打赏
  • 举报
回复
int bitAnd(int x, int y) {
return ~(~x | ~y);
}

int bitOr(int x, int y) {
return ~(~x & ~y);
}

int isZero(int x) {
return !x;
}

int minusOne(void) {
return ~0;
}

/*
* TMax - return maximum two's complement integer
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 4
* Rating: 1
*/
int tmax(void) {
/* 暂时做不出来 */
}

int bitXor(int x, int y) {
return ~(~x & ~ y) & ~(x & y);
}

int getByte(int x, int n) {
/* 暂时做不出来 */
}

int isEqual(int x, int y) {
return !(x ^ y);
}

int negate(int x) {
return (~x + 1) ;
}

int isPositive(int x) {
/* 暂时做不出来
return !(x >> 31); 或 return !(x >> 15);
*/
}
jixingzhong 2006-04-12
  • 打赏
  • 举报
回复
/*
* bitOr - x|y using only ~ and &
* Example: bitOr(6, 5) = 7
* Legal ops: ~ &
* Max ops: 8
* Rating: 1
*/
int bitOr(int x, int y) {

return ~((~x)&(~y));

}

====================
下面的操作类似 ~
就是用 合法的操作实现预定的操作,
楼主把关系转换一下就好了 ~
jixingzhong 2006-04-12
  • 打赏
  • 举报
回复
return ~((~x)|(~y)); /*x&y == ~((~x)|(~y))*/
jixingzhong 2006-04-12
  • 打赏
  • 举报
回复
好像出错了 ~
汗 ~

德摩根定律用错地方了 ~ -_- bbbbbbbbb
jixingzhong 2006-04-12
  • 打赏
  • 举报
回复
/*
* bitAnd - x&y using only ~ and |
* Example: bitAnd(6, 5) = 4
* Legal ops: ~ |
* Max ops: 8
* Rating: 1
*/
int bitAnd(int x, int y) {

return ~(x|y); /*x&y == ~(x|y)*/

}
jixingzhong 2006-04-12
  • 打赏
  • 举报
回复
呵呵, 看了一下,
其实也不难,
基本就是一些操作的转换 ~

逻辑运算的转换而已 ~
jixingzhong 2006-04-12
  • 打赏
  • 举报
回复
楼主可以去看看 STL 中 bitset 的实现,
所有的位操作这个里面都涉及了 ~
jinjiajie 2006-04-12
  • 打赏
  • 举报
回复
...晕晕的
jixingzhong 2006-04-12
  • 打赏
  • 举报
回复
直接求代码, 转 新手乐园 ~
zez 2006-04-12
  • 打赏
  • 举报
回复
你要做什么??

不问问题,列一堆代码....

33,321

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧