{"id":1987,"date":"2016-07-02T00:01:00","date_gmt":"2016-07-02T07:01:00","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=1987"},"modified":"2016-07-09T07:17:56","modified_gmt":"2016-07-09T14:17:56","slug":"to-zero-and-back","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=1987","title":{"rendered":"To Zero and Back"},"content":{"rendered":"<p>Many first-time programmers rush over variable type descriptions and head full-bore into coding. That&#8217;s fine. I did it. You probably did it. But eventually you encounter code that doesn&#8217;t work properly because of negative numbers. That&#8217;s when you go back and review the concept of negative integer variable types.<br \/>\n<!--more--><br \/>\nUnlike floating point numbers, integers are stored in memory using plain old binary. If you can read binary (or hex), you can look at a clutch of bits and quickly divine what the value could be. In fact, nerds pride themselves on such abilities:<\/p>\n<p><code>0110 0101 1110 0001 = 0x65E1 = 26081<\/code><\/p>\n<p>The <em>int<\/em> variables you create in C are signed unless you specify the <em>unsigned<\/em> type. That means those variables can store both positive and negative values. A <em>signed<\/em> keyword exists to assert that fact, but it&#8217;s redundant for plain <em>int<\/em>, <em>char<\/em>, <em>long<\/em>, <em>short<\/em>, and <em>long long<\/em> variables.<\/p>\n<p>This issue with signed variables is that at a certain point, large values become negative. This effect can get your code into trouble if you&#8217;re unaware of negative binary numbers.<\/p>\n<p>Unlike reading positive binary numbers, reading a negative binary value isn&#8217;t something most nerds can do:<\/p>\n<p><code>1000 0000 0000 0011 = 0x8003 = -32765<\/code><\/p>\n<p>The far left bit in a signed integer value is the sign bit. When set, it determines that the value stored is negative. That bit doesn&#8217;t work like the minus sign in decimal math. The value above isn&#8217;t -3, it&#8217;s -32765. In fact, -3 looks like this when stored in a <em>short signed int<\/em> variable:<\/p>\n<p><code>1111 1111 1111 1101 = 0xFFFD = -3<\/code><\/p>\n<p>That representation might make no sense until you understand how binary values are counted and what happens when the bits keep flipping in the same direction.<\/p>\n<p>In the following program, <em>char<\/em> variable <code>z<\/code> is signed. It starts at 1 and loops &#8220;up&#8221; to zero. It does this because after positive value 127, the <em>signed char<\/em> variable <code>z<\/code> sets its far left bit to 1, making the values negative. The next value after 127 is -128, which then steps up to zero:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    char z;\r\n\r\n    z = 1;\r\n    while(z != 0)\r\n    {\r\n        printf(\"%d\\n\",z);\r\n        z++;\r\n    }\r\n\r\n    return(0);\r\n}<\/pre>\n<p>Here is a snippet of the output:<\/p>\n<pre><code>1\r\n2\r\n3\r\n4\r\n...\r\n126\r\n127\r\n-128\r\n-127\r\n...\r\n-4\r\n-3\r\n-2\r\n-1<\/code><\/pre>\n<p>If you modify the code, and edit Line 5 to read:<\/p>\n<pre><code>unsigned char z<\/code><\/pre>\n<p>Then the output looks like this (snippet):<\/p>\n<pre><code>1\r\n2\r\n3\r\n4\r\n...\r\n252\r\n253\r\n254\r\n255<\/code><\/pre>\n<p>The program stops at 255 because at that point, all the bits in the value flip back to zero.<\/p>\n<p>The same binary values are used in both programs, but when the integer variable is <em>signed<\/em>, the output shows negative numbers.<\/p>\n<p>The <em>signed int<\/em> and <em>unsigned int<\/em> values also explains why the range of integer numbers is show in two sets. Refer to Appendix D in <em>Beginning C Programming For Dummies<\/em> to view the values and ranges.<\/p>\n<p>In <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=1989\">next week&#8217;s Lesson<\/a>, I explore further the concept of negative Integers and how they work.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>How negative values are stored in integers. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=1987\">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-1987","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\/1987","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=1987"}],"version-history":[{"count":6,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1987\/revisions"}],"predecessor-version":[{"id":2019,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1987\/revisions\/2019"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1987"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1987"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1987"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}