Convert binary to 2's complement binary

binary, 2's, complement, convert

Page 1 of 1

6 Replies - 18805 Views - Last Post: 01 December 2010 - 01:24 PM Rate Topic: -----

#1 Ashley.O  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • 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  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1622
  • View blog
  • Posts: 5,709
  • 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

Was This Post Helpful? 0
  • +
  • -

#3 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • 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?
Was This Post Helpful? 0
  • +
  • -

#4 Ashley.O  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#5 Ashley.O  Icon User is offline

  • New D.I.C Head

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

Re: Convert binary to 2's complement binary

Posted 01 December 2010 - 07:38 AM

View PostAshley.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

Was This Post Helpful? 0
  • +
  • -

#6 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6058
  • View blog
  • Posts: 23,495
  • 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???

:code:
Was This Post Helpful? 0
  • +
  • -

#7 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1