{"id":35523,"date":"2005-05-27T09:03:25","date_gmt":"2005-05-27T09:03:25","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2005\/05\/27\/when-is-x2-different-from-x1\/"},"modified":"2005-05-27T09:03:25","modified_gmt":"2005-05-27T09:03:25","slug":"when-is-x2-different-from-x1","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20050527-25\/?p=35523","title":{"rendered":"When is x\/2 different from x&#062;&#062;1?"},"content":{"rendered":"<p>Everyone &#8220;knows&#8221; that the following pairs of expressions are equivalent:<\/p>\n<table>\n<tr>\n<td><code>x*2 &equiv; x&lt;&lt;1<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>x\/2 &equiv; x&gt;&gt;1<\/code><\/td>\n<\/tr>\n<\/table>\n<p> Too bad they aren&#8217;t.\n In the C language standard, there is no requirement that the internal representation of signed integers be two&#8217;s complement. All the permissible representations agree for positive numbers, but negative numbers can have different representations. If <code>x<\/code> is negative, then <code>x*2<\/code> and <code>x&lt;&lt;1<\/code> are quite different on a sign\/magnitude system.\n However, Win32 requires a two&#8217;s complement machine, in which case the first equivalence <code>x*2 &equiv; x&lt;&lt;1<\/code> is indeed always true.\n Of course, the compiler is free to recognize this and rewrite your multiplication or shift operation.  In fact, it is very likely to do this, because <code>x+x<\/code> is more easily <a href=\"http:\/\/www.gamedev.net\/reference\/articles\/article212.asp\"> pairable<\/a> than a multiplication or shift. Your shift or multiply-by-two is probably going to be rewritten as something closer to an <code>add eax, eax<\/code> instruction.\n As for the second so-called equivalence, the C language specification originally did not specify whether division of a negative number by a positive number rounds towards or away from zero, but in 1999, the specification was revised to require rounding towards zero. Furthermore, the result of a right-shift of a negative value is unspecified, so the expression <code>x&gt;&gt;1<\/code> has an unspecified result if <code>x<\/code> is negative.<\/p>\n<p> Even if you assume that the shift fills with the sign bit, The result of the shift and the divide are different if <code>x<\/code> is negative. <\/p>\n<table>\n<tr>\n<td><code>(-1) \/ 2 &equiv; 0<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>(-1) &gt;&gt; 1 &equiv; -1<\/code><\/td>\n<\/tr>\n<\/table>\n<p> The moral of the story is to write what you mean. If you want to divide by two, then write &#8220;<code>\/2<\/code>&#8220;, not &#8220;<code>&gt;&gt;1<\/code>&#8220;. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Everyone &#8220;knows&#8221; that the following pairs of expressions are equivalent: x*2 &equiv; x&lt;&lt;1 x\/2 &equiv; x&gt;&gt;1 Too bad they aren&#8217;t. In the C language standard, there is no requirement that the internal representation of signed integers be two&#8217;s complement. All the permissible representations agree for positive numbers, but negative numbers can have different representations. If [&hellip;]<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-35523","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Everyone &#8220;knows&#8221; that the following pairs of expressions are equivalent: x*2 &equiv; x&lt;&lt;1 x\/2 &equiv; x&gt;&gt;1 Too bad they aren&#8217;t. In the C language standard, there is no requirement that the internal representation of signed integers be two&#8217;s complement. All the permissible representations agree for positive numbers, but negative numbers can have different representations. If [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/35523","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=35523"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/35523\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=35523"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=35523"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=35523"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}