pseudo code c
Example 63 (x)+ 89 (y) => 52 (sum) and carry=1
Add fixed-width decimal integers; N digits.
1: carry ← 0 //initiliazzed caarry as 0
2: for i = 0 to N // i-th bit
3: sumi ← (xi + yi +
carry) % 10 // mod operation if we add 9+3 then sum(i) will be
(9+3)%10=2
4: carry ← (xi + yi + carry) ∕ 10
//div operation carry=(9+3)/10=1
5: end for //end loop
Add fixed-width binary integers; N bits.
1: carry ← 0
2: for i = 0 to N // i-th bit
3: sumi ← (xi + yi +
carry) % 2 // mod operation
4: carry ← (xi + yi + carry) ∕ 2
// division operation
5: end for
Add fixed-width integers in hexadecimal; N digits.
1: carry ← 0
2: for i = 0 to N − 1 // i-th digit
3: sumi ← (xi + yi +
carry) % 16 //mod
4: carry ← (xi + yi + carry) ∕ 16
//division
5: end for
Subtract fixed-width decimal integers; N digits.
1: borrow ← 0
2: for i = 0 to N − 1 do // i-th digit
3: if yi ≤ xi then //subtract Y
from X
4: differencei ← (xi −
yi)
5: else if i = N − 1 then
6: borrow ← 1 // Result will be wrong!
7: xi ← xi + 10
8: differencei ← (xi −
yi)
9: else
10: j ← i + 1
11: while xj = 0 do
12: j ← j + 1
13: end while
14: xj ← xj − 1
15: j ← j − 1
16: while j > i do
17: xj ← xj + 10 − 1
18: j ← j − 1
19: end while
20: differencei ← (10+ xi −
yi)
21: end if
22: end for
Subtract fixed-width binary integers; N digits.
1: borrow ← 0
2: for i = 0 to N − 1 do // i-th digit
3: if yi ≤ xi then //subtract Y
from X
4: differencei ← (xi −
yi)
5: else if i = N − 1 then
6: borrow ← 1 //Result will be wrong!
7: xi ← xi + 2 // if we subtract
1 from 0 we cannot so we have to take borrow from left side now
//will be 0+(2 if binary ,10 for decibal and 16 for
hexadecimal)
8: differencei ← (xi −
yi)
9: else
10: j ← i + 1
11: while xj = 0 do
12: j ← j + 1
13: end while
14: xj ← xj − 1
15: j ← j − 1
16: while j > i do
17: xj ← xj + 2− 1
18: j ← j − 1
19: end while
20: differencei ← (2+ xi −
yi)
21: end if
22: end for
Subtract fixed-width hexadecimal integers; N digits.
1: borrow ← 0
2: for i = 0 to N − 1 // i-th digit
3: if yi ≤ xi then //subtract Y
from X
4: differencei ← (xi −
yi)
5: else if i = N − 1 then
6: borrow ← 1 //Result will be wrong!
7: xi ← xi + 16
8: differencei ← (xi −
yi)
9: else
10: j ← i + 1
11: while xj = 0 do
12: j ← j + 1
13: end while
14: xj ← xj − 1
15: j ← j − 1
16: while j > i do
17: xj ← xj + 16 − 1
18: j ← j − 1
19: end while
20: differencei ← (16 + xi −
yi)
21: end if
22: end for
if my answer help you then please upvote me.
Get Answers For Free
Most questions answered within 1 hours.