frankV
frankV

Reputation: 5513

Trouble with a Two's Complement Function

Trying to implement a function to return the two's complement of a string of bits. I've tried two varieties and get odd results.

Version 1 (does the inversion but not the "+1"): string twosComp(signed int number) {

string twosComp(signed int number) {

     if ( number == 0 ) { return "1"; }
     if ( number == 1 ) { return "0"; }

     if ( number % 2 == 0 ) {
         return twosComp(number / 2) + "1";
     }
     else {
         return twosComp(number / 2) + "0";
     }
}

Version 2 (inverts and attempts "+1" but doesn't always get it right)

string twosComp(signed int number) {

    bool bit = 0;
    int size = 3; // not sure what to do about this, value could be -32768 to 32767
    string twos;
    number = ~abs(number) + 1;

    for(int i = 0; i < size; i++) {

        //Get right-most bit
        bit = number & 1;
        if(bit) {
            twos += '1';
        }
        else {
            twos += '0';
        }

        //Shift all bits right one place
        number >>= 1;
    }

    return twos;
} // end twosComp

I've been trying various iterations of both of these functions. I'm running out of steam on this. If anyone has a better option -- I'm VERY open to suggestions at this point.

Upvotes: 0

Views: 5319

Answers (3)

Sandipan Karmakar
Sandipan Karmakar

Reputation: 297

For reference you can have a look at the below link for converting a integer to 2's compliment in C++ using bitset : http://2scomplimentcpp.blogspot.com.au/

#include <iostream>
#include <bitset>

using namespace std;

int disp_number()
{
    int i = 0;
    cout << "Enter Intiger : " ;
    cin >> i;
    cout << "decimal : " << std::dec << i << endl; 
    cout << "hex : " << std::hex << i << endl;
    cout << "oct : " << std::oct << i << endl;
    cout << "Binary : " << (bitset<16>)i << endl;
    cout << "Inverse : " << bitset<16>(~i) << endl;
    i = (0 <= i)?i:(-1)*i;
    cout << "One's compliment : " << ~(bitset<16>)i << endl;
    int d = ((bitset<16>)i).flip().to_ulong();
    cout << "Two's compliment : " << bitset<16>(++d) << endl;
    return 0;
}

You can use to_string() method of bitset to convert the representation to a string.

Upvotes: 0

Floris
Floris

Reputation: 46365

The following code does what you want for a short (16 bit) int: note - I wrote this in C not C++...

char* twosComplement(signed int n) {
    static char s[17];  // static so the variable persists after the call
    unsigned int i;
    int j;
    i = (2<<16)-n; // definition of twos complement

    for(j=0;j<16;j++){
        s[15-j] = ((i&1)==0)?'0':'1'; // test lowest bit
        printf("%c", s[15-j]);        // print for confirmation
        i=i>>1;                       // right shift by one
    }
    printf("\n"); // just to make output look clean
    s[16]='\0';   // terminate the string
    return s;
}

int main() {
printf("the string is %s\n", twosComplement(15)); // just an example
}

Upvotes: 0

calccrypto
calccrypto

Reputation: 8991

how about (abs(number) ^ 0xffffffff) + 1, and then turning that value into a string?

edit: also, why is size = 3? ints are 32 bits, usually

Upvotes: 3

Related Questions