6 Replies  31826 Views  Last Post: 01 December 2010  01:24 PM
#1
Convert binary to 2's complement binary
Posted 30 November 2010  05:23 PM
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?
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
Quote
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 8bit 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
#4
Re: Convert binary to 2's complement binary
Posted 01 December 2010  07:14 AM
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 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
#7
Re: Convert binary to 2's complement binary
Posted 01 December 2010  01:24 PM
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.
