![]() |
|
#1
|
|||
|
|||
Calculating two's complementI'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
|
|||
|
|||
|
You can scan in the number as a hexidecimal with the sscanf() function:
CPP / C++ / C Code:
|
|
#3
|
||||
|
||||
|
Quote:
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... __________________
The best damn Sports Blog period. |
|
#4
|
||||
|
||||
|
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:
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:
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. __________________
The best damn Sports Blog period. |
|
#5
|
|||
|
|||
|
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
|
||||
|
||||
|
Quote:
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:
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... __________________
The best damn Sports Blog period. |
|
#7
|
|||
|
|||
|
Quote:
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
|
||||
|
||||
|
Quote:
Quote:
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
|
|||
|
|||
|
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
|
||||
|
||||
|
Quote:
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... __________________
The best damn Sports Blog period. |
Recent GIDBlog
Stupid Management Policies by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
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