📌  相关文章
📜  第K个置1位在数字中的位置

📅  最后修改于: 2021-04-27 18:38:10             🧑  作者: Mango

给定两个数字NK。任务是从右侧找到数字中第K个设置位的索引。
注意:二进制表示形式的索引从右侧的0开始。例如,在二进制数“ 000011”中,第一个设置位在从右开始的索引0处,第二个设置位在从右开始的索引1处。
例子:

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 index 1 from right. 

方法:初始化计数器0,如果数字中的最后一位被设置,则将其增加。为了访问下一位,将数字右移1。当计数器的值等于K时,我们返回数字的索引,该索引在每次右移时都会递增。
下面是上述方法的实现:

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
?>


输出:
2