 |
|
 |
| Parallax Forums > Public Forums > Propeller Chip > dealing with numbers that go negative (two's compliment) | Forum Quick Jump
|
|  James Long Lil Brother SMT Assembly Services

       Date Joined Aug 2004 Total Posts : 976 | Posted 2/26/2008 8:08 PM (GMT -8) |   | | OK....I basically stick with Spin....because I just don't have time to learn PASM.
I've hit a problem, that until this point, I have not encountered.
A number that can go negative, and when it does, it is in two's compliment.
How does a programmer let the chip know if the items goes to two's compliment?
I'm sort of confused.....as how to prevent the program from thinking the number just jumped to some huge unreasonable number.
I'm a little rusty with respect to programming....so be easy on me.
Partner/Designer
| | Back to Top | | |
 |  Mike Green Registered Member

       Date Joined Oct 2004 Total Posts : 13558 | Posted 2/26/2008 8:17 PM (GMT -8) |   | | Spin does all arithmetic in 32 bit signed arithmetic. All of the operators understand about negative numbers, so you don't have to tell the chip about anything. For a lot of the operators, the sign doesn't matter anyway like logical shifts or Boolean operations. Multiplication and division and the compare operators are signed operations. Things like addition and subtraction work the same whether signed or unsigned. | | Back to Top | | |
 |  James Long Lil Brother SMT Assembly Services

       Date Joined Aug 2004 Total Posts : 976 | Posted 2/26/2008 8:25 PM (GMT -8) |   |
Mike Green said... Spin does all arithmetic in 32 bit signed arithmetic. All of the operators understand about negative numbers, so you don't have to tell the chip about anything. For a lot of the operators, the sign doesn't matter anyway like logical shifts or Boolean operations. Multiplication and division and the compare operators are signed operations. Things like addition and subtraction work the same whether signed or unsigned. Mike,
Ok.....I see....so my little testing (Hyperterminal) is just a formating problem for the output for me.
Thanks for the reply......I didn't actually think about it that way.....but that makes sense.
I will have to put some led output and test the program to make sure the limit's are working right. I could go back and try to format the output....but it is not that important.
Partner/Designer
| | Back to Top | | |
     |  Rayman Registered Member
        Date Joined Jul 2007 Total Posts : 2408 | Posted 2/27/2008 5:05 PM (GMT -8) |   | This is another area that is easy to miss in the manual and could be handled better by a compiler with a more C-like syntax (like UINT and INT) declarations.
As I understand it though: All variables not declared as bytes or words are longs. All longs are signed. If you RDbyte or RDword the source is treated as unsigned and the result is always positive. | | Back to Top | | |
| Forum Information | Currently it is Saturday, November 21, 2009 11:31 AM (GMT -8) There are a total of 393,855 posts in 55,536 threads. In the last 3 days there were 84 new threads and 706 reply posts. View Active Threads
| | Who's Online | This forum has 17692 registered members. Please welcome our newest member, old guy. 65 Guest(s), 13 Registered Member(s) are currently online. Details heater, Siri, Kenny Gardner, keith_kw, Jay Kickliter, mctrivia, Alsowolfman, Shawn Lowe, dMajo, Harprit, Beau Schwabe (Parallax), Leon, MicroDirk |
Forum powered by dotNetBB v2.42EC SP2.02 dotNetBB © 2000-2009 |
|
|