{"id":1989,"date":"2016-07-09T00:01:53","date_gmt":"2016-07-09T07:01:53","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=1989"},"modified":"2016-07-16T07:48:38","modified_gmt":"2016-07-16T14:48:38","slug":"making-negative-integers","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=1989","title":{"rendered":"Making Negative Integers"},"content":{"rendered":"<p>In the C language, you have several ways to create a negative integer: You can assign a negative value to a variable, you can perform math that results in a negative value, or you can manipulate bits to convert a positive value to a negative one. That final operation isn&#8217;t as easy as it sounds.<br \/>\n<!--more--><br \/>\nTo review, in <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=1987\">last week&#8217;s Lesson<\/a> I demonstrated the difference between a signed and unsigned integer. The key is the left-most bit, which is interpreted as a sign bit in signed integer values. The sign bit is not the same as a minus sign.<\/p>\n<p>For example, you can&#8217;t just set the sign bit and &mdash; <em>presto<\/em>! &mdash; you have a negative number. To wit:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    char s = 5;\r\n\r\n    printf(\"The value of 's' is %d\\n\",s);\r\n    s = s | 0x80;   <span class=\"comments\">\/* set left-most bit *\/<\/span>\r\n    printf(\"The value of 's' is %d\\n\",s);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>In the above code, Line 8 uses the bitwise | (OR) to set the far-left bit in <em>char<\/em> variable <code>s<\/code>. (<em>char<\/em> variables are 8-bits wide; <code>0x80<\/code> is binary 1000-0000.) That operation makes the value in variable <code>s<\/code> negative; no other bits are altered, but the result isn&#8217;t -5. Here&#8217;s the program&#8217;s output:<\/p>\n<pre><code>The value of 's' is 5\r\nThe value of 's' is -123<\/code><\/pre>\n<p>To view things on a binary level, below I&#8217;ve added the <em>binbin()<\/em> function to the code. This function is demonstrated in my book, <em>Beginning Programming with C For Dummies<\/em>:<\/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    char s = 5;\r\n\r\n    printf(\"The value of 's' is %d\\n\",s);\r\n    printf(\"Binary: %s\\n\",binbin(s));\r\n    s = s | 0x80;   <span class=\"comments\">\/* set left-most bit *\/<\/span>\r\n    printf(\"The value of 's' is %d\\n\",s);\r\n    printf(\"Binary: %s\\n\",binbin(s));\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>Here is the output, which confirms that the far-left bit in variable <code>s<\/code> was set:<\/p>\n<p><code><\/p>\n<pre>The value of 's' is 5\r\nBinary: 00000101\r\nThe value of 's' is -123\r\nBinary: 10000101<\/code><\/pre>\n<p>So what does -5 look like in binary? Here's yet another modification to the code, this time I simply assign the value -5 to variable <code>s<\/code>:<\/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    char s;\r\n\r\n    s = 5;\r\n    printf(\"Variable s = %d\\n\",s);\r\n    printf(\"Binary: %s\\n\",binbin(s));\r\n    s = -5;\r\n    printf(\"Variable s = %d\\n\",s);\r\n    printf(\"Binary: %s\\n\",binbin(s));\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>Here's the output:<\/p>\n<pre><code>Variable s = 5\r\nBinary: 00000101\r\nVariable s = -5\r\nBinary: 11111011<\/code><\/pre>\n<p>In <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=2010\">next week's Lesson<\/a>, I cover the binary method for converting a positive integer to a negative integer of the same value. Yes, that's a nerdy thing to do, but this is the C language and it often and deliberately encroaches upon the nerdy.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>You just can&#8217;t set an integer&#8217;s far-left bit to one and claim that you&#8217;ve made a value negative. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=1989\">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-1989","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\/1989","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=1989"}],"version-history":[{"count":7,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1989\/revisions"}],"predecessor-version":[{"id":2030,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1989\/revisions\/2030"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1989"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1989"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1989"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}