LeafLabs Garden » Topic: Overflow mathematics
http://forums.leaflabs.com/topic.php?id=74060
A place to share, learn, and grow...en-USFri, 22 Jan 2016 00:17:34 +0000http://bbpress.org/?v=1.0.2<![CDATA[Search]]>q
http://forums.leaflabs.com/search.php
pyrohaz on "Overflow mathematics"
http://forums.leaflabs.com/topic.php?id=74060#post-104797
Sun, 20 Oct 2013 07:22:33 +0000pyrohaz104797@http://forums.leaflabs.com/<p>Oh! I didn't know that was actually how the twos complement is calculated, I knew of the twos complement obviously but thats awesome! There are so many clever engineers in this works, past and present. We've done about binary addition and subtraction through 2's complement (and why signed and offset methods don't work either). I meant 256 in the earlier post come to think of it but thank you for the clarification. Digital electronics is great!
</p>gbulmer on "Overflow mathematics"
http://forums.leaflabs.com/topic.php?id=74060#post-104795
Sat, 19 Oct 2013 10:09:15 +0000gbulmer104795@http://forums.leaflabs.com/<p>pyrohaz - if we work through your proposal:</p>
<p>A = 200<br />
B = 40</p>
<p>result = A - B<br />
= 200 + (255-40)<br />
= 200 (1100100) + 215 (11010111)<br />
= 415 (110011111)<br />
masked to 8 bits = 10011111 = 159<br />
which is incorrect.</p>
<p>The number 215 is suspicious.<br />
In binary, the bottom binary digit (bit) of a number determines if the number is odd or even.<br />
Adding or subtracting two even numbers always produces an even number, so 200-40 must be even.<br />
The operation 255-40 converts an even number to an odd number, so it is either wrong, or there are other steps missing from the algorithm. </p>
<p>As mlundinse has explained, taking the two's complement, is 256-x, and that does produce the correct binary value.<br />
Geting the two's complement is straightforward, and does not need the subtraction operation.<br />
Two's complement: flip all the bits and add one.<br />
Example 40: (00101000)<br />
-> flip all the bits (1's complement) = 11010111<br />
-> add 1 to 11010111 = 11011000 (216)<br />
Those operations are easy to implement (there must be an add implemented :-)</p>
<p>An important point which needs reinforcing is, the electronics to do the bit operations on the numbers for signed and unsigned binary addition is the same. The difference is how carry or overflow is interpreted. </p>
<p>So the nifty part is subtraction <strong>is</strong> addition using the two's complement of the subtrahend, even for unsigned arithmetic.<br />
Addition is the same for signed and unsigned numbers, the difference is the interpretation of the carry and top bit (sign) to identify overflow.
</p>mlundinse on "Overflow mathematics"
http://forums.leaflabs.com/topic.php?id=74060#post-104794
Sat, 19 Oct 2013 05:11:50 +0000mlundinse104794@http://forums.leaflabs.com/<p>First you should add 216 = 256-40 since the overflow subtracts 256.</p>
<p>Yes this is really the difference between signed and unsigned 8 bit numbers</p>
<p>The binary representation of unsigned 216 = signed -40 = 11011000'b</p>
<p>So this can, and it is, used but if used carelessly can add a lot of confusion.<br />
For instance if you are playing with 8 bit values on a 32 bit machine, can you<br />
be sure how the compiler/processor handles the overflow ?
</p>pyrohaz on "Overflow mathematics"
http://forums.leaflabs.com/topic.php?id=74060#post-104787
Fri, 18 Oct 2013 16:24:49 +0000pyrohaz104787@http://forums.leaflabs.com/<p>Hey guys,</p>
<p>This isn't really a question based on the maple, just a question based on digital logic. I was in a lecture earlier this week day dreaming when the lecturer was talking about 2's complement and I thought to myself why overflow mathematics hasn't been thought of? I know that obviously this doesn't allow for numbers below 0 but it seems really easy to implement in logic.</p>
<p>For example, say you have an 8 bit register and you're currently on 200 yet you want to subtract 40 to say 160, couldnt you surely add 215? Working this out through A-B = C</p>
<p>A = Number you want<br />
B = Number you have</p>
<p>A+(255-B). Obviously where 255 is the MSB number of the bit depth of the system?</p>
<p>Any ideas?
</p>