{"id":198,"date":"2016-04-13T09:12:53","date_gmt":"2016-04-13T09:12:53","guid":{"rendered":"https:\/\/ibex.tech\/csharp\/?p=198"},"modified":"2022-02-17T06:24:14","modified_gmt":"2022-02-17T06:24:14","slug":"and-or-and-binary-operations-on-variables","status":"publish","type":"post","link":"https:\/\/ibex.tech\/csharp\/c-sharp\/memory\/and-or-and-binary-operations-on-variables","title":{"rendered":"AND, OR and binary operations on variables"},"content":{"rendered":"\n<p><span style=\"color: #ff8c00;\"><em><strong>C# is not the same as C or C++!!!!! &nbsp;Simple byte operations for example do not result in a byte result, they often result in an Int32 result<\/strong><\/em><\/span><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">An example binary operation<\/h4>\n\n\n\n<p>This will generat the error &#8220;Cannot implicitly convert type &#8216;int&#8217; to &#8216;byte&#8217;. An explicit conversion exists (are you missing a cast)&#8221;:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\tTxData&#91;ByteIndex++] = (byte)TagIdCharacter&#91;0] | 0x80;<\/code><\/pre>\n\n\n\n<p>Its because the result of the IOR operation does not result in a byte, so you need to&nbsp;use&nbsp;parenthesis:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\tTxData&#91;ByteIndex++] = (byte)(TagIdCharacter&#91;0] | 0x80);<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Gotchas!<\/h4>\n\n\n\n<p>Convert.ToInt16( &#8230; ) does not work error free!  If you try and convert 0x0000FFFF) for instance it will generate an error!  However using (Int16) won&#8217;t.  Here&#8217;s an example<\/p>\n\n\n\n<p>This generates a &#8216;Value to big or small&#8217; error:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\tByte&#91;] RxData = new Byte&#91;10];\r\n\tRxData&#91;0] = 0xff;\r\n\tRxData&#91;1] = 0xff;\r\n\tTemperature = Convert.ToInt16((Convert.ToInt16(RxData&#91;0]) &lt;&lt; 8) | RxData&#91;1]);<\/code><\/pre>\n\n\n\n<p>This works fine and produces the correct -1 result:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\tByte&#91;] RxData = new Byte&#91;10];\r\n\tRxData&#91;0] = 0xff;\r\n\tRxData&#91;1] = 0xff;\r\n\tTemperature = (Int16)((Convert.ToInt16(RxData&#91;0]) &lt;&lt; 8) | RxData&#91;1]);<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Examples<\/h4>\n\n\n\n<h5 class=\"wp-block-heading\">Merge 2 bytes values into a Int16 variable<\/h5>\n\n\n\n<p>Remember the result of an OR operation is Int32, and you can&#8217;t convert 0x0000FFFF to Int16 using ConvertTo, but this works fine:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\tByte&#91;] RxData = new Byte&#91;10];\r\n\tRxData&#91;0] = 0xff;\r\n\tRxData&#91;1] = 0xff;\r\n\tTemperature = (Int16)((Convert.ToInt16(RxData&#91;0]) &lt;&lt; 8) | RxData&#91;1]);<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>C# is not the same as C or C++!!!!! &nbsp;Simple byte operations for example do not result in a byte result, they often result in an Int32 result An example binary operation This will generat the error &#8220;Cannot implicitly convert type &#8216;int&#8217; to &#8216;byte&#8217;. An explicit conversion exists (are you missing a cast)&#8221;: Its because [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[],"class_list":["post-198","post","type-post","status-publish","format-standard","hentry","category-memory"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/posts\/198","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/comments?post=198"}],"version-history":[{"count":6,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/posts\/198\/revisions"}],"predecessor-version":[{"id":511,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/posts\/198\/revisions\/511"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/media?parent=198"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/categories?post=198"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/tags?post=198"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}