{"id":2010,"date":"2016-07-16T00:01:29","date_gmt":"2016-07-16T07:01:29","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=2010"},"modified":"2022-11-26T10:09:01","modified_gmt":"2022-11-26T18:09:01","slug":"more-negative-integers","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=2010","title":{"rendered":"More Negative Integers"},"content":{"rendered":"<p>The far left bit in a <em>signed<\/em> integer value &mdash; no matter how wide the integer &mdash; is the sign bit. If it&#8217;s set, the value is negative. Otherwise, the value is positive. But the sign bit is more than just a minus sign. It also plays into binary math.<br \/>\n<!--more--><br \/>\n<em>Binary math<\/em>. There&#8217;s a term to plunge a college sophomore into convulsions. It&#8217;s not that terrible of a concept. That&#8217;s because the early proto-nerds figured everything out for you. You just need to accept it.<\/p>\n<p>For example, consider the elementary school math problem: 9 &#8211; 5<\/p>\n<p>In your head (I hope), you know the result is 4. From your programming experience, you might even know that hexadecimal <code>0x09<\/code> minus <code>0x05<\/code> is equal to <code>0x04<\/code>. That&#8217;s simple enough. Here are the values in binary:<\/p>\n<p><code>9 = 0x09 = 0000-1001<br \/>\n5 = 0x05 = 0000-0101<br \/>\n4 = 0x04 = 0000-0100<\/code><\/p>\n<p>In the computer, however, the problem isn&#8217;t really 9 &#8211; 5. No, it&#8217;s 9+(-5). At the low level, the processor <em>adds<\/em> bits. Even when the machine language instruction is SUBTRACT, the processor is adding bits.<\/p>\n<p>In binary, -5 isn&#8217;t 1000-0101 it&#8217;s 1111-1011 or <code>0xFB<\/code>. I&#8217;ll show how that translation works in this Lesson&#8217;s sample code later, but for now understand that <code>0xFB<\/code> is the binary representation of -5. Math-wise, the problem is illustrated in Figure 1.<\/p>\n<div id=\"attachment_2012\" style=\"width: 460px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-2012\" src=\"http:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2016\/07\/0716-figure1-binary_subtraction.png\" alt=\"Figure 1. Binary addition of a positive and negative value.\" width=\"450\" height=\"299\" class=\"size-full wp-image-2012\" srcset=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2016\/07\/0716-figure1-binary_subtraction.png 450w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2016\/07\/0716-figure1-binary_subtraction-300x199.png 300w\" sizes=\"auto, (max-width: 450px) 100vw, 450px\" \/><p id=\"caption-attachment-2012\" class=\"wp-caption-text\">Figure 1. Binary addition of a positive and negative value.<\/p><\/div>\n<p>The values <code>0x09<\/code> (9) and <code>0xFB<\/code> (-5) are added in Figure 1. Binary addition works the same as the math you already know:<\/p>\n<p>0 + 0 = 0<br \/>\n0 + 1 = 1<br \/>\n1 + 1 = 0 and carry the 1 to the next digit<\/p>\n<p>For the math in Figure 1, the sum at bit 3 gets carried all the way over past the sign bit (bit 7) in the value. This condition sets what&#8217;s called the <em>carry bit<\/em> in the processor. It might also be called an overflow and set an <em>overflow flag<\/em>. Regardless, the final value is <code>0x04<\/code> or 0000-0100, which is the result of subtracting 5 from 9 &mdash; or adding 9 and -5.<\/p>\n<p>Don&#8217;t wrack your brain on this binary math! The processor handles all the details. A bigger question might be how do you get from 5 binary 0000-0101 to -5 binary 1111-1011. For that answer, you need to do some binary manipulation. The steps to convert a positive binary value to a negative binary value are:<\/p>\n<p><strong>1. Take the two&#8217;s complement of the original value, C operator:<\/strong> <code>~<\/code><br \/>\n<strong>2. Increment the result, C operator:<\/strong> <code>++<\/code><\/p>\n<p>Here&#8217;s sample code that implements this process:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nchar *binbin(char n);\r\n\r\nint main()\r\n{\r\n    int v;\r\n    char i;\r\n\r\n    printf(\"Enter a value (-127 to 127): \");\r\n    scanf(\"%d\",&amp;v);\r\n    i = (char)v;\r\n    printf(\"Value = %d\\n\",i);\r\n    printf(\"Binary: %s\\n\",binbin(i));\r\n    i = ~i;\r\n    printf(\"Two's complement = %d\\n\",i);\r\n    printf(\"Binary: %s\\n\",binbin(i));\r\n    i++;\r\n    printf(\"Increment result = %d\\n\",i);\r\n    printf(\"Binary: %s\\n\",binbin(i));\r\n\r\n    return(0);\r\n}\r\n\r\nchar *binbin(char n)\r\n{\r\n    static char bin[9];\r\n    int x;\r\n\r\n    for(x=0; x&lt;8; x++)\r\n    {\r\n        bin[x] = n & 0x80 ? '1' : '0';\r\n        n &lt;&lt;= 1;\r\n    }\r\n    bin[x] = '\\0';\r\n    return(bin);\r\n}<\/pre>\n<p>The code prompts for an input value between -127 and 127 (Line 10).  Though the range of an <em>signed char<\/em> value really goes to -128, that specific value creates problems with the integer conversion required at Line 12. (Otherwise, <em>int<\/em> variable <code>v<\/code> pads bits on the left.)<\/p>\n<p>At Lines 13 and 14, the original is shown in decimal and then binary, thanks to the <em>binbin()<\/em> function.<\/p>\n<p>Line 15 converts variable <code>i<\/code> into its two&#8217;s complement value. Then Lines 16 and 17 display the result in decimal and binary.<\/p>\n<p>Line 18 increments variable <code>i<\/code>, resulting in the final, negative value displayed in Lines 19 and 20.<\/p>\n<p>Run the code a few times to test its output. Here is a sample run:<\/p>\n<pre><code>Enter a value (-127 to 127): 5\r\nValue = 5\r\nBinary: 00000101\r\nTwo's complement = -6\r\nBinary: 11111010\r\nIncrement result = -5\r\nBinary: 11111011<\/code><\/pre>\n<p>Also, note that when you start with a negative number, you get a positive value in return:<\/p>\n<pre><code>Enter a value (-127 to 127): -100\r\nValue = -100\r\nBinary: 10011100\r\nTwo's complement = 99\r\nBinary: 01100011\r\nIncrement result = 100\r\nBinary: 01100100<\/code><\/pre>\n<p>That&#8217;s pretty solid. Further, here&#8217;s the result when you process the value zero:<\/p>\n<pre><code>Enter a value (-127 to 127): 0\r\nValue = 0\r\nBinary: 00000000\r\nTwo's complement = -1\r\nBinary: 11111111\r\nIncrement result = 0\r\nBinary: 00000000<\/code><\/pre>\n<p>And all the mathematicians shout, &#8220;Amen!&#8221;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>It&#8217;s an attitude thing. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=2010\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-2010","post","type-post","status-publish","format-standard","hentry","category-main"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2010","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2010"}],"version-history":[{"count":6,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2010\/revisions"}],"predecessor-version":[{"id":5665,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2010\/revisions\/5665"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2010"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2010"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2010"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}