{"id":4685,"date":"2021-04-03T00:01:05","date_gmt":"2021-04-03T07:01:05","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=4685"},"modified":"2021-04-10T08:30:08","modified_gmt":"2021-04-10T15:30:08","slug":"sign-bits","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=4685","title":{"rendered":"Sign Bits"},"content":{"rendered":"<p>I recall the math class where negative numbers were introduced. I was appalled. From <em>Star Trek<\/em>, I easily accepted the concept of antimatter, but the notion of negative values deeply offended me.<br \/>\n<!--more--><br \/>\nAs I learned about computers, negative binary numbers blew me away. In a clutch of bits, which is essentially how values on a computer are stored, how can you have something negative? I mean, binary is just one and zero, right? Where is the negative?<\/p>\n<p>The answer is the <em>sign bit<\/em>.<\/p>\n<p>In a binary value, the left-most bit is the sign bit &mdash; for <em>signed<\/em> integer values. The <em>unsigned<\/em> integer type values treat the bit as part of the number. But for a <em>signed<\/em> value, the bit is 0 for a positive value or 1 for a negative value, as crudely illustrated in Figure 1.<\/p>\n<div id=\"attachment_4687\" style=\"width: 560px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2021\/04\/0403-figure1.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-4687\" src=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2021\/04\/0403-figure1.png\" alt=\"\" width=\"550\" height=\"253\" class=\"size-full wp-image-4687\" srcset=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2021\/04\/0403-figure1.png 550w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2021\/04\/0403-figure1-300x138.png 300w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2021\/04\/0403-figure1-500x230.png 500w\" sizes=\"auto, (max-width: 550px) 100vw, 550px\" \/><\/a><p id=\"caption-attachment-4687\" class=\"wp-caption-text\">Figure 1. How the computer interprets signed versus unsigned values.<\/p><\/div>\n<p>In Figure 1, the first value shown is -1, binary <code>11111111<\/code> for a <em>signed<\/em> byte-sized (<em>char<\/em>) integer value. For the <em>unsigned<\/em> data type, the value shown is 255. The latter value makes more sense to me. Therefore, to best illustrate how this nonsense works, a program is in order.<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2021_04_03-Lesson-a.c\" rel=\"noopener\" target=\"_blank\">2021_04_03-Lesson-a.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    signed char a = 0;\r\n    int x;\r\n\r\n    for( x=0; x&lt;256; x++ )\r\n    {\r\n        printf(\"%d\\n\",a);\r\n        a++;\r\n    }\r\n\r\n    return(0);\r\n}<\/pre>\n<p>This code loops through char values from 0 to 256. But because variable <code>a<\/code> is <em>signed<\/em>, once it huts the value where the sign bit is flipped from 0 to 1, negative values are output. Here is the interesting part of that output:<\/p>\n<p><code>123<br \/>\n124<br \/>\n125<br \/>\n126<br \/>\n127<br \/>\n-128<br \/>\n-127<br \/>\n-126<br \/>\n-125<\/code><\/p>\n<p>After 127 (binary <code>01111111<\/code>) the next value is -128 (binary <code>10000000<\/code>). Below, I&#8217;ve modified the code to include my <em>binbin()<\/em> function, which is showcased in my C books for outputting binary values:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2021_04_03-Lesson-b.c\" rel=\"noopener\" target=\"_blank\">2021_04_03-Lesson-b.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nchar *binbin(unsigned 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 &amp; 0x80 ? '1' : '0';\r\n        n &lt;&lt;= 1;\r\n    }\r\n    bin[x] = '\\0';\r\n    return(bin);\r\n}\r\n\r\nint main()\r\n{\r\n    signed char a = 0;\r\n    int x;\r\n\r\n    for( x=0; x&lt;256; x++ )\r\n    {\r\n        printf(\"%d\\t%s\\n\",a,binbin(a));\r\n        a++;\r\n    }\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The code runs the same, but with an additional column in the output. Again, here is the interesting part:<\/p>\n<p><code>123&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;01111011<br \/>\n124&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;01111100<br \/>\n125&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;01111101<br \/>\n126&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;01111110<br \/>\n127&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;01111111<br \/>\n-128&nbsp;&nbsp;&nbsp;&nbsp;10000000<br \/>\n-127&nbsp;&nbsp;&nbsp;&nbsp;10000001<br \/>\n-126&nbsp;&nbsp;&nbsp;&nbsp;10000010<br \/>\n-125&nbsp;&nbsp;&nbsp;&nbsp;10000011<\/code><\/p>\n<p>As soon as the 8th bit (for a <em>char<\/em>) flips, the values output are negative. Further, they flow upward as variable <code>a<\/code> increments. Eventually, the value returns to zero.<\/p>\n<p>The sign bit is significant for <em>signed<\/em> integer variables. In fact, you can force-flip the bit so that values turn negative, as demonstrated in the following code:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2021_04_03-Lesson-c.c\" rel=\"noopener\" target=\"_blank\">2021_04_03-Lesson-c.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nchar *binbin(unsigned 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 &amp; 0x80 ? '1' : '0';\r\n        n &lt;&lt;= 1;\r\n    }\r\n    bin[x] = '\\0';\r\n    return(bin);\r\n}\r\n\r\nint main()\r\n{\r\n    signed char a = 0;\r\n    int x;\r\n\r\n    for( x=0; x&lt;256; x++ )\r\n    {\r\n        a |= 0x80;\r\n        printf(\"%d\\t%s\\n\",a,binbin(a));\r\n        a++;\r\n    }\r\n\r\n    return(0);\r\n}<\/pre>\n<p>This code is identical to the preceding example, save for a new Line 24: <code>a |= 0x80<\/code>. This statement sets the first bit in the byte to 1, which makes all the output negative. Again, the same chunk of output from the sample run as shown earlier:<\/p>\n<p><code>-5&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;11111011<br \/>\n-4&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;11111100<br \/>\n-3&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;11111101<br \/>\n-2&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;11111110<br \/>\n-1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;11111111<br \/>\n-128&nbsp;&nbsp;&nbsp;&nbsp;10000000<br \/>\n-127&nbsp;&nbsp;&nbsp;&nbsp;10000001<br \/>\n-126&nbsp;&nbsp;&nbsp;&nbsp;10000010<br \/>\n-125&nbsp;&nbsp;&nbsp;&nbsp;10000011<\/code><\/p>\n<p>With the sign bit set, the first part of the output shows negative values, from -128 on up to -1. Then the loop starts over again at -128.<\/p>\n<p>By the way, <code>10000000<\/code> is interpreted as -128 and not &#8220;negative zero.&#8221; Unlike other programming languages, C lacks an equivalent for negative zero. I explore this notion in <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4697\">next week&#8217;s Lesson<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Negative binary values are quite clever in their implementation. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4685\">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-4685","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\/4685","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=4685"}],"version-history":[{"count":7,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4685\/revisions"}],"predecessor-version":[{"id":4719,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4685\/revisions\/4719"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4685"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4685"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4685"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}