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
Python – Bitwise OR among list elements
The bitwise OR operation combines the binary representations of numbers using the "|" operator. In Python, you can perform bitwise OR among all elements in a list using several approaches including iteration, lambda functions, and NumPy.
Understanding Bitwise OR
The bitwise OR operator "|" compares each bit position and returns 1 if at least one of the corresponding bits is 1 ?
# Example: 40 | 50
# 40 in binary: 101000
# 50 in binary: 110010
# Result: 111010 (which is 58 in decimal)
print(f"40 | 50 = {40 | 50}")
print(f"Binary result: {bin(40 | 50)}")
40 | 50 = 58 Binary result: 0b111010
Using Iteration Method
The simplest approach uses a for loop to iterate through all elements ?
# Initialize the list with integer elements
numbers = [40, 50, 90]
# Start with 0 (neutral element for OR operation)
result = 0
# Perform bitwise OR with each element
for num in numbers:
result |= num
print(f"Bitwise OR result: {result}")
print(f"Binary representation: {bin(result)}")
Bitwise OR result: 122 Binary representation: 0b1111010
Using Lambda with reduce()
The functional approach uses reduce() with a lambda function ?
from functools import reduce
# Initialize the list with integer elements
numbers = [40, 50, 90]
# Use lambda function with reduce to perform bitwise OR
result = reduce(lambda a, b: a | b, numbers)
print(f"Bitwise OR result: {result}")
print(f"Binary representation: {bin(result)}")
Bitwise OR result: 122 Binary representation: 0b1111010
Using NumPy Module
NumPy provides a built-in function for bitwise operations ?
import numpy as np
# Initialize the list with integer elements
numbers = [40, 50, 90]
# Use NumPy's bitwise_or.reduce() function
result = np.bitwise_or.reduce(numbers)
print(f"Bitwise OR result: {result}")
Bitwise OR result: 122
Performance Comparison
| Method | Readability | Performance | Dependencies |
|---|---|---|---|
| Iteration | High | Good | None |
| Lambda + reduce() | Medium | Good | functools |
| NumPy | High | Best (large lists) | numpy |
Working Example with Different Values
# Demonstrate with different numbers
data = [12, 25, 30]
print("Individual binary representations:")
for num in data:
print(f"{num}: {bin(num)}")
# Calculate step by step
step1 = 12 | 25 # 12 OR 25
step2 = step1 | 30 # result OR 30
print(f"\nStep-by-step calculation:")
print(f"12 | 25 = {step1}")
print(f"{step1} | 30 = {step2}")
print(f"Final result: {step2}")
Individual binary representations: 12: 0b1100 25: 0b11001 30: 0b11110 Step-by-step calculation: 12 | 25 = 29 29 | 30 = 31 Final result: 31
Conclusion
Use iteration for simple cases and better readability. The reduce() approach is more functional, while NumPy is optimal for large datasets. All methods produce the same result by combining bits using the OR operation.
