<p>I have the following C# code:</p>

<pre><code>byte rule = 0;
...
rule = rule | 0x80;
</code></pre>

<p>which produces the error: </p>

<blockquote>
  <p><em>Cannot implicitly convert type 'int' to 'byte'. An explicit conversion exists (are you missing a cast?)</em></p>
</blockquote>

<p>[Update: first version of the question was wrong ... I misread the compiler output]</p>

<p>Adding the cast <strong>doesn't</strong> fix the problem:</p>

<pre><code>rule = rule | (byte) 0x80;
</code></pre>

<p>I need to write it as:</p>

<pre><code>rule |= 0x80;
</code></pre>

<p>Which just seems weird. Why is the <code>|=</code> operator any different to the <code>|</code> operator?</p>

<p>Is there any other way of telling the compiler to treat the constant as a byte?</p>

<hr>

<p><strong>@ Giovanni Galbo</strong> : yes and no.  The code is dealing with the programming of the flash memory in an external device, and logically represents a single byte of memory.  I could cast it later, but this seemed more obvious.  I guess my C heritage is showing through too much!</p>

<p><strong>@ Jonathon Holland</strong> : the 'as' syntax looks neater but unfortunately doesn't appear to work ... it produces:</p>

<blockquote>
  <p><em>The as operator must be used with a reference type or nullable type ('byte' is a non-nullable value type)</em>    </p>
</blockquote>

[{'answer_id': 43057, 'author': 'John Rutherford', 'author_id': 3880, 'author_profile': 'https://Stackoverflow.com/users/3880', 'pm_score': 1, 'selected': False, 'text': '<p>Looks like you may just have to do it the ugly way: <a href="http://msdn.microsoft.com/en-us/library/5bdb6693.aspx" rel="nofollow noreferrer">http://msdn.microsoft.com/en-us/library/5bdb6693.aspx</a>.</p>\n'}, {'answer_id': 43059, 'author': 'Eric Z Beard', 'author_id': 1219, 'author_profile': 'https://Stackoverflow.com/users/1219', 'pm_score': 5, 'selected': False, 'text': '<p>C# does not have a literal suffix for byte.  u = uint, l = long, ul = ulong, f = float, m = decimal, but no byte.  You have to cast it.</p>\n'}, {'answer_id': 43060, 'author': 'FlySwat', 'author_id': 1965, 'author_profile': 'https://Stackoverflow.com/users/1965', 'pm_score': 3, 'selected': False, 'text': '<p>The term you are looking for is "Literal" and unfortunately C# does not have a byte literal.</p>\n\n<p>Here\'s a list of <a href="http://msdn.microsoft.com/en-us/library/aa664672(VS.71).aspx" rel="nofollow noreferrer">all C# literals</a>.</p>\n'}, {'answer_id': 43061, 'author': 'grom', 'author_id': 486, 'author_profile': 'https://Stackoverflow.com/users/486', 'pm_score': 2, 'selected': False, 'text': '<p>According to the <a href="http://www.ecma-international.org/publications/standards/Ecma-334.htm" rel="nofollow noreferrer">ECMA Specification, pg 72</a> there is no byte literal. Only integer literals for the types: int, uint, long, and ulong.</p>\n'}, {'answer_id': 43062, 'author': 'Peter Meyer', 'author_id': 1875, 'author_profile': 'https://Stackoverflow.com/users/1875', 'pm_score': 0, 'selected': False, 'text': '<p>Unfortunately, your only recourse is to do it just the way you have.  There is no suffix to mark the literal as a byte.  The | operator does not provide for implicit conversion as an assignment (i.e. initialization) would.</p>\n'}, {'answer_id': 43064, 'author': 'jmatthias', 'author_id': 2768, 'author_profile': 'https://Stackoverflow.com/users/2768', 'pm_score': 4, 'selected': False, 'text': "<p>This works:</p>\n\n<pre><code>rule = (byte)(rule | 0x80);\n</code></pre>\n\n<p>Apparently the expression 'rule | 0x80' returns an int even if you define 0x80 as 'const byte 0x80'.</p>\n"}, {'answer_id': 43074, 'author': 'David J. Sokol', 'author_id': 1390, 'author_profile': 'https://Stackoverflow.com/users/1390', 'pm_score': 4, 'selected': True, 'text': '<pre><code>int rule = 0;\nrule |= 0x80;\n</code></pre>\n\n<p><a href="http://msdn.microsoft.com/en-us/library/kxszd0kx.aspx" rel="noreferrer">http://msdn.microsoft.com/en-us/library/kxszd0kx.aspx</a> The | operator is defined for all value types.  I think this will produced the intended result.  The "|=" operator is an or then assign operator, which is simply shorthand for rule = rule | 0x80.</p>\n\n<p>One of the niftier things about C# is that it lets you do crazy things like abuse value types simply based on their size.  An \'int\' is exactly the same as a byte, except the compiler will throw warnings if you try and use them as both at the same time.  Simply sticking with one (in this case, int) works well.  If you\'re concerned about 64bit readiness, you can specify int32, but all ints are int32s, even running in x64 mode.</p>\n'}, {'answer_id': 43090, 'author': 'jfs', 'author_id': 718, 'author_profile': 'https://Stackoverflow.com/users/718', 'pm_score': 0, 'selected': False, 'text': "<blockquote>\n  <p>Apparently the expression 'rule |\n  0x80' returns an int even if you\n  define 0x80 as 'const byte 0x80'.</p>\n</blockquote>\n\n<p>I think the rule is numbers like 0x80 defaults to int unless you include a literal suffix. So for the expression <code>rule | 0x80</code>, the result will be an int since 0x80 is an int and rule (which is a byte) can safely be converted to int.</p>\n"}, {'answer_id': 14554599, 'author': 'Bruce Bowman', 'author_id': 2016761, 'author_profile': 'https://Stackoverflow.com/users/2016761', 'pm_score': 0, 'selected': False, 'text': "<p>According to the C standard, bytes ALWAYS promote to int in expressions, even constants. However, as long as both values are UNSIGNED, the high-order bits will be discarded so the operation should return the correct value.</p>\n\n<p>Similarly, floats promote to double, etc.</p>\n\n<p>Pull out of copy of K&amp;R. It's all in there.</p>\n"}, {'answer_id': 16884882, 'author': 'arx', 'author_id': 292432, 'author_profile': 'https://Stackoverflow.com/users/292432', 'pm_score': 2, 'selected': False, 'text': '<p>Almost five years on and nobody has actually answered the question.</p>\n\n<p>A couple of answers claim that the problem is the lack of a byte literal, but this is irrelevant.  If you calculate <code>(byte1 | byte2)</code> the result is of type <code>int</code>.  Even if "b" were a literal suffix for byte the type of <code>(23b | 32b)</code> would still be <code>int</code>.</p>\n\n<p>The accepted answer links to an MSDN article claiming that <code>operator|</code> is defined for all integral types, but this isn\'t true either.</p>\n\n<p><code>operator|</code> is not defined on <code>byte</code> so the compiler uses its usual overload resolution rules to pick the version that\'s defined on <code>int</code>.  Hence, if you want to assign the result to a <code>byte</code> you need to cast it:</p>\n\n<pre><code>rule = (byte)(rule | 0x80);\n</code></pre>\n\n<p>The question remains, why does <code>rule |= 0x80;</code> work?</p>\n\n<p>Because the C# specification has a special rule for compound assignment that allows you to omit the explicit conversion.  In the compound assignment <code>x op= y</code> the rule is:</p>\n\n<blockquote>\n  <p>if the selected operator is a predefined operator, if the return type of the selected operator is explicitly convertible to the type of x, and if y is implicitly convertible to the type of x or the operator is a shift operator, then the operation is evaluated as <code>x = (T)(x op y)</code>, where T is the type of x, except that x is evaluated only once.</p>\n</blockquote>\n'}]
['https://Stackoverflow.com/questions/43051', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/3631/']