Extrae ‘k’ bits de una posición dada en un número.

¿Cómo extraer bits ‘k’ de una posición dada ‘p’ en un número?

Ejemplos: 

Input : number = 171
             k = 5 
             p = 2
Output : The extracted number is 21
171 is represented as 10101011 in binary,
so, you should get only 10101 i.e. 21.

Input : number = 72
            k = 5 
            p = 1
Output : The extracted number is 8
72 is represented as 1001000 in binary,
so, you should get only 01000 i.e 8.
 

1) Número de desplazamiento a la derecha por p-1. 
2) Hacer bit wise AND de k set bits con el número modificado. Podemos obtener k bits establecidos haciendo (1 << k) – 1.

C++

// C++ program to extract k bits from a given
// position.
#include <bits/stdc++.h>
using namespace std;
 
// Function to extract k bits from p position
// and returns the extracted value as integer
int bitExtracted(int number, int k, int p)
{
    return (((1 << k) - 1) & (number >> (p - 1)));
}
 
// Driver code
int main()
{
    int number = 171, k = 5, p = 2;
    cout << "The extracted number is " <<
            bitExtracted(number, k, p);
             
    return 0;
}
 
// This code is contributed by importantly

C

// C program to extract k bits from a given
// position.
#include <stdio.h>
 
// Function to extract k bits from p position
// and returns the extracted value as integer
int bitExtracted(int number, int k, int p)
{
    return (((1 << k) - 1) & (number >> (p - 1)));
}
 
// Driver code
int main()
{
    int number = 171, k = 5, p = 2;
    printf("The extracted number is %d",
               bitExtracted(number, k, p));
    return 0;
}

Java

// Java program to extract k bits from a given
// position.
 
class GFG {
 
    // Function to extract k bits from p position
    // and returns the extracted value as integer
    static int bitExtracted(int number, int k, int p)
    {
        return (((1 << k) - 1) & (number >> (p - 1)));
    }
  
    // Driver code
    public static void main (String[] args) {
        int number = 171, k = 5, p = 2;
        System.out.println("The extracted number is "+
               bitExtracted(number, k, p));
    }
}

Python3

# Python program to extract k bits from a given
# position.
 
# Function to extract k bits from p position
# and returns the extracted value as integer
def bitExtracted(number, k, p):  
    return ( ((1 << k) - 1)  &  (number >> (p-1) ) );
 
# number is from where 'k' bits are extracted
# from p position
number = 171
k = 5
p = 2
print ("The extracted number is ", bitExtracted(number, k, p))

C#

// C# program to extract k bits from a given
// position.
using System;
 
class GFG {
  
    // Function to extract k bits from p position
    // and returns the extracted value as integer
    static int bitExtracted(int number, int k, int p)
    {
        return (((1 << k) - 1) & (number >> (p - 1)));
    }
   
    // Driver code
    public static void Main()
    {
        int number = 171, k = 5, p = 2;
         
        Console.WriteLine("The extracted number is "
                      + bitExtracted(number, k, p));
    }
}
 
//This code is contributed by Anant Agarwal.

PHP

<?php
//PHP program to extract
// k bits from a given
// position.
 
// Function to extract k
// bits from p position
// and returns the extracted
// value as integer
function bitExtracted($number, $k, $p)
{
    return (((1 << $k) - 1) &
           ($number >> ($p - 1)));
}
 
    // Driver Code
    $number = 171; $k = 5; $p = 2;
    echo "The extracted number is ",
          bitExtracted($number, $k, $p);
           
// This code is contributed by Ajit
?>

Javascript

<script>
// JavaScript program to extract k bits from a given
// position.
 
// Function to extract k bits from p position
// and returns the extracted value as integer
function bitExtracted(number, k, p)
{
    return (((1 << k) - 1) & (number >> (p - 1)));
}
 
// Driver code
 
    let number = 171, k = 5, p = 2;
    document.write("The extracted number is ",
            bitExtracted(number, k, p));
 
 
// This code is contributed by Manoj.
</script>

Producción : 

The extracted number is 21

Complejidad de tiempo: O(1)

Espacio Auxiliar: O(1)
 

Este artículo es una contribución de Ujjwal Aryal . Si te gusta GeeksforGeeks y te gustaría contribuir, también puedes escribir un artículo usando write.geeksforgeeks.org o enviar tu artículo por correo a review-team@geeksforgeeks.org. Vea su artículo que aparece en la página principal de GeeksforGeeks y ayude a otros Geeks.
Escriba comentarios si encuentra algo incorrecto o si desea compartir más información sobre el tema tratado anteriormente.
 

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 *