Skip to content

Commit 3996b3b

Browse files
authored
Merge pull request #183 from BrianLusina/feat/bit-manipulation
feat(bit-manipulation): sum two integers
2 parents b3facec + 1e3291d commit 3996b3b

11 files changed

+143
-0
lines changed

DIRECTORY.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,8 @@
451451
* [Test Number Of One Bits](https://github.com/BrianLusina/PythonSnips/blob/master/bit_manipulation/number_of_1_bits/test_number_of_one_bits.py)
452452
* Single Number
453453
* [Test Single Number](https://github.com/BrianLusina/PythonSnips/blob/master/bit_manipulation/single_number/test_single_number.py)
454+
* Sum Two Integers
455+
* [Test Sum Of Two Integers](https://github.com/BrianLusina/PythonSnips/blob/master/bit_manipulation/sum_two_integers/test_sum_of_two_integers.py)
454456

455457
## Cryptography
456458
* Atbash Cipher
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# Sum of Two Integers
2+
3+
Given two integers a and b, return the sum of the two integers without using the operators + and -.
4+
5+
6+
## Examples
7+
8+
Example 1:
9+
```text
10+
Input: a = 1, b = 2
11+
Output: 3
12+
```
13+
14+
Example 2:
15+
```text
16+
Input: a = 2, b = 3
17+
Output: 5
18+
```
19+
20+
## Constraints
21+
22+
- -1000 <= a, b <= 1000
23+
24+
## Topics
25+
26+
- Math
27+
- Bit Manipulation
28+
29+
## Solution
30+
31+
We will use the bitwise manipulation pattern to calculate the sum of two integers without using the arithmetic operators.
32+
To calculate the desired sum, we'll use the bitwise operations.
33+
34+
Let's dive into the logic to get a better idea of how to use bitwise operations for this task:
35+
36+
- Bitwise XOR (`^`) operation: This operation allows us to calculate the sum of corresponding bits in a and b without
37+
considering the carry (if any).
38+
39+
- Bitwise AND (`&`) operation: The AND operation will help us determine where carry propagation is necessary. By applying
40+
this operation to a and b, we can identify which bit positions require carry consideration.
41+
42+
- Iterative Process: The algorithm functions iteratively, much like how binary addition is carried out. We will start
43+
with the least significant bits and advance toward the most significant bits of a and b. At each bit position, we
44+
calculate the sum while considering the carry from the previous bit's addition. This ensures that any carry is
45+
appropriately propagated throughout the process.
46+
47+
The algorithm to obtain the sum of the given integers is as follows:
48+
49+
1. To limit the result to 32-bits, we set a mask variable to `0xFFFFFFFF`.
50+
2. Start a loop that continues while b is not equal to 0. We calculate the sum of the two integers without carrying, by
51+
taking XOR and then AND the result with the mask.
52+
3. For calculating the carry which needs to be added to the next pair of integers, we take the AND of a and b,
53+
leftshifting the result by 1-bit, and then AND this result with the mask.
54+
4. We update a and b with the new values for the next iteration, which are the result and carry calculated in steps 2
55+
and 3, respectively.
56+
5. At the end, to handle overflow when performing the addition of two integers, we have a condition that checks if the
57+
result exceeds the maximum value of a signed 32-bit integer. If it does, we return the two’s complement of the result
58+
to ensure that the returned value remains within the valid range.
59+
60+
![Solution 1](./images/solutions/sum_two_integers_solution_1.png)
61+
![Solution 2](./images/solutions/sum_two_integers_solution_2.png)
62+
![Solution 3](./images/solutions/sum_two_integers_solution_3.png)
63+
![Solution 4](./images/solutions/sum_two_integers_solution_4.png)
64+
![Solution 5](./images/solutions/sum_two_integers_solution_5.png)
65+
![Solution 6](./images/solutions/sum_two_integers_solution_6.png)
66+
![Solution 7](./images/solutions/sum_two_integers_solution_7.png)
67+
68+
### Time Complexity
69+
70+
The time complexity of this solution is O(1), because the while loop runs for a maximum of 32 times, since the integers
71+
are represented by 32−bits. Each iteration does bitwise operations, which takes constant O(1), time hence making the
72+
total time complexity as O(1)*O(32) = O(32), which is equivalent to O(1).
73+
74+
### Space Complexity
75+
76+
The solution uses fixed numbers of variables to compute the result. Hence the space complexity is constant. O(1).
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
def integer_addition(a: int, b: int):
2+
# Mask to limit the result to 32-bits (the size of an integer in Python)
3+
mask = 0xFFFFFFFF
4+
# Setting maximum integer value
5+
max_int = 0x7FFFFFFF
6+
7+
while b != 0:
8+
# Sum of the integers without carrying
9+
result = (a ^ b) & mask
10+
11+
# Calculate the carry that needs to be added to the next pair of integers
12+
carry = ((a & b) << 1) & mask
13+
14+
# Update a and b to the new values for the next iteration
15+
a = result
16+
b = carry
17+
18+
# We check if the result is greater than the maximum integer value
19+
# Return the result as is if result is not greater than max_int
20+
if a <= max_int:
21+
return a
22+
23+
# If it is, then return the two's complement of the result
24+
else:
25+
return ~(a ^ mask)
26+
27+
28+
def integer_addition_2(a: int, b: int) -> int:
29+
mask = 0xFFFFFFFF
30+
max_int = 2 ** 31 - 1
31+
while b != 0:
32+
sum_ = (a ^ b) & mask
33+
carry = (a & b) & mask
34+
a = sum_
35+
b = carry << 1
36+
return a if a <= max_int else ~(a ^ mask)
51.5 KB
Loading
84.8 KB
Loading
57.4 KB
Loading
69.2 KB
Loading
60.8 KB
Loading
70.1 KB
Loading
46.7 KB
Loading

0 commit comments

Comments
 (0)