덧셈 & 나눗셈

7829 단어 &
 1 #include <stdio.h>

 2 

 3 //   

 4 int div(int loperand, int roperand)

 5 {

 6     int cnt = 0;

 7     while (loperand > roperand)

 8     {

 9         cnt++;

10         loperand -= roperand;

11     }

12 

13     return cnt;

14 }

15 

16 //    

17 int getRemainder(int loperand, int roperand)

18 {

19     while (loperand > roperand)

20         loperand -= roperand;

21 

22     return loperand;

23 }

24 

25 //   

26 unsigned int add(unsigned int loperand, unsigned int roperand)

27 {

28     int currentbit = 0, carrybit = 0;

29     unsigned int res = 0;

30     int cnt = 0;

31     int len = sizeof(loperand) << 3;

32 

33     while (cnt < len)

34     {

35         int tmp = carrybit;     //        

36         carrybit = 0;

37         //        

38         currentbit = (loperand & 0x01) ^ (roperand & 0x01);

39         //             

40         if (currentbit == 0 && (loperand & 0x01) == 1)

41             carrybit = 1;

42         else if (currentbit == 1 && tmp == 1)

43             carrybit = 1;

44         currentbit ^= tmp;

45         //     

46         res |= (currentbit << cnt);

47         cnt++;

48         loperand >>= 1, roperand >>= 1;

49     }

50 

51     return res;

52 }

53 int main()

54 {

55     int num1 = 65535;

56     int num2 = 65535;

57 

58     printf("%d + %d = %d", num1, num2, add(num1, num2));

59 

60     return 0;

61 }

좋은 웹페이지 즐겨찾기