Find value of k-th bit in binary representation

Given a number n and k (1 <= k <= 32), find the value of k-th bit in the binary representation of n. Bits are numbered from right (Least Significant Bit) to left (Most Significant Bit).

Examples :

```Input :
n = 13, k = 2
Output :
0
Explanation:
Binary representation of 13 is 1101.
Second bit from right is 0.

Input :
n = 14, k = 3
Output :
1
Explanation:
Binary representation of 14 is 1110.
Third bit from right is 1.```

Approach 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.

Below is the implementation of the above approach:

C++

 `// CPP program to find k-th bit from right` `#include ` `using` `namespace` `std;`   `void` `printKthBit(unsigned ``int` `n, unsigned ``int` `k)` `{` `    ``cout << ((n & (1 << (k - 1))) >> (k - 1));` `}`   `// Driver Code` `int` `main()` `{` `    ``unsigned ``int` `n = 13, k = 2;` `  `  `    ``// Function Call` `    ``printKthBit(n, k);` `    ``return` `0;` `}`

Java

 `// Java program to find` `// k-th bit from right` `import` `java.io.*;`   `class` `GFG {` `    ``static` `void` `printKthBit(``long` `n, ``long` `k)` `    ``{` `        ``System.out.println(` `            ``((n & (``1` `<< (k - ``1``))) >> (k - ``1``)));` `    ``}`   `    ``// Driver Code` `    ``public` `static` `void` `main(String[] args)` `    ``{` `        ``long` `n = ``13``, k = ``2``;` `      `  `        ``// Function Call` `        ``printKthBit(n, k);` `    ``}` `}`   `// This code is contributed by anuj_67.`

Python3

 `# Python 3 program to find` `# k-th bit from right`   `def` `printKthBit(n, k):`   `    ``print``((n & (``1` `<< (k ``-` `1``))) >> (k ``-` `1``))`   `# Driver Code` `n ``=` `13` `k ``=` `2`   `# Function Call` `printKthBit(n, k)`   `# This code is contributed by Smitha`

C#

 `// C# program to find k-th bit from right` `using` `System;`   `class` `GFG {`   `    ``static` `void` `printKthBit(``long` `n, ``long` `k)` `    ``{` `        ``Console.WriteLine((n & (1 << (k - 1))) >> (k - 1));` `    ``}`   `    ``// Driver Code` `    ``public` `static` `void` `Main()` `    ``{` `        ``long` `n = 13, k = 2;` `        `  `        ``// Function Call` `        ``printKthBit(n, k);` `    ``}` `}`   `// This code is contributed by anuj_67.`

PHP

 ``

Javascript

 ``

Output

`0`

Time Complexity: O(1), the code will run in a constant time.
Auxiliary Space: O(1), no extra space is required, so it is a constant.

Approach 2:

1) Right shift the number (k-1) times, so that the kth bit comes at the right (Least Significant Bit). We can do this by n >> (k-1). For example for number 37(100101) and k = 3, ( 32 >> (3-1) ) will be equal to 9(1001).

2) Then just check the last bit is set or not. We can easily do this by checking if number is odd or even.

Below is the implementation of the above approach:

C++

 `// CPP program to find k-th bit from right` `#include ` `using` `namespace` `std;` ` `  `void` `printKthBit(unsigned ``int` `n, unsigned ``int` `k)` `{` `    ``cout << (n >> (k-1)) % 2;` `}` ` `  `// Driver Code` `int` `main()` `{` `    ``unsigned ``int` `n = 37, k = 3;` `   `  `    ``// Function Call` `    ``printKthBit(n, k);` `    ``return` `0;` `}` `// This code is contributed by priyam26.`

Java

 `// Java program to find k-th bit from right` `import` `java.io.*;` ` `  `class` `GFG {` `    ``static` `void` `printKthBit(``long` `n, ``long` `k)` `    ``{` `        ``System.out.println((n >> (k-``1``)) % ``2``);` `    ``}` ` `  `    ``// Driver Code` `    ``public` `static` `void` `main(String[] args)` `    ``{` `        ``long` `n = ``37``, k = ``3``;` `       `  `        ``// Function Call` `        ``printKthBit(n, k);` `    ``}` `}`   `// This code is contributed by priyam26.`

Python3

 `# Python 3 program to find k-th bit from right` ` `  `def` `printKthBit(n, k):` ` `  `    ``print``((n >> (k``-``1``)) ``%` `2``)` ` `  `# Driver Code` `n ``=` `37` `k ``=` `3` ` `  `# Function Call` `printKthBit(n, k)`   `# This code is contributed by priyam26.`

C#

 `// C# program to find k-th bit from right`   `using` `System;` `using` `System.Collections.Generic;`   `class` `GFG {` `    ``static` `void` `printKthBit(``int` `n, ``int` `k)` `    ``{` `        ``Console.WriteLine((n >> (k-1)) % 2);` `    ``}` ` `  `    ``// Driver Code` `    ``public` `static` `void` `Main(``string``[] args)` `    ``{` `        ``int` `n = 37, k = 3;` `       `  `        ``// Function Call` `        ``printKthBit(n, k);` `    ``}` `}`   `// This code is contributed by phasing17.`

Javascript

 ``

Output

`1`

Time Complexity: O(1), the code will run in a constant time.
Auxiliary Space: O(1), no extra space is required, so it is a constant.

Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!

Previous
Next