Given a number n and k (1 <= k <= 32), find value of k-th bit in binary representation of n. Bits are numbered from right (Least Significant Bit) to left (Most Significant Bit).
Input : n = 13, k = 2 Output : 0 Binary representation of 13 is 1101. Second bit from right is 0. Input : n = 14, k = 3 Output : 1 Binary representation of 14 is 1110. Third bit from right is 1.
1) Find a number with all 0s except k-th position. We get this number using ( 1 << (k-1) ). For example if k = 3, then ( 1 << 2) gives us (00..00100).
2) Do bitwise and of above obtained number with n to find if k-th bit in n is set or not.
- Program to find whether a no is power of two
- Program to find parity
- Find the Number Occurring Odd Number of Times
- Find the two non-repeating elements in an array of repeating elements
- Find whether a given number is a power of 4 or not
- Find the element that appears once
- Binary representation of a given number
- Find position of the only set bit
- Check if binary representation of a number is palindrome
- Find nth Magic Number
- Find XOR of two number without using XOR operator
- Find the maximum subarray XOR in a given array
- Find Next Sparse Number
- Given a set, find XOR of the XOR's of all subsets.
- Find the maximum subset XOR of a given set
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.