Compruebe si el K-th bit está configurado o no

Dado un número n, compruebe si el K-ésimo bit de n está activado o no.
 

Ejemplos: 

C++

// CPP program to check if k-th bit
// of a given number is set or not
#include <iostream>
using namespace std;
  
void isKthBitSet(int n, int k)
{
    if (n & (1 << (k - 1)))
        cout << "SET";
    else
        cout << "NOT SET";
}
  
// Driver code
int main()
{
    int n = 5, k = 1;
    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 - 1)))
        printf("SET");
    else
        printf("NOT SET");
}
  
// Driver code
int main()
{
    int n = 5, k = 1;
    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 - 1))) > 0)
            System.out.print("SET");
        else
            System.out.print("NOT SET");
    }
  
    // driver code
    public static void main(String[] args)
    {
        int n = 5, k = 1;
        isKthBitSet(n, k);
    }
}
  
// This code is contributed by rishabh_jain

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 - 1)):
        print( "SET")
    else:
        print("NOT SET")
  
# Driver code
n = 5
k = 1
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 - 1))) > 0)
            Console.Write("SET");
        else
            Console.Write("NOT SET");
    }
  
    // Driver code
    public static void Main()
    {
        int n = 5, k = 1;
  
        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 - 1)))
        echo "SET";
    else
        echo "NOT SET";
}
  
// Driver code
$n = 5; $k = 1;
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 - 1))) > 0)
            document.write("SET");
        else
            document.write("NOT SET");
    }
      
    let n = 5, k = 1;
    
      isKthBitSet(n, k);
                                  
</script>

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)) & 1)
        cout << "SET";
    else
        cout << "NOT SET";
}
  
// Driver code
int main()
{
    int n = 5, k = 1;
    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)) & 1)
        printf("SET");
    else
        printf("NOT SET");
}
  
// Driver code
int main()
{
    int n = 5, k = 1;
    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)) &
               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;
    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 - 1)) and 1):
        print("SET")
    else:
        print("NOT SET") 
  
# Driver code 
n, k = 5, 1
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)) &
              1) > 0)
        Console.WriteLine("SET");
    else
        Console.WriteLine("NOT SET");
}
  
// Driver code
static public void Main ()
{
    int n = 5, k = 1;
    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)) & 1)
        echo "SET";
    else
        echo "NOT SET";
}
  
// Driver code
$n = 5; $k = 1;
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)) &
               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>

Publicación traducida automáticamente

Artículo escrito por GeeksforGeeks-1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *