- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python program to extract ‘k’ bits from a given position?
This function is use to extract k bits from pos position and returns the extracted value. Here we use python slicing technique.
Input:: number=170 K=5 Pos=2 Output=21
Extractionbit(no,k,pos) /*user input number is stored in variable no, extracted bit is stored in variable k and the position of bit is pos. */ Step 1 : first convert the number into its binary form using bin(). Step 2 : remove the first two character. Step 3 : then extracting k bits from starting position pos from right.so, the ending index of the extracting substring is e=len(bi)-pos and starting index=e-k+1 Step 4 : extract k bit sub-string. Step 5 : convert extracted sub-string into decimal again.
# python program to extract ‘k’ bits from a given position in a number def extractedbits(no,k,pos): bi = bin(no) bi = bi[2:] e = len(bi) - pos s = e - k + 1 substr = bi[s : e+1] print ("FINAL RESULT ::>",int(substr,2)) # Driver program if __name__ == "__main__": no=int(input("Enter number ::>")) k = int(input("Enter k bit's ::>")) pos = int(input("Enter position ::>")) extractedbits(no,k,pos)
Enter number ::>170 Enter k bit's ::>5 Enter position ::>2 FINAL RESULT ::>21
- Java program to extract ‘k’ bits from a given position
- Python program to extract characters in given range from a string list
- Python program to extract Keywords from a list
- Python – Extract Rear K digits from Numbers
- Python – Extract element from a list succeeded by K
- Python program to reverse an array up to a given position
- Extract only characters from given string in Python
- Python Program to Extract Elements from a List in a Set
- Program to reverse an array up to a given position in Python
- C program to rotate the bits for a given number
- Python Program to Extract Strings with at least given number of characters from other list
- How to extract substring from a sting starting at a particular position in MySQL?
- Program to find out is a point is reachable from the current position through given points in Python
- Python program to find hash from a given tuple
- Python Program to get K length groups with given summation