# Convert binary to 2's complement binary

Page 1 of 1

## 6 Replies - 34029 Views - Last Post: 01 December 2010 - 01:24 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=202907&amp;s=8e17dc619a8f9954dc78678e663f9915&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Ashley.O

Reputation: -1
• Posts: 6
• Joined: 29-November 10

# Convert binary to 2's complement binary

Posted 30 November 2010 - 05:23 PM

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?

Is This A Good Question/Topic? 0

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

### #2 ishkabible

• spelling expret

Reputation: 1676
• Posts: 5,836
• Joined: 03-August 09

## 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?

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:

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 anonymous26

• D.I.C Lover

Reputation: 1
• Posts: 3,638
• Joined: 26-November 10

## 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 Ashley.O

Reputation: -1
• Posts: 6
• Joined: 29-November 10

## 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

```//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 Ashley.O

Reputation: -1
• Posts: 6
• Joined: 29-November 10

## 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 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

• Saucy!

Reputation: 6219
• Posts: 23,965
• Joined: 23-August 08

## 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 NickDMax

Reputation: 2254
• Posts: 9,245
• Joined: 18-February 07

## 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.