Check whether kth bit is set or not
Introduction to Check nth bit
To check whether nth bit is set or not, we have to consider the position of nth bit to be set or not. “Check whether nth bit is set or not” refers to the process of determining the state of a specific bit within the binary representation of a number.
In binary, each digit is a bit, and these bits are numbered from right to left, starting with 0. The term “nth bit” indicates the bit at position n in the binary representation of a number.
Welcome to our guide where we explore algorithm involve in checking nth bit of any number and also its complexity of code.
Understanding "Check nth bit"
Checking nth bit means it is the process where we examine a binary number to see if the digit at a specific position, referred to as the nth bit, is either 1 (set) or 0 (not set).
- In binary, each digit is a bit, and these bits are numbered from right to left, starting with 0.
- The term “nth bit” indicates the bit at position n in the binary representation of a number.
Example :
Example 1 :
Let’s break it down with an example:
Consider the binary number 101001. Each digit in this binary number is a bit, and we number them from right to left starting with 0. So, in this case:
- The 0th bit is 1
- The 1st bit is 0
- The 2nd bit is 0
- The 3rd bit is 1
- The 4th bit is 0
- The 5th bit is 1
Now, if we want to check whether the 3rd bit is set or not, we look at the digit at the 3rd position, which is 1. Since it’s 1, we say that the 3rd bit is set.
In simple terms, checking whether the nth bit is set or not involves inspecting a binary number and determining if the digit at the specified position (nth bit) is 1 or 0.
Example 2 :
Binary number: 101001
Check whether the 4th bit is set or not.
Result: The 4th bit is set because the digit at the 4th position is 0.
Idea for "Check whether nth bit is set or not"
- Right-shift n by (k – 1) positions and assign the result to temp.
- If the bitwise AND operation between temp and 1 equals 1, then
- Return True
- Return False
Program to check whether nth bit is set or not
To check whether the kth bit is set, we can use a bitwise AND operation. The AND operation compares each bit of two binary numbers and produces a result where a bit is set to 1 only if the corresponding bits in both numbers are 1.
def solve(n, k): return (n >> (k - 1)) & 1 == 1 n = 9 k = 3 print(solve(n, k))
Output :
True
Explanation :
the optimized code combines the bitwise right shift, bitwise AND operation, and a comparison in a single line, eliminating the need for an explicit if statement. The result is a more concise and readable implementation of the same functionality.
Practical Applications
Following are the practical application :
To wrap it up:
In conclusion, the ability to check whether the nth bit is set or not involves leveraging bitwise operations to examine the binary representation of a number and determine the state of a specific bit. This operation finds utility in low-level programming, device driver development, cryptography, and other domains where direct control over individual bits is essential.
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment