Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Check if bits in range L to R of two numbers are complement of each other or not in Python
Suppose we have two numbers x and y and a given range (left, right). We have to check whether all bits in range left to right in both the given numbers are complement of each other or not. We have to keep in mind that from right to left, so the least significant bit is considered to be at first position.
So, if the input is like x = 41 y = 54 left = 2 right = 5, then the output will be True, as binary representation of 41 and 54 are 101001 and 110110. The bits in range 2 to 5 of x and y are "1001" and "0110" which are complement of each other.
Approach
To solve this, we will follow these steps ?
- temp := x XOR y
- return true when all bits in range (left, right) of temp is 1, otherwise false
Understanding with Binary Representation
Let's visualize how this works with our example ?
x = 41 # Binary: 101001
y = 54 # Binary: 110110
print(f"x = {x} (Binary: {bin(x)[2:]})")
print(f"y = {y} (Binary: {bin(y)[2:]})")
# XOR operation
temp = x ^ y
print(f"x XOR y = {temp} (Binary: {bin(temp)[2:]})")
# Check bits in positions 2 to 5 (from right, 1-indexed)
print(f"Bits in range 2-5 of x: {bin(x)[2:][-5:-1]}")
print(f"Bits in range 2-5 of y: {bin(y)[2:][-5:-1]}")
x = 41 (Binary: 101001) y = 54 (Binary: 110110) x XOR y = 31 (Binary: 11111) Bits in range 2-5 of x: 1001 Bits in range 2-5 of y: 0110
Implementation
Here's the complete solution ?
def are_all_setbits_in_range(n, left, right):
# Create a mask with 1s in the range [left, right]
val = ((1 << right) - 1) ^ ((1 << (left - 1)) - 1)
# Extract bits in the range
new_value = n & val
# Check if all bits in range are 1
if val == new_value:
return True
return False
def solve(x, y, left, right):
# XOR gives 1 where bits are different (complement)
temp = x ^ y
# Check if all bits in range are 1 (i.e., complement)
return are_all_setbits_in_range(temp, left, right)
# Test with example
x = 41
y = 54
left = 2
right = 5
result = solve(x, y, left, right)
print(f"Are bits in range {left}-{right} complement? {result}")
Are bits in range 2-5 complement? True
How It Works
The algorithm works in two steps ?
- XOR Operation: When we XOR two numbers, we get 1 where bits are different and 0 where they are same. If bits are complements, XOR will produce 1.
- Range Check: We create a mask to isolate bits in the specified range and check if all are 1s.
Additional Examples
# Test with different cases
test_cases = [
(41, 54, 2, 5), # True - bits are complement
(10, 5, 1, 4), # False - not all bits are complement
(15, 0, 1, 4) # True - 1111 and 0000 are complements
]
for x, y, left, right in test_cases:
result = solve(x, y, left, right)
print(f"x={x}, y={y}, range={left}-{right}: {result}")
print(f" x binary: {bin(x)[2:]:>6}")
print(f" y binary: {bin(y)[2:]:>6}")
print(f" XOR: {bin(x^y)[2:]:>6}")
print()
x=41, y=54, range=2-5: True x binary: 101001 y binary: 110110 XOR: 11111 x=10, y=5, range=1-4: False x binary: 1010 y binary: 101 XOR: 1111 x=15, y=0, range=1-4: True x binary: 1111 y binary: 0 XOR: 1111
Conclusion
This solution uses XOR properties to efficiently check if bits in a specific range are complements. The key insight is that XOR produces 1 when bits differ, making it perfect for complement detection.
