如何从数字的给定位置“p”中提取“k”位?
null
例如:
Input : number = 171 k = 5 p = 2Output : The extracted number is 21171 is represented as 0101011 in binary,so, you should get only 10101 i.e. 21.Input : number = 72 k = 5 p = 1Output : The extracted number is 872 is represented as 1001000 in binary,so, you should get only 01000 i.e 8.
1) 按p-1右移。 2) 使用修改后的数字对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> |
输出:
The extracted number is 21
本文由 Ujjwal Aryal .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄去评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END