当前位置:编程学习 > C/C++ >>

用位运算实现加法

[cpp]
#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 
#include <math.h> 
#include <WTYPES.H> 
#include <memory.h> 
 
/************************************************************************************************************************************************************
****                                                    lxmuyu
****                                        begin:      2012 11 23
****                                                    
****                                            用位运算实现加法
****    
****                                        end  :      2012 11 23
****   
************************************************************************************************************************************************************/ 
 
int add(int a, int b){ 
    int temp = 0; 
    do{ 
        temp = a; 
        a = a & b; 
        b = b ^ temp; 
        a = a << 1; 
    }while(a != 0); 
     
    return b; 

 
void main(){ 
    int a, b; 
    scanf("%d %d", &a, &b); 
    printf("a + b = %d\r\n", add(a,b)); 
    scanf("%d", a); 

补充:软件开发 , C++ ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,