29. Check whether K-th bit is set or not
Last updated
Was this helpful?
Last updated
Was this helpful?
The problem can be found at the following link:
To check whether the K-th bit is set in an integer n
, I use a bit manipulation technique.
I create a mask with the K-th bit set to 1 by left-shifting 1 by K positions.
Then, I perform a bitwise AND operation between n
and the mask. If the result is greater than 0, it means the K-th bit is set; otherwise, it's not set.
Time Complexity: O(1)
- The time complexity is constant because the number of operations is not dependent on the size of the input.
Auxiliary Space Complexity: O(1)
- The auxiliary space complexity is constant because we use a fixed amount of memory for the mask and the result.
For discussions, questions, or doubts related to this solution, please visit our . We welcome your input and aim to foster a collaborative learning environment.
If you find this solution helpful, consider supporting us by giving a ⭐ star to the repository.