Extract ‘k’ bits from a given position in a number.

How to extract ‘k’ bits from a given position ‘p’ in a number?

Examples:

Input : number = 171
             k = 5 
             p = 2
Output : The extracted number is 21
171 is represented as 0101011 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) Right shift number by p-1.
2) Do bit wise AND of k set bits with the modified number. We can get k set bits by doing (1 << k) – 1.

C

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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));
    }
}

chevron_right


Python

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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)

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right



Output :

The extracted number is 21

This article is contributed by Ujjwal Aryal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks 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.



My Personal Notes arrow_drop_up

Improved By : jit_t



Article Tags :
Practice Tags :


1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.