Background
In various problem-solving techniques, we often need to find the i'th bit of a number. For eg., lets say we have the number 5 , it's binary notation is 101. If I want to get 0th bit it is 1, 1st bit is 0 and 2nd bit is again 1. In this post, we will see how to get the ith bit for a given number in Python.
Use of getting i'th bit
Before we see how to get ith bit let's try to understand a problem-solving use case where we can use this logic. Consider the following problem statement:
Given an array of integers, "data" , find all possible subsets of data, including the empty set.
The question is basically asking us to find all possible subsets of a given array of integers.
For e.g, consider input as [2, 5, 7] the possible combinations are
- i=0: {}
- i=1: {2}
- i=2: {5}
- i=3: {2,5}
- i=4: {7}
- i=5: {2,7}
- i=6: {5,7}
- i=7: {2,5,7}
The total combinations possible for a given list is 2^N where N is the length of the array. In the above case since N=3 (length of input array) the expected combinations are 2^3 = 8 which is exactly what you see above.
Now to compute these we iterate from i=0 to 2^N-1 which in this case will be from 0 to 7.
For each of these numbers, we can find the binary notation and then check if the binary bit is 1 for a given index and include it in the combination.
For eg., let's take i=3, binary notation is 110 which means we should consider numbers from 1st and 2nd position of the original array. Since the original array was [2,5,7] and we are considering 1st and 2nd position arrays the subset is {2,5} which is what you see in above combination list as well.
For all "i" values subsets computed are shown below:
Now let's see the Python code to get the ith bit for a number.
NOTE: In python for computing 2^N we do 2 ** N, In Java you would do Math.pow(2, N).
How to get "i" th bit of a number in Python
We will test the same example we took above when num =3 and we want to get all bits of it to decide which position data from the original array to include in the current subset. Python code for the above is as follows:
def get_ith_bit(num, i):
# Shift the operand specified number of bits to the left
temp = (1 << i)
temp = temp & num
if temp == 0:
return 0
return 1
print(get_ith_bit(3, 0))
print(get_ith_bit(3, 1))
print(get_ith_bit(3, 2))
Output is:
which is 110 in binary as we saw before as well.
Complete code
The complete code for the problem statement is as follows:
PS: Given an array of integers, "data" , find all possible subsets of data, including the empty set.
def get_ith_bit(num, i):
# Shift the operand specified number of bits to the left
temp = (1 << i)
temp = temp & num
if temp == 0:
return 0
return 1
def find_all_subsets(data):
subsets = []
if not data:
return [[]]
else:
combinations = 2 ** len(data)
for i in range(0, combinations):
subset = set()
for j in range(0, len(data)):
if get_ith_bit(i, j) == 1 and data[j] not in subset:
subset.add(data[j])
if i == 0:
subsets.append([])
else:
subsets.append(list(subset))
return subsets
print(find_all_subsets([2,5,7]))
Output:
[[], [2], [5], [2, 5], [7], [2, 7], [5, 7], [2, 5, 7]]
Related links