In the previous article, we have discussed Program to Find All Non Repeated Characters in a String
Given a number and the bit position, the task is to get the bit that is present at that position (in the binary representation of a number).
Bitwise & Operator:
If both bits are 1, sets each bit to 1.
Examples:
Example1:
Input:
Given Number = 2 Bit position(in range 0-31)= 0
Output:
The bit present at the given position{ 0 } for a given number is 0
Example2:
Input:
Given Number = 14 Bit position(in range 0-31)= 3
Output:
The bit present at the given position{ 3 } for a given number is 1
Program to Get nth Bit of a Number in Python
Below are the ways to get the bit that is present at that position (in the binary representation of a number) in python:
Method #1: Using Bitwise &(and) Operator (Static Input)
Approach:
- Give the number as static input and store it in a variable.
- Give the bit position that you need to get the bit value at that position as static input and store it in another variable.
- Apply the left shift operator to 1 and the above-given bit position and store it in another variable.
- Apply bitwise & operation for the given number and the above result and store it in another variable say bit_val.
- Check if the above result bit_val is greater than 0 using the if conditional statement.
- If the statement is true, then print “The bit present at the given position for a given number is 1”.
- Else print “The bit present at the given position for a given number is 0”.
- The Exit of the Program.
Note: ‘0’ indexing .Give the bit position in range(0-31).
Below is the implementation:
# Give the number as static input and store it in a variable. gvn_numb = 2 # Give the bit position that you need to get the bit value at that position as static input # and store it in another variable. bitpositin = 0 # Apply the left shift operator to 1 and the above-given bit position and # store it in another variable. numbr_bit = (1 << bitpositin) # Apply bitwise & operation for the given number and the above result and # store it in another variable say bit_val. bit_val = gvn_numb & numbr_bit # Check if the above result bit_val is greater than 0 using the if conditional statement. if (bit_val > 0): # If the statement is true, then print "The bit present at the given position is 1". print( "The bit present at the given position{", bitpositin, "} for a given number is 1") else: # Else print "The bit present at the given position is 0". print( "The bit present at the given position{", bitpositin, "} for a given number is 0")
Output:
The bit present at the given position{ 0 } for a given number is 0
Method #2: Using Bitwise &(and) Operator (User Input)
Approach:
- Give the number as user input using the int(input()) function and store it in a variable.
- Give the bit position that you need to get the bit value at that position as user input using the int(input()) function and store it in another variable.
- Apply the left shift operator to 1 and the above-given bit position and store it in another variable.
- Apply bitwise & operation for the given number and the above result and store it in another variable say bit_val.
- Check if the above result bit_val is greater than 0 using the if conditional statement.
- If the statement is true, then print “The bit present at the given position for a given number is 1”.
- Else print “The bit present at the given position for a given number is 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 = ")) # Give the bit position that you need to get the bit value at that position as user input # using the int(input()) function and store it in another variable. bitpositin = int(input("Enter some random number = ")) # Apply the left shift operator to 1 and the above-given bit position and # store it in another variable. numbr_bit = (1 << bitpositin) # Apply bitwise & operation for the given number and the above result and # store it in another variable say bit_val. bit_val = gvn_numb & numbr_bit # Check if the above result bit_val is greater than 0 using the if conditional statement. if (bit_val > 0): # If the statement is true, then print "The bit present at the given position is 1". print( "The bit present at the given position{", bitpositin, "} for a given number is 1") else: # Else print "The bit present at the given position is 0". print( "The bit present at the given position{", bitpositin, "} for a given number is 0")
Output:
Enter some random number = 14 Enter some random number = 3 The bit present at the given position{ 3 } for a given number is 1
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.