GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 12-Nov-2004, 09:47
pinball27 pinball27 is offline
New Member
 
Join Date: Nov 2004
Posts: 5
pinball27 is on a distinguished road
Question

Calculating two's complement


I'm trying to write a program for an electronic test application and am having problems figuring out how to calculate two's complement then display the value as a decimal. I receive hex data through a com port as a character sting then convert the piece of the string I need to an integer (e.g. 150h). I’m going insane trying to get two’s complement of this number and display the decimal value to the operator (e.g. 176d).

Thanks in advance for any help.
  #2  
Old 12-Nov-2004, 09:52
Dr. Evil Dr. Evil is offline
Member
 
Join Date: Oct 2004
Location: Netherlands
Posts: 120
Dr. Evil will become famous soon enough
You can scan in the number as a hexidecimal with the sscanf() function:
CPP / C++ / C Code:
//input contains hexidecimal number
char hex;

sscanf(input, "%x", &hex);
//...
  #3  
Old 12-Nov-2004, 10:02
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Quote:
Originally Posted by pinball27
I'm trying to write a program for an electronic test application and am having problems figuring out how to calculate two's complement then display the value as a decimal. I receive hex data through a com port as a character sting then convert the piece of the string I need to an integer (e.g. 150h). I’m going insane trying to get two’s complement of this number and display the decimal value to the operator (e.g. 176d).

Thanks in advance for any help.

Hi pinball. First of, welcome to GIDForums™. I remember reviewing 2's complements in another topic a while back. Unfortunately, this thread only explains what 2's complement is and doesn't provide a C code for it. If no one else responds with something, I will see if I can code up that explanation...
  #4  
Old 12-Nov-2004, 10:38
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Hi again pinball. The twos compliment is actually very straightforward if you think about it based on the way that computers store negative numbers:

CPP / C++ / C Code:
twos_compliment = -number;

That should be it. Here is a little test program that takes a twos compliment the hard way and the easy way:

CPP / C++ / C Code:
int main()
{
	int		number;
	int		two;

	number = 100;
	two = ~number;		//One's compliment
	two = two + 1;		//Two's compliment

	printf("Original number is: %d(%x)\n",number,number);
	printf("Two's compliment is: %d(%x)\n",two,two);
	printf("Unsiged is: %u\n",two);
	printf("Negative number is: %d(%x)\n",-number,-number);

	return 0;

}

As far as displaying your number as a decimal, it should be real easy with the printf command. Just tell printf command what you want to display. There is no conversion. The most popular tutorials on GIDForums™
discusses this. We get alot of people looking for answers how to "convert" from decimal to binary to hexidecimal etc. The quick answer is there is no need to.

I hope this helps. Let me know if it doesn't answer your question.
  #5  
Old 12-Nov-2004, 12:29
pinball27 pinball27 is offline
New Member
 
Join Date: Nov 2004
Posts: 5
pinball27 is on a distinguished road
I see what the test program is doing but for some reason it doesn't work for my application. The number I should get is positive when I manually do the math, for example:

336d = 101010000
1's complement = 010101111
+1
2's complement = 010110000 = B0h or 176d

Am I formating somthing wrong or am I missing somthing else?

Thanks.
  #6  
Old 12-Nov-2004, 13:07
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Quote:
Originally Posted by pinball27
I see what the test program is doing but for some reason it doesn't work for my application. The number I should get is positive when I manually do the math, for example:

336d = 101010000
1's complement = 010101111
+1
2's complement = 010110000 = B0h or 176d

Am I formating somthing wrong or am I missing somthing else?

Thanks.

haha! I think I see what the problem is (potentially). The twos compliment is going to switch every bit of your number.

So 336 decimal is actually 0000000101010000 (based on a short)
The twos compliment is: 1111111010110000 (FEB0).

Here is a sample program (using code stolen from WaltP ) that should show this. Notice that I changed to a short int (4 bytes) as an int (8 bytes) gives even more distorted results.
CPP / C++ / C Code:
void convert(short bin, char *str)
{
    unsigned short mask;      // used to check each individual bit, unsigned
                            //    to alleviate sign extension problems

    mask = 0x8000;      // Set only the high-end bit
    while (mask)            // Loop until MASK is empty
    {
        if (bin & mask)     // test the masked bit
              *str = '1';   // if true, value is 1
          else
              *str = '0';   // if false, value is 0
        str++;              // next character
        mask >>= 1;         // shift the mask 1 bit
    }
    *str = 0;               // add the trailing null
}


int main()
{
	short		number;
	short		two;
	char		binary[20];

	number = 336;
	two = ~number;		//One's compliment
	two = two + 1;		//Two's compliment

	printf("Original number is: %hd(%hx)\n",number,number);
	printf("Two's compliment is: %hd(%hx)\n",two,two);
	printf("Unsiged is: %hu\n",two);
	printf("Negative number is: %hd(%hx)\n",-number,-number);

	convert(number,binary);
	printf("Number in binary is: %s\n",binary);

	convert(two,binary);
	printf("Number in binary is: %s\n",binary);

	return 0;

}

So I guess the question is: what size of data are you working with? 336 does not fit into 2-bytes.

Not sure if that helps, but it should explain your differences...
  #7  
Old 12-Nov-2004, 14:04
pinball27 pinball27 is offline
New Member
 
Join Date: Nov 2004
Posts: 5
pinball27 is on a distinguished road
Quote:
Originally Posted by dsmith
haha! I think I see what the problem is (potentially). The twos compliment is going to switch every bit of your number.

So 336 decimal is actually 0000000101010000 (based on a short)
The twos compliment is: 1111111010110000 (FEB0).
.....<snip>
So I guess the question is: what size of data are you working with? 336 does not fit into 2-bytes.

Not sure if that helps, but it should explain your differences...

Ahh yes, it makes sense now. It was all the leading 1's that were throwing off my number. I think I know how to take care of that but I'll have to do some more testing.

Thanks again for all your help! I would have lost my mind with out it.
  #8  
Old 12-Nov-2004, 23:53
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,258
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Quote:
Originally Posted by dsmith
Here is a sample program (using code stolen from WaltP ) that should show this.
STOP! THIEF!!!!
Quote:
Originally Posted by dsmith
Notice that I changed to a short int (4 bytes) as an int (8 bytes) gives even more distorted results.
Are you working in 64 bits now??? You lucky stiff

D meant 2 and 4 above, I'll bet.
__________________

Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough.
-- Pearl Williams
  #9  
Old 13-Nov-2004, 06:36
Dr. Evil Dr. Evil is offline
Member
 
Join Date: Oct 2004
Location: Netherlands
Posts: 120
Dr. Evil will become famous soon enough
Oops... Ignore my post, thought he was talking about converting a hex string to an int. Sorry, I'm not the best at all the compliments, bitwise operators, etc. yet. Excuse my ignorance. What a waste of memory...
  #10  
Old 13-Nov-2004, 07:24
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Quote:
Originally Posted by WaltP

Are you working in 64 bits now??? You lucky stiff

D meant 2 and 4 above, I'll bet.

I just put things in there like that to make sure that you are still awake Walt.

Thanks, my bad. I was to intent on the problem at hand to sit down and count properly...
 
 

Recent GIDBlogStupid Management Policies by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Calculating mathematical string flowercamel C Programming Language 9 04-Feb-2004 14:55

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 04:34.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.