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.

2
  • 5
    Please indent your code properly, it's unreadable now. Commented Sep 28, 2011 at 11:44
  • 1
    Please put some effort in the formatting of your questions if you expect answers. I indented the code, removed some empty lines and added some spaces, because int k = m % 2; definitely reads better than int k=m%2;. Commented Sep 28, 2011 at 11:54

2 Answers 2

1

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;
}
Sign up to request clarification or add additional context in comments.

4 Comments

That looks incorrect to me in that the loop does not depend on the loop variable at all...
made corrections. now works. @themel we just need to iterate thru all the bits in the number
True, no need to reference the loop variable, but the previous code still printed the same bit all over.
exactly. that was a mistake. and the second was in wrong bit mask. I used 0xfffffffe (i. e. -2) rather then 0x1
1

The simplest way to see the bits in C++ is to use std::bitset. It supports iostream output and conversion to/from string. Like this:

#include <stdio.h>      // printf
#include <limits.h>     // CHAR_BIT
#include <bitset>

int main()
{
    int const bitsPerByte   = CHAR_BIT;
    int const bitsPerInt    = sizeof( int )*bitsPerByte;
    int const m             = -10;
    std::bitset<bitsPerInt>     bits( m );

    printf(
        "%d in decimal is %s in %d-bits binary.\n",
        m,
        bits.to_string().c_str(),
        bitsPerInt
        );
}

Output:

-10 in decimal is 11111111111111111111111111110110 in 32-bits binary.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.