给定一个正整数 N .问题是检查在二进制表示中是否只设置了第一位和最后一位 N . 例如:
null
Input : 9Output : Yes(9)10 = (1001)2, only the first andlast bits are set.Input : 15Output : No(15)10 = (1111)2, except first and lastthere are other bits also which are set.
方法: 以下是步骤:
- 如果n==1,则返回“是”。
- 否则检查n-1是否为2的幂。参考 这 邮递
C++
// C++ to check whether the number has only // first and last bits set #include <bits/stdc++.h> using namespace std; // function to check whether 'n' // is a power of 2 or not bool powerOfTwo(unsigned int n) { return (!(n & n-1)); } // function to check whether the number has only // first and last bits set bool onlyFirstAndLastAreSet(unsigned int n) { if (n == 1) return true ; if (n == 2) return false ; return powerOfTwo(n-1); } // Driver program to test above int main() { unsigned int n = 9; if (onlyFirstAndLastAreSet(n)) cout << "Yes" ; else cout << "No" ; return 0; } |
JAVA
// Java program to check whether the // number has only first and last // bits set import java.util.*; class GFG { // function to check whether 'n' // is a power of 2 or not static boolean powerOfTwo( int n) { return ((n & n - 1 ) == 0 ); } // function to check whether the number has // only first and last bits set static boolean onlyFirstAndLastAreSet( int n) { if (n == 1 ) return true ; return powerOfTwo(n- 1 ); } // Driver program to test above public static void main (String[] args) { int n = Integer.parseUnsignedInt( "9" ); if (onlyFirstAndLastAreSet(n)) System.out.println( "Yes" ); else System.out.println( "No" ); } } /* This code is contributed by Mr. Somesh Awasthi */ |
Python3
# Python3 program to check whether number # has only first and last bits set # function to check whether 'n' # is a power of 2 or not def powerOfTwo (n): return ( not (n & n - 1 )) # function to check whether number # has only first and last bits set def onlyFirstAndLastAreSet (n): if (n = = 1 ): return True return powerOfTwo (n - 1 ) # Driver program to test above n = 9 if (onlyFirstAndLastAreSet (n)): print ( 'Yes' ) else : print ( 'No' ) # This code is contributed by Shariq Raza |
C#
// C# program to check whether the // number has only first and last // bits set using System; class GFG { // function to check whether 'n' // is a power of 2 or not static bool powerOfTwo( uint n) { return ((n & n - 1) == 0); } // function to check whether the number has // only first and last bits set static bool onlyFirstAndLastAreSet( uint n) { if (n == 1) return true ; return powerOfTwo(n - 1); } // Driver program to test above public static void Main() { uint n = ( uint )9; if (onlyFirstAndLastAreSet(n)) Console.WriteLine( "Yes" ); else Console.WriteLine( "No" ); } } // This code is contributed by Sam007 |
PHP
<?php // PHP to check whether the number // has only first and last bits set // function to check whether 'n' // is a power of 2 or not function powerOfTwo( $n ) { return (!( $n & $n - 1)); } // function to check whether // the number has only first // and last bits set function onlyFirstAndLastAreSet( $n ) { if ( $n == 1) return true; if ( $n == 2) return false; return powerOfTwo( $n - 1); } // Driver Code $n = 9; if (onlyFirstAndLastAreSet( $n )) echo "Yes" ; else echo "No" ; // This code is contributed // by Sach_Code ?> |
Javascript
<script> // Javascript to check whether the number has only // first and last bits set // function to check whether 'n' // is a power of 2 or not function powerOfTwo(n) { return (!(n & n-1)); } // function to check whether the number has only // first and last bits set function onlyFirstAndLastAreSet(n) { if (n == 1) return true ; if (n == 2) return false ; return powerOfTwo(n-1); } // Driver program to test above var n = 9; if (onlyFirstAndLastAreSet(n)) document.write( "Yes" ); else document.write( "No" ); </script> |
输出:
Yes
本文由 阿尤什·焦哈里 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄去评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END