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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:39:15+00:00 2026-05-26T00:39:15+00:00

I am trying to see how compiler writes in binary negative numbers, so here

  • 0

I am trying to see how compiler writes in binary negative numbers, so here is code which should do it

#include <cstdlib>
#include <iostream>
using namespace std;
void complement(int n)
{
    int l = n;
    int s = 0;
    int k = 0;
    int m = 0;

    while (n != 0)
    {
        s = n  %  2;
        n /= 2;
        cout << s << "  ";
    }

    cout << endl;
    m = ~l + 1;
    cout << m << endl;
    cout << endl;

    while (m != 0)
    {
        int k = m  %  2;
        m /= 2;
        cout << k << "  ";
    }
}

int main(int argc, char *argv[])
{
    int n;
    cin >> n;
    cout << endl;
    complement(n);
    system("PAUSE");
    return EXIT_SUCCESS;
}

but strange is that, when I enter 5, for instance, which clearly in binary form is 101 in 3 bit, its complement -5 is represented -10-1? This is what my code’s output shows me, but I know that is it not correct, because 2’s complement of any number is given by reversing its bits, 0 by 1 and vice-versa, and then +1, in case of 5(101), -5 will be (010+1)=(011). Please help me, how to correct my code so that, it could make complement correctly.

  • 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-05-26T00:39:16+00:00Added an answer on May 26, 2026 at 12:39 am

    If you want to see the bits of number you better use such constructs:

    int main(int argc, _TCHAR* argv[])
    {
        int i = -10; //my value
        std::string result;
        for (int bit = 0; bit < sizeof(int)*8; ++bit)
        {
           int bit_val = 1 & i;
           result = (bit_val ? "1" : "0") + result;
           i = i >> 1;
        }
        std::cout << result << std::endl;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

While trying to compile a 64 bit linux kernel using gcc, I see the
I'm trying to see if anyone knows how to cluster some Lat/Long results, using
Using Microsoft Visual C++ I am trying to compile the file "test.cc" which is
I'm trying to compile a simple example of generic classes / type patterns (see
I'm trying to see if there's a way to get a consistent unit test
I am trying to see from an SQL console what is inside an Oracle
I'm trying to see the value of a variable, but the debugger shows me
I'm trying to see if there is a way to build a Linq statement
I am trying to see if an element have a specific CSS attribute. I
I am trying to see if an option was selected in a selectbox and

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.