Hello,

What I am trying to do is take a whole number user input and convert this number to 2's complement binary http://en.wikipedia.org/wiki/Two's_complement .

I have been told it would be best to convert to binary, then convert to 2's complement binary?

I would love some help on this one as I am struggling!

As an aside, I can program this in C, but eventually I need to write it in BASIC. Crazy, I know. Can anyone help there?

## 6 Replies - 41149 Views - Last Post: 01 December 2010 - 01:24 PM

##
**Replies To:** Convert binary to 2's complement binary

### #2

## Re: Convert binary to 2's complement binary

Posted 30 November 2010 - 06:14 PM

what do you mean by converting it to binary? do you mean a string or bitfield? i say the best way to do it is to leave it as an integer and use bit-wise not plus one to get 2's complement. what dose Wikipedia say about it?

so (-x == ~x+1) should always be true right?

edit: i just realized that i didnt get the part that explains it from Wikipedia. lol that must seemed rather incoherent

Quote

Calculating two's complement

In two's complement notation, a positive number is represented by its ordinary binary representation, using enough bits that the high bit (the sign bit) is 0. The two's complement operation is the negation operation, so negative numbers are represented by the two's complement of the representation of the absolute value.

In finding the two's complement of a binary number, the bits are inverted, or "flipped", by using the bitwise NOT operation; the value of 1 is then added to the resulting value. Bit overflow is ignored, which is the normal case with the zero value.

For example, beginning with the signed 8-bit binary representation of the decimal value 5, using subscripts to indicate the base of a representation needed to interpret its value:

In two's complement notation, a positive number is represented by its ordinary binary representation, using enough bits that the high bit (the sign bit) is 0. The two's complement operation is the negation operation, so negative numbers are represented by the two's complement of the representation of the absolute value.

In finding the two's complement of a binary number, the bits are inverted, or "flipped", by using the bitwise NOT operation; the value of 1 is then added to the resulting value. Bit overflow is ignored, which is the normal case with the zero value.

For example, beginning with the signed 8-bit binary representation of the decimal value 5, using subscripts to indicate the base of a representation needed to interpret its value:

so (-x == ~x+1) should always be true right?

edit: i just realized that i didnt get the part that explains it from Wikipedia. lol that must seemed rather incoherent

This post has been edited by **ishkabible**: 30 November 2010 - 06:41 PM

### #3

## Re: Convert binary to 2's complement binary

Posted 30 November 2010 - 06:14 PM

Who told you it's best to convert to binary first?

### #4

## Re: Convert binary to 2's complement binary

Posted 01 December 2010 - 07:14 AM

I have read that once you have the binary, inverting it and adding one yields the two's complement equivalent.

I have some code started, but am really struggling. Here's what I've got

I have some code started, but am really struggling. Here's what I've got

//This doesnt take an input, but does this work? how to i get it to take a whole number input, convert to binary, then perform this?// #define SIZE 128 int a[SIZE] = {0}; int i, c, tmp; input a // a[i] = 0 or 1 for(i=0;i<SIZE;i++) a[i] = !a[i]; a[0] += 1; for(c=0, i=0;i<SIZE;i++){ tmp = a[i]; a[i] = (tmp + c)%2; c = (tmp + c)/2; } // This is another suggestion I am looking at. again, doesnt take an input (right?) but this goes from a whole number to 2's complement?// int i = 1; while (i) { printf(x & i ? "1" : "0"); i = i << 1; } printf("\n");

This post has been edited by **JackOfAllTrades**: 01 December 2010 - 08:38 AM

Reason for edit:: Added code tags

### #5

## Re: Convert binary to 2's complement binary

Posted 01 December 2010 - 07:38 AM

Ashley.O, on 01 December 2010 - 06:14 AM, said:

I have read that once you have the binary, inverting it and adding one yields the two's complement equivalent.

I have some code started, but am really struggling. Here's what I've got

// This is another suggestion I am looking at. again, doesnt take an input (right?) but this goes from a whole number to 2's complement?//

int i = 1; while (i) { printf(x & i ? "1" : "0"); i = i << 1; } printf("\n");[/code]

I have some code started, but am really struggling. Here's what I've got

//This doesnt take an input, but does this work? how to i get it to take a whole number input, convert to binary, then perform this?// #define SIZE 128 int a[SIZE] = {0}; int i, c, tmp; input a // a[i] = 0 or 1 for(i=0;i<SIZE;i++) a[i] = !a[i]; a[0] += 1; for(c=0, i=0;i<SIZE;i++){ tmp = a[i]; a[i] = (tmp + c)%2; c = (tmp + c)/2; }

// This is another suggestion I am looking at. again, doesnt take an input (right?) but this goes from a whole number to 2's complement?//

int i = 1; while (i) { printf(x & i ? "1" : "0"); i = i << 1; } printf("\n");[/code]

//Just came across this bit of code. Looks like it doesnt take an input, but converts binary to 2's complement and prints the string?// har *complement(unsigned int value){ int numchars = 8 * sizeof(unsigned int); int n; char *returnvalue = malloc((numchars + 1) * sizeof(char)); for(n = 0; n < numchars; n++){ if(value & (1 << (numchars - 1 - n))){ returnvalue[n] = '1'; }else{ returnvalue[n] = '0'; } } returnvalue[numchars] = (char)NULL; return returnvalue; }

This post has been edited by **JackOfAllTrades**: 01 December 2010 - 08:40 AM

Reason for edit:: Added code tags

### #6

## Re: Convert binary to 2's complement binary

Posted 01 December 2010 - 08:41 AM

Can you please make the effort to obey the rules and use code tags when posting code???

### #7

## Re: Convert binary to 2's complement binary

Posted 01 December 2010 - 01:24 PM

So to take the two's compliment of X your do: ~x + 1

So you need two steps. One to find the compliment, and another to add 1.

It would probably be more logical to break these into functions.

So you need two steps. One to find the compliment, and another to add 1.

It would probably be more logical to break these into functions.

Page 1 of 1