Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7967089
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T06:36:23+00:00 2026-06-04T06:36:23+00:00

convert a positive integer number in C++ (0 to 2,147,483,647) to a 32 bit

  • 0

convert a positive integer number in C++ (0 to 2,147,483,647) to a 32 bit binary and display.

I want do it in traditional “mathematical” way (rather than use bitset or use vector *.pushback* or recursive function or some thing special in C++…), (one reason is so that you can implement it in different languages, well maybe)

So I go ahead and implement a simple program like this:

#include <iostream>
using namespace std;
int main()
{
    int dec,rem,i=1,sum=0;
    cout << "Enter the decimal to be converted: ";
    cin>>dec;
    do
    {
        rem=dec%2;
        sum=sum + (i*rem);
        dec=dec/2;
        i=i*10;
    } while(dec>0);

    cout <<"The binary of the given number is: " << sum << endl;

    system("pause");
    return 0;
}

Problem is when you input a large number such as 9999, result will be a negative or some weird number because sum is integer and it can’t handle more than its max range, so you know that a 32 bit binary will have 32 digits so is it too big for any number type in C++?. Any suggestions here and about display 32 bit number as question required?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T06:36:24+00:00Added an answer on June 4, 2026 at 6:36 am

    Using a for-loop, and a predefined array of zero-chars:

    #include <iostream>
    using namespace std;
    int main()
    {
        int dec;
        cout << "Enter the decimal to be converted: ";
        cin >> dec;
    
        char bin32[]  = "00000000000000000000000000000000";
        for (int pos = 31; pos >= 0; --pos)
        {
            if (dec % 2) 
                bin32[pos] = '1';
            dec /= 2;
        }
    
        cout << "The binary of the given number is: " << bin32 << endl;
    }
    

    For performance reasons, you may prematurely suspend the for loop:

        for (int pos = 31; pos >= 0 && dec; --pos)
    

    Note, that in C++, you can treat an integer as a boolean – everything != 0 is considered true.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How do I convert a positive Integer to a negative with Delphi? I know
You can convert a negative number to positive like this: int myInt = System.Math.Abs(-5);
How can I convert a negative number to positive in Python? (And keep a
I am looking for a way to convert any number to a percentage in
I can use Convert.ToString to convert an integer to its binary representation: int x
My question is related to Algorithm to convert any positive integer to an RGB
I need to convert numbers, positive and negative, into binary format - so, 2
I want to convert a 64-width binary string to long, there is a static
I created a structure to represent a fixed-point positive number. I want the numbers
I want to convert a view ID into a URL using EL. The purpose

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.