{"id":2243,"date":"2011-09-26T09:28:12","date_gmt":"2011-09-26T08:28:12","guid":{"rendered":"https:\/\/blogs.mentor.com\/colinwalls\/?p=2243"},"modified":"2026-03-26T16:35:53","modified_gmt":"2026-03-26T20:35:53","slug":"efficient-code-a-quiz","status":"publish","type":"post","link":"https:\/\/blogs.sw.siemens.com\/embedded-software\/2011\/09\/26\/efficient-code-a-quiz\/","title":{"rendered":"Efficient code &#8211; a quiz"},"content":{"rendered":"<p>I was recently reading a set of &#8220;golden rules&#8221; for embedded programming. I am very skeptical about such proscriptive instructions as, for them to be valid, a great many assumptions must be made and clearly stated. These rules were supposed to promote the production of safe, efficient code. I am OK with &#8220;safe&#8221; &#8211; that simply means that the code does what it is supposed to do, without deviation as a result of unexpected data or unexpected side-effects. I am not so sure about &#8220;efficient&#8221;, as that depends entirely on the design parameters in force; it might mean fast or it might mean small, for example. And what about maintainability and portability being significant parameters?<\/p>\n<p>So, instead of trying to outline rules myself, I thought that I might set out to find out what real developers think &#8230;<!--more--><\/p>\n<p>Just for fun [budgets to not stretch to offering a prize], I would like to pose a question and request answers by comment or <a href=\"mailto:colin_walls@mentor.com\">email<\/a>:<\/p>\n<p>You are developing a system, where execution speed is important. At one point in the code, there is an unsigned integer variable x which needs to be divided by 8. I can immediately think of 4 ways to code this:<\/p>\n<pre>x \/= 8;<\/pre>\n<pre>x = x \/ 8;<\/pre>\n<pre>x &gt;&gt;= 3;<\/pre>\n<pre>x = x &gt;&gt; 3;<\/pre>\n<p>My question is: which of these options is the most efficient and why? Of course, if you have other suggestions about how to address the problem, I would be very interested to hear.<\/p>\n<p>I will publish some results and my answer in a couple of weeks.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I was recently reading a set of &#8220;golden rules&#8221; for embedded programming. I am very skeptical about such proscriptive instructions&#8230;<\/p>\n","protected":false},"author":71677,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spanish_translation":"","french_translation":"","german_translation":"","italian_translation":"","polish_translation":"","japanese_translation":"","chinese_translation":"","footnotes":""},"categories":[1],"tags":[313,339,300,327,340],"industry":[],"product":[],"coauthors":[],"class_list":["post-2243","post","type-post","status-publish","format-standard","hentry","category-news","tag-c","tag-development-tools","tag-embedded-software","tag-optimization","tag-programming-languages"],"_links":{"self":[{"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/posts\/2243","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/users\/71677"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/comments?post=2243"}],"version-history":[{"count":1,"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/posts\/2243\/revisions"}],"predecessor-version":[{"id":10001,"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/posts\/2243\/revisions\/10001"}],"wp:attachment":[{"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/media?parent=2243"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/categories?post=2243"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/tags?post=2243"},{"taxonomy":"industry","embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/industry?post=2243"},{"taxonomy":"product","embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/product?post=2243"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/coauthors?post=2243"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}