📜  给定数字的二进制表示

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

编写程序以打印给定数字的二进制表示形式。

资料来源:Microsoft Interview Set-3

方法1:迭代
对于任何数字,我们都可以通过将其与“ 2 ^ i”按位与(2升至i)来检查其第i个位是0(OFF)还是1(ON)。

1) Let us take number 'NUM' and we want to check whether it's 0th bit is ON or OFF    
    bit = 2 ^ 0 (0th bit)
    if  NUM & bit == 1 means 0th bit is ON else 0th bit is OFF

2) Similarly if we want to check whether 5th bit is ON or OFF    
    bit = 2 ^ 5 (5th bit)
    if NUM & bit == 1 means its 5th bit is ON else 5th bit is OFF.

让我们采用无符号整数(32位),该整数由0-31位组成。要打印无符号整数的二进制表示,请从第31位开始,检查第31位是ON还是OFF,如果为ON,则打印“ 1”,否则打印“ 0”。现在检查第30位是ON还是OFF,如果为ON,则打印“ 1”,否则打印“ 0”,对从31到0的所有位都执行此操作,最后将得到数字的二进制表示形式。

C++
// C++ Program for the binary
// representation of a given number
#include 
using namespace std;
 
  void bin(long n)
  {
    long i;
    cout << "0";
    for (i = 1 << 30; i > 0; i = i / 2)
    {
      if((n & i) != 0)
      {
        cout << "1";
      }
      else
      {
        cout << "0";
      }
    }
  }
 
// Driver Code
int main(void)
{
    bin(7);
    cout << endl;
    bin(4);
}
 
// This code is contributed by souravghosh0416


C
#include
void bin(unsigned n)
{
    unsigned i;
    for (i = 1 << 31; i > 0; i = i / 2)
        (n & i) ? printf("1") : printf("0");
}
 
int main(void)
{
    bin(7);
    printf("\n");
    bin(4);
}


Java
public class GFG
{
  static void bin(long n)
  {
    long i;
    System.out.print("0");
    for (i = 1 << 30; i > 0; i = i / 2)
    {
      if((n & i) != 0)
      {
        System.out.print("1");
      }
      else
      {
        System.out.print("0");
      }
    }
  }
 
  // Driver code
  public static void main(String[] args)
  {
    bin(7);
    System.out.println();
    bin(4);
  }
}
 
// This code is contributed by divyesh072019.


Python3
def bin(n) :
     
    i = 1 << 31
    while(i > 0) :
     
        if((n & i) != 0) :
         
            print("1", end = "")
         
        else :
            print("0", end = "")
             
        i = i // 2
             
bin(7)
print()
bin(4)
 
# This code is contributed by divyeshrabadiya07.


C#
using System;
 
public class GFG{
    static void bin(long n)
    {
        long i;
        Console.Write("0");
        for (i = 1 << 30; i > 0; i = i / 2)
        {
            if((n & i) != 0)
            {
                Console.Write("1");
            }
            else
            {
                Console.Write("0");
            }
        }
    }
    // Driver code
    static public void Main (){
        bin(7);
        Console.WriteLine();
        bin(4);
    }
}
// This code is contributed by avanitrachhadiya2155


Javascript


C++
// C++ Program for the binary
// representation of a given number
#include 
using namespace std;
 
void bin(unsigned n)
{
    /* step 1 */
    if (n > 1)
        bin(n / 2);
 
    /* step 2 */
    cout << n % 2;
}
 
// Driver Code
int main(void)
{
    bin(7);
    cout << endl;
    bin(4);
}
 
// This code is contributed
// by Akanksha Rai


C
// C Program for the binary
// representation of a given number
void bin(unsigned n)
{
    /* step 1 */
    if (n > 1)
        bin(n / 2);
 
    /* step 2 */
    printf("%d", n % 2);
}
 
// Driver Code
int main(void)
{
    bin(7);
    printf("\n");
    bin(4);
}


Java
// Java Program for the binary
// representation of a given number
class GFG {
    static void bin(int n)
    {
        /* step 1 */
        if (n > 1)
            bin(n / 2);
 
        /* step 2 */
        System.out.print(n % 2);
    }
 
    // Driver code
    public static void main(String[] args)
    {
        bin(7);
        System.out.println();
        bin(4);
    }
}
 
// This code is contributed
// by ChitraNayal


Python3
# Python3 Program for the binary
# representation of a given number
 
 
def bin(n):
 
    if n > 1:
        bin(n//2)
 
    print(n % 2, end="")
 
 
# Driver Code
if __name__ == "__main__":
 
    bin(7)
    print()
    bin(4)
 
# This code is contributed by ANKITRAI1


C#
// C# Program for the binary
// representation of a given number
using System;
 
class GFG {
 
    static void bin(int n)
    {
 
        // step 1
        if (n > 1)
            bin(n / 2);
 
        // step 2
        Console.Write(n % 2);
    }
 
    // Driver code
    static public void Main()
    {
        bin(7);
        Console.WriteLine();
        bin(4);
    }
}
 
// This code is contributed ajit


PHP
 1)
        bin($n/2);
 
    /* step 2 */
    echo ($n % 2);
}
 
// Driver code
bin(7);
echo ("\n");
bin(4);
 
// This code is contributed
// by Shivi_Aggarwal
?>


Javascript


C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Function to convert decimal
// to binary number
void bin(unsigned n)
{
    if (n > 1)
        bin(n >> 1);
 
    printf("%d", n & 1);
}
 
// Driver code
int main(void)
{
    bin(131);
    printf("\n");
    bin(3);
    return 0;
}


Java
// Java implementation of the approach
 
class GFG {
 
    // Function to convert decimal
    // to binary number
    static void bin(Integer n)
    {
        if (n > 1)
            bin(n >> 1);
 
        System.out.printf("%d", n & 1);
    }
 
    // Driver code
    public static void main(String[] args)
    {
        bin(131);
        System.out.printf("\n");
        bin(3);
    }
}
/*This code is contributed by PrinciRaj1992*/


Python3
# Python 3 implementation of above approach
 
# Function to convert decimal to
# binary number
 
 
def bin(n):
 
    if (n > 1):
        bin(n >> 1)
    print(n & 1, end="")
 
 
# Driver code
bin(131)
print()
bin(3)
 
# This code is contributed by PrinciRaj1992


C#
// C# implementation of above approach
using System;
 
public class GFG {
 
    // Function to convert decimal
    // to binary number
    static void bin(int n)
    {
        if (n > 1)
            bin(n >> 1);
 
        Console.Write(n & 1);
    }
 
    // Driver code
    public static void Main()
    {
        bin(131);
        Console.WriteLine();
        bin(3);
    }
}
/*This code is contributed by PrinciRaj1992*/


PHP
 1)
    bin($n>>1);
     
    echo ($n & 1);
}
 
// Driver code
bin(131);
echo "\n";
bin(3);
 
// This code is contributed
// by Akanksha Rai


Javascript


Python3
def binary(num):
    return int(bin(num).split('0b')[1])
 
if __name__ == "__main__" :
    x = 10
    binary_x = binary(x)
    print(binary_x)
 
# This code is contributed by Rishika Gupta.


输出
00000000000000000000000000000111
00000000000000000000000000000100

方法2:递归
以下是打印“ NUM”的二进制表示形式的递归方法。

step 1) if NUM > 1
    a) push NUM on stack
    b) recursively call function with 'NUM / 2'
step 2)
    a) pop NUM from stack, divide it by 2 and print it's remainder.

C++

// C++ Program for the binary
// representation of a given number
#include 
using namespace std;
 
void bin(unsigned n)
{
    /* step 1 */
    if (n > 1)
        bin(n / 2);
 
    /* step 2 */
    cout << n % 2;
}
 
// Driver Code
int main(void)
{
    bin(7);
    cout << endl;
    bin(4);
}
 
// This code is contributed
// by Akanksha Rai

C

// C Program for the binary
// representation of a given number
void bin(unsigned n)
{
    /* step 1 */
    if (n > 1)
        bin(n / 2);
 
    /* step 2 */
    printf("%d", n % 2);
}
 
// Driver Code
int main(void)
{
    bin(7);
    printf("\n");
    bin(4);
}

Java

// Java Program for the binary
// representation of a given number
class GFG {
    static void bin(int n)
    {
        /* step 1 */
        if (n > 1)
            bin(n / 2);
 
        /* step 2 */
        System.out.print(n % 2);
    }
 
    // Driver code
    public static void main(String[] args)
    {
        bin(7);
        System.out.println();
        bin(4);
    }
}
 
// This code is contributed
// by ChitraNayal

Python3

# Python3 Program for the binary
# representation of a given number
 
 
def bin(n):
 
    if n > 1:
        bin(n//2)
 
    print(n % 2, end="")
 
 
# Driver Code
if __name__ == "__main__":
 
    bin(7)
    print()
    bin(4)
 
# This code is contributed by ANKITRAI1

C#

// C# Program for the binary
// representation of a given number
using System;
 
class GFG {
 
    static void bin(int n)
    {
 
        // step 1
        if (n > 1)
            bin(n / 2);
 
        // step 2
        Console.Write(n % 2);
    }
 
    // Driver code
    static public void Main()
    {
        bin(7);
        Console.WriteLine();
        bin(4);
    }
}
 
// This code is contributed ajit

的PHP

 1)
        bin($n/2);
 
    /* step 2 */
    echo ($n % 2);
}
 
// Driver code
bin(7);
echo ("\n");
bin(4);
 
// This code is contributed
// by Shivi_Aggarwal
?>

Java脚本


输出
111
100

方法3:使用按位运算运算符递归
下面给出了将十进制数转换为其二进制表示形式的步骤:

step 1: Check n > 0
step 2: Right shift the number by 1 bit and recursive function call
step 3: Print the bits of number

C++

// C++ implementation of the approach
#include 
using namespace std;
 
// Function to convert decimal
// to binary number
void bin(unsigned n)
{
    if (n > 1)
        bin(n >> 1);
 
    printf("%d", n & 1);
}
 
// Driver code
int main(void)
{
    bin(131);
    printf("\n");
    bin(3);
    return 0;
}

Java

// Java implementation of the approach
 
class GFG {
 
    // Function to convert decimal
    // to binary number
    static void bin(Integer n)
    {
        if (n > 1)
            bin(n >> 1);
 
        System.out.printf("%d", n & 1);
    }
 
    // Driver code
    public static void main(String[] args)
    {
        bin(131);
        System.out.printf("\n");
        bin(3);
    }
}
/*This code is contributed by PrinciRaj1992*/

Python3

# Python 3 implementation of above approach
 
# Function to convert decimal to
# binary number
 
 
def bin(n):
 
    if (n > 1):
        bin(n >> 1)
    print(n & 1, end="")
 
 
# Driver code
bin(131)
print()
bin(3)
 
# This code is contributed by PrinciRaj1992

C#

// C# implementation of above approach
using System;
 
public class GFG {
 
    // Function to convert decimal
    // to binary number
    static void bin(int n)
    {
        if (n > 1)
            bin(n >> 1);
 
        Console.Write(n & 1);
    }
 
    // Driver code
    public static void Main()
    {
        bin(131);
        Console.WriteLine();
        bin(3);
    }
}
/*This code is contributed by PrinciRaj1992*/

的PHP

 1)
    bin($n>>1);
     
    echo ($n & 1);
}
 
// Driver code
bin(131);
echo "\n";
bin(3);
 
// This code is contributed
// by Akanksha Rai

Java脚本


输出
10000011
11

方法4:内置的Python库

Python3

def binary(num):
    return int(bin(num).split('0b')[1])
 
if __name__ == "__main__" :
    x = 10
    binary_x = binary(x)
    print(binary_x)
 
# This code is contributed by Rishika Gupta.
输出
1010

https://youtu.be/p0Vyq2_Q_uI