给定一个数字n,检查是否设置了n的第k位。
null
例如:
Input : n = 5, k = 1Output : SET5 is represented as 101 in binary and has its first bit set.Input : n = 2, k = 3Output : NOT SET2 is represented as 10 in binary, all higher i.e. beyond MSB, bits are NOT SET.
方法1(使用左移位运算符) 以下是查找第k位值的简单步骤:
1) Left shift given number 1 by k-1 to create a number that has only set bit as k-th bit. temp = 1 << (k-1)2) If bitwise AND of n and temp is non-zero, then result is SET else result is NOT SET.
例子:
n = 75 and k = 4 temp = 1 << (k-1) = 1 << 3 = 8 Binary Representation of temp = 0..00001000 Binary Representation of n = 0..01001011 Since bitwise AND of n and temp is non-zero, result is SET.
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; } |
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> |
输出:
SET
方法2(使用右移运算符) 如果将n右移k-1,如果第k位设为0,则最后一位为1。
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; } |
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> |
输出:
SET
本文由 萨希·提瓦里 .如果你喜欢极客(我们知道你喜欢!)如果你想投稿,你也可以用 写极客。组织 或者把你的文章寄去评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END