📜  检查数字的所有位是否都已设置

📅  最后修改于: 2021-05-25 04:55:41             🧑  作者: Mango

给定数字n 。问题是检查给定数字的二进制表示形式中的每个位是否都已设置。这里0 <= n
例子 :

Input : 7
Output : Yes
(7)10 = (111)2

Input : 14
Output : No

方法1:如果n = 0,则答案为“否”。否则执行两个操作,直到n变为0。

While (n > 0)
 If n & 1 == 0, 
     return 'No'
 n >> 1

如果循环终止而不返回“ No”,则所有位均以n的二进制表示形式设置。

C++
// C++ implementation to check whether every
// digit in the binary representation of the
// given number is set or not
#include 
using namespace std;
 
// function to check if all the bits are set
// or not in the binary representation of 'n'
string areAllBitsSet(int n)
{
    // all bits are not set
    if (n == 0)
        return "No";
 
    // loop till n becomes '0'
    while (n > 0)
    {
        // if the last bit is not set
        if ((n & 1) == 0)
            return "No";
 
        // right shift 'n' by 1
        n = n >> 1;
    }
 
    // all bits are set
    return "Yes";
}
 
// Driver program to test above
int main()
{
    int n = 7;
    cout << areAllBitsSet(n);
    return 0;
}


Java
// java implementation to check
// whether every digit in the
// binary representation of the
// given number is set or not
import java.io.*;
 
class GFG {
     
    // function to check if all the bits
    // are setthe bits are set or not
    // in the binary representation of 'n'
    static String areAllBitsSet(int n)
    {
        // all bits are not set
        if (n == 0)
            return "No";
     
        // loop till n becomes '0'
        while (n > 0)
        {
            // if the last bit is not set
            if ((n & 1) == 0)
                return "No";
     
            // right shift 'n' by 1
            n = n >> 1;
        }
     
            // all bits are set
            return "Yes";
    }
     
    // Driver program to test above
    public static void main (String[] args) {
    int n = 7;
     
    System.out.println(areAllBitsSet(n));
    }
}
 
 
// This code is contributed by vt_m


Python3
# Python implementation
# to check whether every
# digit in the binary
# representation of the
# given number is set or not
 
# function to check if
# all the bits are set
# or not in the binary
# representation of 'n'
def areAllBitsSet(n):
 
    # all bits are not set
    if (n == 0):
        return "No"
  
    # loop till n becomes '0'
    while (n > 0):
     
        # if the last bit is not set
        if ((n & 1) == 0):
            return "No"
  
        # right shift 'n' by 1
        n = n >> 1
     
  
    # all bits are set
    return "Yes"
 
  
# Driver program to test above
 
n = 7
print(areAllBitsSet(n))
 
# This code is contributed
# by Anant Agarwal.


C#
// C# implementation to check
// whether every digit in the
// binary representation of the
// given number is set or not
using System;
 
class GFG
{
     
    // function to check if 
    // all the bits are set
    // or not in the binary
    // representation of 'n'
    static String areAllBitsSet(int n)
    {
        // all bits are not set
        if (n == 0)
            return "No";
     
        // loop till n becomes '0'
        while (n > 0)
        {
            // if the last bit
            // is not set
            if ((n & 1) == 0)
                return "No";
     
            // right shift 'n' by 1
            n = n >> 1;
        }
     
            // all bits are set
            return "Yes";
    }
     
    // Driver Code
    static public void Main ()
    {
        int n = 7;
        Console.WriteLine(areAllBitsSet(n));
    }
}
 
// This code is contributed by ajit


PHP
 0)
    {
        // if the last bit is not set
        if (($n & 1) == 0)
            return "No";
 
        // right shift 'n' by 1
        $n = $n >> 1;
    }
 
    // all bits are set
    return "Yes";
}
 
// Driver Code
$n = 7;
echo areAllBitsSet($n);
 
// This code is contributed by aj_36
?>


Javascript


C++
// C++ implementation to check whether every
// digit in the binary representation of the
// given number is set or not
#include 
using namespace std;
 
// function to check if all the bits are set
// or not in the binary representation of 'n'
string areAllBitsSet(int n)
{
    // all bits are not set
    if (n == 0)
        return "No";
 
    // if true, then all bits are set
    if (((n + 1) & n) == 0)
        return "Yes";
 
    // else all bits are not set
    return "No";
}
 
// Driver program to test above
int main()
{
    int n = 7;
    cout << areAllBitsSet(n);
    return 0;
}


Java
// JAVA implementation to check whether
// every digit in the binary representation
// of the given number is set or not
import java.io.*;
 
class GFG {
     
    // function to check if all the
    // bits are set or not in the
    // binary representation of 'n'
    static String areAllBitsSet(int n)
    {
        // all bits are not set
        if (n == 0)
            return "No";
     
        // if true, then all bits are set
        if (((n + 1) & n) == 0)
            return "Yes";
     
        // else all bits are not set
        return "No";
    }
     
    // Driver program to test above
    public static void main (String[] args) {
    int n = 7;
    System.out.println(areAllBitsSet(n));
    }
}
 
// This code is contributed by vt_m


Python3
# Python implementation to
# check whether every
# digit in the binary
# representation of the
# given number is set or not
 
# function to check if
# all the bits are set
# or not in the binary
# representation of 'n'
def areAllBitsSet(n):
 
    # all bits are not set
    if (n == 0):
        return "No"
  
    # if true, then all bits are set
    if (((n + 1) & n) == 0):
        return "Yes"
  
    # else all bits are not set
    return "No"
 
  
# Driver program to test above
 
n = 7
print(areAllBitsSet(n))
 
# This code is contributed
# by Anant Agarwal.


C#
// C# implementation to check
// whether every digit in the
// binary representation of
// the given number is set or not
using System;
 
class GFG
{
     
    // function to check if all the
    // bits are set or not in the
    // binary representation of 'n'
    static String areAllBitsSet(int n)
    {
        // all bits are not set
        if (n == 0)
            return "No";
     
        // if true, then all
        // bits are set
        if (((n + 1) & n) == 0)
            return "Yes";
     
        // else all bits are not set
        return "No";
    }
     
    // Driver Code
    static public void Main ()
    {
        int n = 7;
        Console.WriteLine(areAllBitsSet(n));
    }
}
 
// This code is contributed by m_kit


PHP


Javascript


输出 :

Yes

时间复杂度: O(d),其中“ d”是n的二进制表示形式中的位数。方法2:如果n = 0,则答案为“否”。否则将1加到n 。设num = n +1。如果num&(num – 1)== 0,则所有位都置1,否则所有位均不置1。
说明:如果设置了n的二进制表示形式中的所有位,则将其添加为“ 1”将产生一个数字,该数字将是2的完美幂。现在,检查新数字是否是2的完美幂。

C++

// C++ implementation to check whether every
// digit in the binary representation of the
// given number is set or not
#include 
using namespace std;
 
// function to check if all the bits are set
// or not in the binary representation of 'n'
string areAllBitsSet(int n)
{
    // all bits are not set
    if (n == 0)
        return "No";
 
    // if true, then all bits are set
    if (((n + 1) & n) == 0)
        return "Yes";
 
    // else all bits are not set
    return "No";
}
 
// Driver program to test above
int main()
{
    int n = 7;
    cout << areAllBitsSet(n);
    return 0;
}

Java

// JAVA implementation to check whether
// every digit in the binary representation
// of the given number is set or not
import java.io.*;
 
class GFG {
     
    // function to check if all the
    // bits are set or not in the
    // binary representation of 'n'
    static String areAllBitsSet(int n)
    {
        // all bits are not set
        if (n == 0)
            return "No";
     
        // if true, then all bits are set
        if (((n + 1) & n) == 0)
            return "Yes";
     
        // else all bits are not set
        return "No";
    }
     
    // Driver program to test above
    public static void main (String[] args) {
    int n = 7;
    System.out.println(areAllBitsSet(n));
    }
}
 
// This code is contributed by vt_m

Python3

# Python implementation to
# check whether every
# digit in the binary
# representation of the
# given number is set or not
 
# function to check if
# all the bits are set
# or not in the binary
# representation of 'n'
def areAllBitsSet(n):
 
    # all bits are not set
    if (n == 0):
        return "No"
  
    # if true, then all bits are set
    if (((n + 1) & n) == 0):
        return "Yes"
  
    # else all bits are not set
    return "No"
 
  
# Driver program to test above
 
n = 7
print(areAllBitsSet(n))
 
# This code is contributed
# by Anant Agarwal.

C#

// C# implementation to check
// whether every digit in the
// binary representation of
// the given number is set or not
using System;
 
class GFG
{
     
    // function to check if all the
    // bits are set or not in the
    // binary representation of 'n'
    static String areAllBitsSet(int n)
    {
        // all bits are not set
        if (n == 0)
            return "No";
     
        // if true, then all
        // bits are set
        if (((n + 1) & n) == 0)
            return "Yes";
     
        // else all bits are not set
        return "No";
    }
     
    // Driver Code
    static public void Main ()
    {
        int n = 7;
        Console.WriteLine(areAllBitsSet(n));
    }
}
 
// This code is contributed by m_kit

的PHP


Java脚本


输出 :

Yes