{"id":40323,"date":"2004-03-10T07:00:00","date_gmt":"2004-03-10T07:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2004\/03\/10\/why-do-operations-on-byte-result-in-int\/"},"modified":"2004-03-10T07:00:00","modified_gmt":"2004-03-10T07:00:00","slug":"why-do-operations-on-byte-result-in-int","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20040310-00\/?p=40323","title":{"rendered":"Why do operations on &quot;byte&quot; result in &quot;int&quot;?"},"content":{"rendered":"<p>(The following discussion applies equally to C\/C++\/C#, so I&#8217;ll\nuse C#, since I talk about it so rarely.)<\/p>\n<p>\nPeople complain that the following code elicits a warning:<\/p>\n<pre>\nbyte b = 32;\nbyte c = ~b;\n\/\/ error CS0029: Cannot implicitly convert type 'int' to 'byte'\n<\/pre>\n<p>\n&#8220;The result of an operation on &#8216;byte&#8217; should be another &#8216;byte&#8217;,\nnot an &#8216;int&#8217;,&#8221; they claim.\n<\/p>\n<p>\nBe careful what you ask for. You might not like it.\n<\/p>\n<p>\nSuppose we lived in a fantasy world where operations on &#8216;byte&#8217;\nresulted in &#8216;byte&#8217;.\n<\/p>\n<pre>\nbyte b = 32;\nbyte c = 240;\nint i = b + c; \/\/ what is i?\n<\/pre>\n<p>\nIn this fantasy world, the value of i would be 16!\nWhy?\nBecause the two operands to the + operator are both bytes, so\nthe sum &#8220;b+c&#8221; is computed as a byte, which results in 16 due to\ninteger overflow.\n(And,\n<a HREF=\"\/oldnewthing\/archive\/2004\/01\/29\/64389.aspx\">\nas I noted earlier,\ninteger overflow is the new security attack vector<\/a>.)<\/p>\n<p>\nSimilarly,<\/p>\n<pre>\nint j = -b;\n<\/pre>\n<p>would result in j having the value 224 and not -32, for the\nsame reason.<\/p>\n<p>\nIs that really what you want?\n<\/p>\n<p>\nConsider the following more subtle scenario:\n<\/p>\n<pre>\nstruct Results {\n byte Wins;\n byte Games;\n};\nbool WinningAverage(Results captain, Results cocaptain)\n{\n return (captain.Wins + cocaptain.Wins) &gt;=\n        (captain.Games + cocaptain.Games) \/ 2;\n}\n<\/pre>\n<p>\nIn our imaginary world, this code would return incorrect\nresults once the total number of games played exceeded 255.\nTo fix it, you would have to insert annoying int casts.<\/p>\n<pre>\n return ((int)captain.Wins + cocaptain.Wins) &gt;=\n        ((int)captain.Games + cocaptain.Games) \/ 2;\n<\/pre>\n<p>So no matter how you slice it, you&#8217;re going to have to insert\nannoying casts.  May as well have the language err on the side\nof safety (forcing you to insert the casts where you know that\noverflow is not an issue) than to err on the side of silence\n(where you may not notice the missing casts until your Payroll\ndepartment asks you why their books don&#8217;t add up at the end of\nthe month).<\/p>\n","protected":false},"excerpt":{"rendered":"<p>(The following discussion applies equally to C\/C++\/C#, so I&#8217;ll use C#, since I talk about it so rarely.) People complain that the following code elicits a warning: byte b = 32; byte c = ~b; \/\/ error CS0029: Cannot implicitly convert type &#8216;int&#8217; to &#8216;byte&#8217; &#8220;The result of an operation on &#8216;byte&#8217; should be another [&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":[2],"class_list":["post-40323","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-history"],"acf":[],"blog_post_summary":"<p>(The following discussion applies equally to C\/C++\/C#, so I&#8217;ll use C#, since I talk about it so rarely.) People complain that the following code elicits a warning: byte b = 32; byte c = ~b; \/\/ error CS0029: Cannot implicitly convert type &#8216;int&#8217; to &#8216;byte&#8217; &#8220;The result of an operation on &#8216;byte&#8217; should be another [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/40323","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=40323"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/40323\/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=40323"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=40323"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=40323"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}