# Position of the K-th set bit in a number

Given two numbers N and K. The task is to find the index of the K-th set bit in the number from the right.

Note: Indexing in the binary representation starts from 0 from the right. For example in the binary number “000011”, the first set bit is at index 0 from right, and the second set bit is at index 1 from the right.

Examples:

```Input: N = 15, K = 3
Output: 2
15 is "1111", hence the third bit is at index 2 from right.

Input:  N = 19, K = 2
Output: 1
19 is "10011", hence the second set bit is at inex 1 from right.
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach: Initialize a counter 0, and increase it if the last bit is set in the number. For accessing the next bit, right shift the number by 1. When the counter’s value is equal to K, then we return the index of the number which was being incremented on every right shift.

Below is the implementation of the above approach:

## C++

 `// C++ program to implement ` `// the above approach ` `#include ` `using` `namespace` `std; ` ` `  `// Function that returns the Kth set bit ` `int` `FindIndexKthBit(``int` `n, ``int` `k) ` `{ ` `    ``int` `cnt = 0; ` `    ``int` `ind = 0; ` ` `  `    ``// Traverse in the binary ` `    ``while` `(n) { ` ` `  `        ``// Check if the last ` `        ``// bit is set or not ` `        ``if` `(n & 1) ` `            ``cnt++; ` ` `  `        ``// Check if count is equal to k ` `        ``// then return the index ` `        ``if` `(cnt == k) ` `            ``return` `ind; ` ` `  `        ``// Increase the index ` `        ``// as we move right ` `        ``ind++; ` ` `  `        ``// Right shift the number by 1 ` `        ``n = n >> 1; ` `    ``} ` ` `  `    ``return` `-1; ` `} ` ` `  `// Driver Code ` `int` `main() ` `{ ` `    ``int` `n = 15, k = 3; ` `    ``int` `ans = FindIndexKthBit(n, k); ` `    ``if` `(ans != -1) ` `        ``cout << ans; ` `    ``else` `        ``cout << ``"No k-th set bit"``; ` `    ``return` `0; ` `} `

## Java

 `// Java program to implement ` `// the above approach ` `import` `java.util.*; ` ` `  `class` `GFG ` `{ ` ` `  `// Function that returns the Kth set bit ` `static` `int` `FindIndexKthBit(``int` `n, ``int` `k) ` `{ ` `    ``int` `cnt = ``0``; ` `    ``int` `ind = ``0``; ` ` `  `    ``// Traverse in the binary ` `    ``while` `(n > ``0``)  ` `    ``{ ` ` `  `        ``// Check if the last ` `        ``// bit is set or not ` `        ``if` `((n & ``1` `) != ``0``) ` `            ``cnt++; ` ` `  `        ``// Check if count is equal to k ` `        ``// then return the index ` `        ``if` `(cnt == k) ` `            ``return` `ind; ` ` `  `        ``// Increase the index ` `        ``// as we move right ` `        ``ind++; ` ` `  `        ``// Right shift the number by 1 ` `        ``n = n >> ``1``; ` `    ``} ` ` `  `    ``return` `-``1``; ` `} ` ` `  `// Driver Code ` `public` `static` `void` `main(String args[]) ` `{ ` `    ``int` `n = ``15``, k = ``3``; ` `    ``int` `ans = FindIndexKthBit(n, k); ` `    ``if` `(ans != -``1``) ` `        ``System.out.println(ans); ` `    ``else` `        ``System.out.println(``"No k-th set bit"``); ` `} ` `} ` ` `  `// This code is contributed by  ` `// Surendra_Gangwar `

## Python3

 `# Python3 implementation of the approach  ` ` `  `# Function that returns the Kth set bit ` `def` `FindIndexKthBit(n, k): ` ` `  `    ``cnt, ind ``=` `0``, ``0` `     `  `    ``# Traverse in the binary ` `    ``while` `n > ``0``:  ` ` `  `        ``# Check if the last ` `        ``# bit is set or not ` `        ``if` `n & ``1``: ` `            ``cnt ``+``=` `1` ` `  `        ``# Check if count is equal to k ` `        ``# then return the index ` `        ``if` `cnt ``=``=` `k: ` `            ``return` `ind ` ` `  `        ``# Increase the index ` `        ``# as we move right ` `        ``ind ``+``=` `1` ` `  `        ``# Right shift the number by 1 ` `        ``n ``=` `n >> ``1` `     `  `    ``return` `-``1` ` `  `# Driver Code ` `if` `__name__ ``=``=` `"__main__"``: ` ` `  `    ``n, k ``=` `15``, ``3` `    ``ans ``=` `FindIndexKthBit(n, k) ` `     `  `    ``if` `ans !``=` `-``1``: ` `        ``print``(ans) ` `    ``else``: ` `        ``print``(``"No k-th set bit"``)  ` `         `  `# This code is contributed by  ` `# Rituraj Jain `

## C#

 `// C# program to implement ` `// the above approach ` `using` `System; ` ` `  `class` `GFG ` `{ ` ` `  `// Function that returns the Kth set bit ` `static` `int` `FindIndexKthBit(``int` `n, ``int` `k) ` `{ ` `    ``int` `cnt = 0; ` `    ``int` `ind = 0; ` ` `  `    ``// Traverse in the binary ` `    ``while` `(n > 0)  ` `    ``{ ` ` `  `        ``// Check if the last ` `        ``// bit is set or not ` `        ``if` `((n & 1 ) != 0) ` `            ``cnt++; ` ` `  `        ``// Check if count is equal to k ` `        ``// then return the index ` `        ``if` `(cnt == k) ` `            ``return` `ind; ` ` `  `        ``// Increase the index ` `        ``// as we move right ` `        ``ind++; ` ` `  `        ``// Right shift the number by 1 ` `        ``n = n >> 1; ` `    ``} ` ` `  `    ``return` `-1; ` `} ` ` `  `// Driver Code ` `public` `static` `void` `Main() ` `{ ` `    ``int` `n = 15, k = 3; ` `    ``int` `ans = FindIndexKthBit(n, k); ` `    ``if` `(ans != -1) ` `        ``Console.WriteLine(ans); ` `    ``else` `        ``Console.WriteLine(``"No k-th set bit"``); ` `} ` `} ` ` `  `// This code is contributed by  ` `// Code_Mech. `

## PHP

 `> 1;  ` `    ``}  ` ` `  `    ``return` `-1;  ` `}  ` ` `  `// Driver Code  ` `\$n` `= 15; ` `\$k` `= 3;  ` ` `  `\$ans` `= FindIndexKthBit(``\$n``, ``\$k``);  ` ` `  `if` `(``\$ans` `!= -1)  ` `    ``echo` `\$ans``;  ` `else` `    ``echo` `"No k-th set bit"``; ` ` `  `// This code is contributed by Ryuga ` `?> `

Output:

```2
```

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.

Article Tags :
Practice Tags :

1

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.