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 9232181
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T06:15:10+00:00 2026-06-18T06:15:10+00:00

Possible Duplicate: Efficiently convert between Hex, Binary, and Decimal in C/C++ Im taking an

  • 0

Possible Duplicate:
Efficiently convert between Hex, Binary, and Decimal in C/C++

Im taking an Assembly Language class and was asked to write an application to accept a signed integer as input and output the corresponding 2’s complement. I’ve been all over the internet trying to find code that would help, but the only thing that I can find is code that converts into exact binary (not the 16-bit format that I need with the leading zeroes). This is the code I have so far:

#include<iostream>
#include<string>
using namespace std;


string binaryArray[15] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};

void toBinary(int);
void convertNegative();

int main()
{
cout << "This app converts an integer from -32768 to 32767 into 16-bit 2's complement binary format" << endl;

cout << "Please input an integer in the proper range: ";

int num;
cin >> num;

if (num < -32768 || num > 32767)
    cout << "You have entered an unacceptable number, sorry." << endl;
if (num < 0)
{
    toBinary(num);
    convertNegative();
}
else
    toBinary(num);
cout << endl;

system("pause");
return 0;
}

My toBinary function was the function you can find on the internet for decimal to binary, but it only works if I am outputting to the console, and it doesn’t work on negative numbers, so I can’t take the 2’s complement. Any ideas?

  • 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-18T06:15:11+00:00Added an answer on June 18, 2026 at 6:15 am

    To compute the two’s complement of a number, you invert the number (change all of its 0 bits to 1, and all of its 1 bits to 0), and then add one. It’s that simple. But you’d only need to do that if the number is negative in the first place; the two’s complement of a non-negative number is simply the number itself (unconverted).

    But you’re taking a number as input, and storing it in your variable ‘num’. That variable IS in two’s complement form. That’s how your computer stores it. You don’t need to “convert” it to two’s complement form at all! If you simply shift the bits off one at a time & print them, you get the two’s complement of that number. Just shift them in the right order & pick off the leftmost bit each time, and you’ve got your solution. It’s really short & simple.

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

Sidebar

Related Questions

Possible Duplicate: Best practices for efficiently storing md5 hashes in mysql We use Hex
Possible Duplicate: How do you efficiently generate a list of K non-repeating integers between
Possible Duplicate: How would you implement tail efficiently? A friend of mine was asked
Possible Duplicate: How to efficiently write to file from SQL datareader in c#? I
Possible Duplicate: How can I convert a list<> to a multi-dimensional array? I want
Possible Duplicate: PHP: Convert uncommon date format to timestamp in most efficient manner possible?
Possible Duplicate: How do you convert Byte Array to Hexadecimal String, and vice versa?
Possible Duplicate: MATLAB: How To Efficiently Remove NaN Elements from Matrix I got code
Possible Duplicate: How to efficiently count the number of keys/properties of an object in
Possible Duplicate: How to efficiently count the number of keys/properties of an object in

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.