
{"id":671,"date":"2019-03-20T11:25:19","date_gmt":"2019-03-20T11:25:19","guid":{"rendered":"http:\/\/blogs.plymouth.ac.uk\/embedded-systems\/?p=671"},"modified":"2019-03-20T11:26:47","modified_gmt":"2019-03-20T11:26:47","slug":"carry-and-overflow-are-not-the-same-thing","status":"publish","type":"post","link":"https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/2019\/03\/20\/carry-and-overflow-are-not-the-same-thing\/","title":{"rendered":"Carry and Overflow are not the same thing!"},"content":{"rendered":"<p>I get questions about this a lot, so I thought I&#8217;d try and explain this. If we are to build an adder circuit from digital electronics, there is only one circuit which performs addition<\/p>\n<blockquote><p>signed and unsigned adder circuits are identical<\/p><\/blockquote>\n<p>The overflow and carry bits will be set or reset depending on the input values you apply. It\u2019s more question of interpreting their\u00a0<i>meaning<\/i>\u00a0when you (mentally) convert back to decimal.<\/p>\n<ul>\n<li><strong>Contrary to what you might read, carry and overflow bits are not the same thing<\/strong><\/li>\n<li>If you consider your inputs to be signed, then you can simply ignore the carry bit (even if it is set) as it has no meaning for signed arithmetic<\/li>\n<li>If you consider your inputs to be unsigned, then you can simply ignore the overflow bit (even if it is set) as it has no meaning for unsigned arithmetic<\/li>\n<\/ul>\n<p>The carry and overflow bits are part of the device and always present, it\u2019s just a question of how to interpret them. Remember the electronics knows nothing of signed or unsigned arithmetic \u2013 they are just binary values.\u00a0It is all a matter of (human) interpretation.<\/p>\n<p>For example, consider some 4-bit numbers:<\/p>\n<ul>\n<li>Unsigned 4-bit values range from 0..15<\/li>\n<li>Signed 4-bit values range from -8 to +7<\/li>\n<\/ul>\n<p>Remember that signed numbers employ 2&#8217;s compliment arithmetic to ensure that binary addition and subtraction are consistent (e.g. x+(-x) = 0), as illustrated in one of the examples below.<\/p>\n<p>Examples:<\/p>\n<ul>\n<li>0111b is interpreted as +7 in decimal, whether signed or unsigned.<\/li>\n<li>1000b is interpreted as +8 for unsigned , and -8 for signed.<\/li>\n<li>1001b is interpreted as +9 for unsigned , and -7 for signed.<\/li>\n<\/ul>\n<p>The computer does not know any of the above \u2013 it just sees binary \u2013 how we choose to interpret the meaning is our decision.<\/p>\n<p>Consider this addition:<\/p>\n<pre>  0111b\r\n  1001b\u00a0 +\r\n1 0000b\r\ncarry = 1\r\noverflow = 0<\/pre>\n<p>The carry bit is set, the overflow is not set.<\/p>\n<ul>\n<li>For signed arithmetic, this was 7 + (-7) = 0 (we ignore the carry for signed arithmetic).\n<ul>\n<li>When you add a positive quantity to a negative quantity, it can never overflow<\/li>\n<\/ul>\n<\/li>\n<li>For unsigned arithmetic, this was 7 + 9 = 0, carry 1 (here the carry can be considered the msb &#8211; making this 16 in decimal)\n<ul>\n<li>Note how a carry is outside the 4-bit field<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>Now this:<\/p>\n<pre class=\"\">  0111b\r\n  0001b\u00a0 +\r\n0 1000b\r\ncarry = 0\r\noverflow = 1<\/pre>\n<p>The carry bit is not set, but the overflow is (because two positive inputs produced a negative output).<\/p>\n<ul>\n<li>For signed arithmetic, this was 7 + 1 = -8 (overflow is set, we ignore the carry)\n<ul>\n<li>The overflow it set because the result has a different sign to both inputs<\/li>\n<li>An overflow can only occur if both inputs have the same sign.<\/li>\n<\/ul>\n<\/li>\n<li>For unsigned arithmetic, this was 7 + 1 = +8, carry 0 (ignore the overflow, but again we can use the carry as the msb of a 5-bit result if we choose)<\/li>\n<\/ul>\n<pre class=\"\">  0011b\r\n  0001b\u00a0 +\r\n0 0100b\r\ncarry = 0\r\noverflow = 0<\/pre>\n<ul>\n<li>For signed arithmetic, this was 3 + 1 = +4 (overflow is 0, we ignore the carry)\n<ul>\n<li>The result has the same sign as both inputs<\/li>\n<\/ul>\n<\/li>\n<li>For unsigned arithmetic, this was 3 + 1 = 4, carry 0 (ignore the overflow)<\/li>\n<\/ul>\n<pre class=\"\">  1101b\r\n  1111b\u00a0 +\r\n1 1100b\r\ncarry = 1\r\noverflow = 0<\/pre>\n<ul>\n<li>For signed arithmetic, this was -3 + (-1) = -4 (overflow is 0, we ignore the carry)\n<ul>\n<li>The result has the same sign as both inputs<\/li>\n<\/ul>\n<\/li>\n<li>For unsigned arithmetic, this was 13 + 15 = 12, (carry 1, we ignore the overflow)<\/li>\n<\/ul>\n<p><b>Whether we consider the values signed or unsigned is up to us,\u00a0<u>the electronics is the same<\/u>. All that changes is our interpretation of the result<\/b><\/p>\n<p>I hope this clears things up.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I get questions about this a lot, so I thought I&#8217;d try and explain this. If we are to build an adder circuit from digital electronics, there is only one circuit which performs addition signed and unsigned adder circuits are identical The overflow and carry bits will be set or reset depending on the input&hellip; <a class=\"more-link\" href=\"https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/2019\/03\/20\/carry-and-overflow-are-not-the-same-thing\/\">Continue reading <span class=\"screen-reader-text\">Carry and Overflow are not the same thing!<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-671","post","type-post","status-publish","format-standard","hentry","category-uncategorized","entry"],"_links":{"self":[{"href":"https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-json\/wp\/v2\/posts\/671","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-json\/wp\/v2\/comments?post=671"}],"version-history":[{"count":2,"href":"https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-json\/wp\/v2\/posts\/671\/revisions"}],"predecessor-version":[{"id":673,"href":"https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-json\/wp\/v2\/posts\/671\/revisions\/673"}],"wp:attachment":[{"href":"https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-json\/wp\/v2\/media?parent=671"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-json\/wp\/v2\/categories?post=671"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-json\/wp\/v2\/tags?post=671"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}