C++ - Decimal to binary converting

后端 未结 29 3452
一向
一向 2020-11-28 18:53

I wrote a \'simple\' (it took me 30 minutes) program that converts decimal number to binary. I am SURE that there\'s a lot simpler way so can you show me? Here\'s the code:<

相关标签:
29条回答
  • 2020-11-28 19:52

    A pretty straight forward solution to print binary:

    #include <iostream>
    using namespace std;
    int main()
    {
     int num,arr[64];
     cin>>num;
     int i=0,r;
     while(num!=0)
    {
      r = num%2;
      arr[i++] = r;
      num /= 2;
    }
    
    for(int j=i-1;j>=0;j--){
     cout<<arr[j];
      }
    }
    
    0 讨论(0)
  • 2020-11-28 19:52
    // function to convert decimal to binary
    void decToBinary(int n)
    {
        // array to store binary number
        int binaryNum[1000];
    
        // counter for binary array
        int i = 0;
        while (n > 0) {
    
            // storing remainder in binary array
            binaryNum[i] = n % 2;
            n = n / 2;
            i++;
        }
    
        // printing binary array in reverse order
        for (int j = i - 1; j >= 0; j--)
            cout << binaryNum[j];
    }
    

    refer :- https://www.geeksforgeeks.org/program-decimal-binary-conversion/

    or using function :-

    #include<bits/stdc++.h>
    using namespace std;
    
    int main()
    {
    
        int n;cin>>n;
        cout<<bitset<8>(n).to_string()<<endl;
    
    
    }
    

    or using left shift

    #include<bits/stdc++.h>
    using namespace std;
    int main()
    {
        // here n is the number of bit representation we want 
        int n;cin>>n;
    
        // num is a number whose binary representation we want
        int num;
        cin>>num;
    
        for(int i=n-1;i>=0;i--)
        {
            if( num & ( 1 << i ) ) cout<<1;
            else cout<<0;
        }
    
    
    }
    
    0 讨论(0)
  • 2020-11-28 19:54

    std::bitset has a .to_string() method that returns a std::string holding a text representation in binary, with leading-zero padding.

    Choose the width of the bitset as needed for your data, e.g. std::bitset<32> to get 32-character strings from 32-bit integers.

    #include <iostream>
    #include <bitset>
    
    int main()
    {
        std::string binary = std::bitset<8>(128).to_string(); //to binary
        std::cout<<binary<<"\n";
    
        unsigned long decimal = std::bitset<8>(binary).to_ulong();
        std::cout<<decimal<<"\n";
        return 0;
    }
    

    EDIT: Please do not edit my answer for Octal and Hexadecimal. The OP specifically asked for Decimal To Binary.

    0 讨论(0)
  • 2020-11-28 19:54
    #include <iostream>
    using namespace std;
    
    int main()
    {  
        int a,b;
        cin>>a;
        for(int i=31;i>=0;i--)
        {
            b=(a>>i)&1;
            cout<<b;
        }
    }
    
    0 讨论(0)
  • 2020-11-28 19:54
    HOPE YOU LIKE THIS SIMPLE CODE OF CONVERSION FROM DECIMAL TO BINARY
    
    
      #include<iostream>
        using namespace std;
        int main()
        {
            int input,rem,res,count=0,i=0;
            cout<<"Input number: ";
            cin>>input;`enter code here`
            int num=input;
            while(input > 0)
            {
                input=input/2;  
                count++;
            }
    
            int arr[count];
    
            while(num > 0)
            {
                arr[i]=num%2;
                num=num/2;  
                i++;
            }
            for(int i=count-1 ; i>=0 ; i--)
            {
                cout<<" " << arr[i]<<" ";
            }
    
    
    
            return 0;
        }
    
    0 讨论(0)
提交回复
热议问题