Skip to content

Commit 4139b2d

Browse files
committed
refactor(bit_manipulation): rename parameters and resolve conflicts in swap_bits
1 parent 7746511 commit 4139b2d

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

bit_manipulation/binary_swap_bits.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@
66
"""
77

88

9-
def swap_bits(number: int, i: int, j: int) -> int:
9+
def swap_bits(number: int, first: int, second: int) -> int:
1010
"""
11-
Swap the bits at the position i and j (0-indexed from right side)
11+
Swap the bits at the position first and second (0-indexed from right side)
1212
1313
Arguments:
1414
number (int): Non-negative integer whose bits are to be swapped.
15-
i (int): Index of 1st bit to swap.
16-
j (int): Index of 2nd bit to swap.
15+
first (int): Index of 1st bit to swap.
16+
second (int): Index of 2nd bit to swap.
1717
1818
Returns:
1919
int: Integer obtained after swapping of bits.
@@ -42,22 +42,22 @@ def swap_bits(number: int, i: int, j: int) -> int:
4242
...
4343
ValueError: Bit positions MUST be non-negative!
4444
"""
45-
if not all(isinstance(x, int) for x in (number, i, j)):
45+
if not all(isinstance(x, int) for x in (number, first, second)):
4646
raise TypeError("All arguments MUST be integers!")
4747

4848
if number < 0:
4949
raise ValueError("The number MUST be non-negative!")
5050

51-
if i < 0 or j < 0:
51+
if first < 0 or second < 0:
5252
raise ValueError("Bit positions MUST be non-negative!")
5353

5454
# Extraction of Bits
55-
bit_first = (number >> i) & 1
56-
bit_second = (number >> j) & 1
55+
bit_first = (number >> first) & 1
56+
bit_second = (number >> second) & 1
5757

5858
# If bits differ swap them
5959
if bit_first != bit_second:
60-
number ^= (1 << i) | (1 << j)
60+
number ^= (1 << first) | (1 << second)
6161

6262
return number
6363

0 commit comments

Comments
 (0)