덧셈 & 나눗셈
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 }
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
java&와 &&의 차이 및 실례java&와 &&의 차이: & 비트 연산자이자 논리 연산자이며 & 의 양측은 int일 수도 있고 boolean 표현식일 수도 있다. & 양측이 int일 때 연산자 양측의 수를 이진수로 전환한 다음에 연산을 해야 한다...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.