{"id":389,"date":"2009-09-11T07:14:12","date_gmt":"2009-09-11T12:14:12","guid":{"rendered":"http:\/\/vgable.com\/blog\/?p=389"},"modified":"2009-09-11T07:14:17","modified_gmt":"2009-09-11T12:14:17","slug":"never-start-an-integer-with-0","status":"publish","type":"post","link":"https:\/\/vgable.com\/blog\/2009\/09\/11\/never-start-an-integer-with-0\/","title":{"rendered":"Never Start An Integer With 0"},"content":{"rendered":"<p>When programming, <strong>never start an integer with 0<\/strong>. <em>Most<\/em> programming languages treat a decimal number that starts with 0 as octal (base-8). So <code>x = 013;<\/code> does <em>not<\/em> set <code>x<\/code> to 13. Instead <code>x<\/code> is 11, because <code>013<\/code> is interpreted as 13<sub>8<\/sub> not 13<sub>10<\/sub>.<\/p>\n<p>Languages with this quirk include: <a href=\"http:\/\/www.cs.umd.edu\/class\/sum2003\/cmsc311\/Notes\/BitOp\/hexoctal.html\">C, C++, Objective-C<\/a>, <a href=\"http:\/\/java.sun.com\/docs\/books\/tutorial\/java\/nutsandbolts\/datatypes.html\">Java<\/a>, <a href=\"http:\/\/www.amazon.com\/gp\/product\/0596101996?ie=UTF8&#038;tag=vincgabl-20&#038;linkCode=as2&#038;camp=1789&#038;creative=390957&#038;creativeASIN=0596101996\">JavaScript<\/a>, <a href=\"http:\/\/perl.active-venture.com\/pod\/perlfaq4-datanumber.html\">Perl<\/a>, <a href=\"http:\/\/www.ibm.com\/developerworks\/opensource\/library\/os-python1\/\">Python 3.0<\/a>, and <a href=\"http:\/\/docs.huihoo.com\/ruby\/ruby-man-1.4\/syntax.html#numeric\">Ruby<\/a>. If you add up the &#8220;<a href=\"http:\/\/www.tiobe.com\/index.php\/content\/paperinfo\/tpci\/index.html\">market share<\/a>&#8221; of these languages, it comes out to above 50%, which is why I say <em>most<\/em> languages.<\/p>\n<h3>&#8220;But I use {Smalltalk, Haskell, Lisp, etc.}&#8221;<\/h3>\n<p>I&#8217;m jealous that you get to use such a nice language. However, it&#8217;s bad programming hygiene to pick up habits that are <em>dangerous<\/em> in common languages.<\/p>\n<p>Now, I assume you wouldn&#8217;t write 7 as <code>007<\/code>, unless the leading zero(s) carried some extra meaning. There are cases where this clarity outweighs &#8220;cleanliness&#8221; (unless the code meant to be ported to a C-like language).<\/p>\n<p>But you should at least be aware of this inter-lingual gotcha.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When programming, never start an integer with 0. Most programming languages treat a decimal number that starts with 0 as octal (base-8). So x = 013; does not set x to 13. Instead x is 11, because 013 is interpreted as 138 not 1310. Languages with this quirk include: C, C++, Objective-C, Java, JavaScript, Perl, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18,7,5,4],"tags":[69,366,394,325,485,417,242,486,487,405],"class_list":["post-389","post","type-post","status-publish","format-standard","hentry","category-bug-bite","category-c","category-objective-c","category-programming","tag-haskell","tag-java","tag-javascript","tag-lisp","tag-octal","tag-programming-language-design","tag-programming-style","tag-python","tag-ruby","tag-smalltalk"],"_links":{"self":[{"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/posts\/389","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/comments?post=389"}],"version-history":[{"count":1,"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/posts\/389\/revisions"}],"predecessor-version":[{"id":390,"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/posts\/389\/revisions\/390"}],"wp:attachment":[{"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/media?parent=389"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/categories?post=389"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/tags?post=389"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}