{"id":49,"date":"2006-08-22T19:03:13","date_gmt":"2006-08-23T00:03:13","guid":{"rendered":"http:\/\/www.nynaeve.net\/?p=49"},"modified":"2019-12-13T17:41:47","modified_gmt":"2019-12-13T22:41:47","slug":"beware-of-the-sign-bit","status":"publish","type":"post","link":"http:\/\/www.nynaeve.net\/?p=49","title":{"rendered":"Beware of the sign bit"},"content":{"rendered":"<p>Signedness can trip you up in unexpected ways.<\/p>\n<p>One example of this is the fairly innocuous <em>islower<\/em> function in the C standard library.  This function takes an <em>int<\/em> and returns a value indicating to you whether it represents a lowercase or uppercase character.<\/p>\n<p>The prototype for this function is:<\/p>\n<pre>int islower(\r\n   int c \r\n);\r\n<\/pre>\n<p>Unfortunately, due to how C works, this code is likely to introduce subtle bugs depending on the input you give it.  For instance, if you have a <em>char<\/em> array and iterate through it checking if each character is lowercase, you might naively write a program like so:<\/p>\n<pre>char ch[] = \"...\";\r\nfor (int = 0; ch[i]; i++)\r\n{\r\n if (islower(ch[i]))\r\n {\r\n   ...\r\n }\r\n}<\/pre>\n<p>This code has a nasty bug in it, though, in that if your compiler defaults to char as an 8-bit signed value (most every mainstream compiler on mainstream platforms does nowadays), and if you are given a character value that has more than 7 significant bits (say, 150), you will go off into undefined-behavior-land because the compiler will sign extend ch[i] to a negative <em>int<\/em> value of -150 instead of an <em>int<\/em> value of 150.  Depending on the implementation of <em>islower<\/em>, this could have various different (bad) effects; for the Microsoft C implementation, <em>islower<\/em> indexes into an array based on the given argument, so you&#8217;ll underrun an array and get garbage results back.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Signedness can trip you up in unexpected ways. One example of this is the fairly innocuous islower function in the C standard library. This function takes an int and returns a value indicating to you whether it represents a lowercase or uppercase character. The prototype for this function is: int islower( int c ); Unfortunately, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[4],"tags":[],"_links":{"self":[{"href":"http:\/\/www.nynaeve.net\/index.php?rest_route=\/wp\/v2\/posts\/49"}],"collection":[{"href":"http:\/\/www.nynaeve.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.nynaeve.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.nynaeve.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.nynaeve.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=49"}],"version-history":[{"count":1,"href":"http:\/\/www.nynaeve.net\/index.php?rest_route=\/wp\/v2\/posts\/49\/revisions"}],"predecessor-version":[{"id":687,"href":"http:\/\/www.nynaeve.net\/index.php?rest_route=\/wp\/v2\/posts\/49\/revisions\/687"}],"wp:attachment":[{"href":"http:\/\/www.nynaeve.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=49"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.nynaeve.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=49"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.nynaeve.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=49"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}