In the previous article, we have discussed Python Program to Toggle Bits of a Number Except First and Last bits
Given a number and the task is to print all the bitwise AND set of a given number.
For some number I the bitwise AND set of a number N is all feasible numbers x smaller than or equal to N such that N & I equals x.
Examples:
Example1:
Input:
Given Number = 6
Output:
The all bitwise AND set of a given number{ 6 } : 0 2 4 6
Explanation:
Iterating from 0 to 6 so 0 & 6 = 0 1 & 6 = 0 2 & 6 = 2 3 & 6 = 2 4 & 6 = 4 5 & 6 = 4 6 & 6 = 6 Hence the all bitwise AND set of a given number = 0 2 4 6 (removing duplicates)
- Python Program to Swap all Odd and Even Bits of a Number
- Python Program to Find the Missing Number in an array/list
- Python Program to Find Whether a Number is a Power of Two
Example2:
Input:
Given Number = 3
Output:
The all bitwise AND set of a given number{ 3 } : 0 1 2 3
Program Print Bitwise AND set of a Number N in Python
Below are the ways to print all the bitwise AND set of a given number in Python:
- Using For Loop (Static Input)
- Using For loop (User Input)
- Using While Loop (Static Input)
- Using While loop (User Input)
Method #1: Using For Loop (Static Input)
Approach:
- Give the number as static input and store it in a variable.
- Loop till the given number using the for loop.
- Inside the Loop, apply AND operation for the given number and the iterator value and store it in another variable.
- Check if the above result is equal to the iterator value using the if conditional statement.
- If the statement is true, then print the iterator value separated by spaces.
- The Exit of the Program.
Below is the implementation:
# Give the number as static input and store it in a variable. gvn_numb = 6 # Loop till the given number using the for loop. print("The all bitwise AND set of a given number{", gvn_numb, "} : ") for itr in range(gvn_numb + 1): # Apply AND operation for the given number and the iterator value and store it in # another variable. p = gvn_numb & itr # Check if the above result is equal to the iterator value using the if conditional # statement. if (p == itr): # If the statement is true, then print the iterator value separated by spaces. print(itr, " ", end="")
Output:
The all bitwise AND set of a given number{ 6 } : 0 2 4 6
Method #2: Using For loop (User Input)
Approach:
- Give the number as user input using the int(input()) function and store it in a variable.
- Loop till the given number using the for loop.
- Inside the Loop, apply AND operation for the given number and the iterator value and store it in another variable.
- Check if the above result is equal to the iterator value using the if conditional statement.
- If the statement is true, then print the iterator value separated by spaces.
- The Exit of the Program.
Below is the implementation:
# Give the number as user input using the int(input()) function and # store it in a variable. gvn_numb = int(input("Enter some random number = ")) # Loop till the given number using the for loop. print("The all bitwise AND set of a given number{", gvn_numb, "} : ") for itr in range(gvn_numb + 1): # Apply AND operation for the given number and the iterator value and store it in # another variable. p = gvn_numb & itr # Check if the above result is equal to the iterator value using the if conditional # statement. if (p == itr): # If the statement is true, then print the iterator value separated by spaces. print(itr, " ", end="")
Output:
Enter some random number = 3 The all bitwise AND set of a given number{ 3 } : 0 1 2 3
Method #3: Using While Loop (Static Input)
Approach:
- Give the number as static input and store it in a variable.
- Assign the given number to the variable say tempry.
- Loop till the above variable tempry is not equal to 0 using the while loop.
- Inside the Loop, Print the value of tempry separated by spaces.
- Apply AND operation for the tempry -1 and given number and store it in the same variable tempry.
- Exit the loop.
- Print 0.
- The Exit of the Program.
Below is the implementation:
# Give the number as static input and store it in a variable. gvn_numb = 10 # Assign the given number to the variable say tempry. tempry = gvn_numb print("The all bitwise AND set of a given number{", gvn_numb, "} : ") # Loop till the above variable tempry is not equal to 0 using the while loop. while(tempry != 0): # Inside the Loop, Print the value of tempry separated by spaces. print(tempry, end=" ") # Apply AND operation for the tempry -1 and given number and store it in the same variable # tempry. tempry = (tempry - 1) & gvn_numb # Exit the loop. # Print 0. print("0")
Output:
The all bitwise AND set of a given number{ 10 } : 10 8 2 0
Method #4: Using While loop (User Input)
Approach:
- Give the number as user input using the int(input()) function and store it in a variable.
- Assign the given number to the variable say tempry.
- Loop till the above variable tempry is not equal to 0 using the while loop.
- Inside the Loop, Print the value of tempry separated by spaces.
- Apply AND operation for the tempry -1 and given number and store it in the same variable tempry.
- Exit the loop.
- Print 0.
- The Exit of the Program.
Below is the implementation:
# Give the number as user input using the int(input()) function and store it in a variable. gvn_numb = int(input("Enter some random number = ")) # Assign the given number to the variable say tempry. tempry = gvn_numb print("The all bitwise AND set of a given number{", gvn_numb, "} : ") # Loop till the above variable tempry is not equal to 0 using the while loop. while(tempry != 0): # Inside the Loop, Print the value of tempry separated by spaces. print(tempry, end=" ") # Apply AND operation for the tempry -1 and given number and store it in the same variable # tempry. tempry = (tempry - 1) & gvn_numb # Exit the loop. # Print 0. print("0")
Output:
Enter some random number = 7 The all bitwise AND set of a given number{ 7 } : 7 6 5 4 3 2 1 0
If you wanna write simple python programs as a part of your coding practice refer to numerous Simple Python Program Examples existing and learn the approach used.
- Python Program to Toggle the Last m Bits
- Python Program for Swapping Three Variables Without Using any Temporary Variable.
- Python Program to Toggle First and Last Bits of a Number
- Python Program to Toggle Bits of a Number Except First and Last bits