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