Sunday, September 22, 2024
Google search engine
HomeData Modelling & AICheck whether K-th bit is set or not

Check whether K-th bit is set or not

Given a number N and a bit number K, check if the Kth bit of N is set or not. A bit is called set if it is 1. 
Note: Indexing starts with 0 from LSB (least significant bit) side in the binary representation of the number.

Examples: 

Input: n = 5, k = 1
Output: NOT SET
Explanation: 5 is represented as 101 in binary and bit at position 1 is not set

Input: n = 2, k = 3
Output: NOT SET
Explanation: 2 is represented as 10 in binary, all higher i.e. beyond MSB, bits are NOT SET.

Check whether the K-th bit is set or not Using Left Shift Operator:

To solve the problem follow the below idea:

  • Left shift given number 1 by k to create a number that has only set bit as k-th bit.
    temp = 1 << k
  • If bitwise AND of n and temp is non-zero, then result is SET else result is NOT SET.

Below is the implementation of the above approach:

C++




// CPP program to check if k-th bit
// of a given number is set or not
#include <bits/stdc++.h>
using namespace std;
 
void isKthBitSet(int n, int k)
{
    if (n & (1 << k))
        cout << "SET";
    else
        cout << "NOT SET";
}
 
// Driver code
int main()
{
    int n = 5, k = 1;
 
    // Function call
    isKthBitSet(n, k);
    return 0;
}


C




// C program to check if k-th bit
// of a given number is set or not
#include <stdio.h>
 
void isKthBitSet(int n, int k)
{
    if (n & (1 << k))
        printf("SET");
    else
        printf("NOT SET");
}
 
// Driver code
int main()
{
    int n = 5, k = 1;
 
    // Function call
    isKthBitSet(n, k);
    return 0;
}
 
// this code is contributed by devendrasaluke


Java




// Java program to check if k-th bit
// of a given number is set or not
 
class Number {
    public static void isKthBitSet(int n, int k)
    {
        if ((n & (1 <<k)) != 0)
            System.out.print("SET");
        else
            System.out.print("NOT SET");
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int n = 5, k = 1;
 
        // Function call
        isKthBitSet(n, k);
    }
}
 
// This code is contributed by Shaweta


Python3




# Python3 code to check if k-th bit
# of a given number is set or not
 
 
def isKthBitSet(n, k):
    if n & (1 << k):
        print("SET")
    else:
        print("NOT SET")
 
 
# Driver code
if __name__ == "__main__":
    n = 5
    k = 1
 
    # Function call
    isKthBitSet(n, k)
 
# This code is contributed by "Sharad_Bhardwaj".


C#




// C# program to check if k-th bit
// of a given number is set or not.
using System;
 
class GFG {
 
    public static void isKthBitSet(int n, int k)
    {
        if ((n & (1 << k)) > 0)
            Console.Write("SET");
        else
            Console.Write("NOT SET");
    }
 
    // Driver code
    public static void Main()
    {
        int n = 5, k = 1;
 
        // Function call
        isKthBitSet(n, k);
    }
}
 
// This code is contributed by nitin mittal.


PHP




<?php
// PHP program to check if
// k-th bit of a given
// number is set or not
function isKthBitSet($n, $k)
{
    if ($n & (1 << $k))
        echo "SET";
    else
        echo "NOT SET";
}
 
// Driver code
$n = 5; $k = 1;
 
// Function call
isKthBitSet($n, $k);
 
// This code is contributed
// by akt_mit
?>


Javascript




<script>
    // Javascript program to check if k-th bit
    // of a given number is set or not.
     
    function isKthBitSet(n, k)
    {
        if ((n & (1 << k)) > 0)
            document.write("SET");
        else
            document.write("NOT SET");
    }
     
    let n = 5, k = 1;
   
      isKthBitSet(n, k);
                                 
</script>


Output

NOT SET

Time Complexity: O(1)
Auxiliary Space: O(1)

Check whether the K-th bit is set or not Using Right Shift Operator:

To solve the problem follow the below idea:

If we right shift n by k, we get the last bit as 1 if the Kth bit is set else 0

Below is the implementation of the above approach:

C++




// CPP program to check if k-th bit
// of a given number is set or not using
// right shift operator.
#include <iostream>
using namespace std;
 
void isKthBitSet(int n, int k)
{
    if ((n >> k) & 1)
        cout << "SET";
    else
        cout << "NOT SET";
}
 
// Driver code
int main()
{
    int n = 5, k = 1;
 
    // Function call
    isKthBitSet(n, k);
    return 0;
}


C




// C program to check if k-th bit
// of a given number is set or not using
// right shift operator.
#include <stdio.h>
 
void isKthBitSet(int n, int k)
{
    if ((n >> k) & 1)
        printf("SET");
    else
        printf("NOT SET");
}
 
// Driver code
int main()
{
    int n = 5, k = 1;
 
    // Function call
    isKthBitSet(n, k);
    return 0;
}
// this code is contributed by devendrasolunke


Java




// Java program to check if
// k-th bit of a given number
// is set or not using right
// shift operator.
import java.io.*;
 
class GFG {
    static void isKthBitSet(int n, int k)
    {
        if (((n >> k) & 1) > 0)
            System.out.println("SET");
        else
            System.out.println("NOT SET");
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int n = 5, k = 1;
 
        // Function call
        isKthBitSet(n, k);
    }
}
 
// This code is contributed
// by ajit


Python3




# PHP program to check if k-th bit of
# a given number is set or not using
# right shift operator.
 
 
def isKthBitSet(n, k):
    if ((n >> k) and 1):
        print("SET")
    else:
        print("NOT SET")
 
 
# Driver code
if __name__ == "__main__":
    n, k = 5, 1
 
    # Function call
    isKthBitSet(n, k)
 
# This code contributed by
# PrinciRaj1992


C#




// C# program to check if
// k-th bit of a given number
// is set or not using right
// shift operator
using System;
 
class GFG {
    static void isKthBitSet(int n, int k)
    {
        if (((n >> k) & 1) > 0)
            Console.WriteLine("SET");
        else
            Console.WriteLine("NOT SET");
    }
 
    // Driver code
    static public void Main()
    {
        int n = 5, k = 1;
 
        // Function call
        isKthBitSet(n, k);
    }
}
 
// This code is contributed
// by ajit


PHP




<?php
// PHP program to check
// if k-th bit of a given
// number is set or not
// using right shift operator.
 
function isKthBitSet($n, $k)
{
    if (($n >> $k) & 1)
        echo "SET";
    else
        echo "NOT SET";
}
 
// Driver code
$n = 5; $k = 1;
 
// Function call
isKthBitSet($n, $k);
 
// This code is contributed
// by akt_mit
?>


Javascript




<script>
 
// Javascript program to check if
// k-th bit of a given number
// is set or not using right
// shift operator.
 
function isKthBitSet(n, k)
{
    if (((n >> k) &
               1) > 0)
        document.write("SET");
    else
        document.write("NOT SET");
}
 
// Driver Code
 
    let n = 5, k = 1;
    isKthBitSet(n, k);
 
// This code is contributed by sanjoy_62.
</script>


Output

NOT SET

Time Complexity: O(1)
Auxiliary Space: O(1)

This article is contributed by SAKSHI TIWARI. If you like neveropen(We know you do!) and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the neveropen main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
 

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 neveropen!

RELATED ARTICLES

Most Popular

Recent Comments