- 注册时间
- 2004-10-2
- 最后登录
- 1970-1-1
|
- #include <stdio.h>
- typedef unsigned int uint ;
- uint add(uint a,uint b){
- uint c,s;
- c=a&b; //And
- s=a^b; //Xor
- if (!c)return s;
- else return add(s,c<<1);
- }
- void main(){
- uint a;
- a=add(100,98);
- printf("%u",a);
- }
复制代码 小改一下就定义了减法。。- #include <stdio.h>
- typedef unsigned int uint ;
- uint minus(uint a,uint b){
- uint c,s;
- c=~a&b; //借位
- s=a^b; //Xor
- if (!c)return s;
- else return minus(s,c<<1);
- }
复制代码
以上是递归定义。。
如你喜欢也可改成循环。。不过。。有时候循环要考虑到赋值顺序。。比较罗嗦。。- #include <stdio.h>
- typedef unsigned int uint ;
- uint add(uint a,uint b){
- uint c,s;
- c=a&b; //And
- s=a^b; //Xor
- for(;c;a=s,b=c<<1);
- return s;
- }
复制代码 |
|