<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Problem with shift operation in Kinetis Design Studio</title>
    <link>https://community.nxp.com/t5/Kinetis-Design-Studio/Problem-with-shift-operation/m-p/365060#M1702</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi, i have this simple operation : image-&amp;gt;start_sector = (((char)(*buff))&amp;lt;&amp;lt;24)|(((char)*(buff+1))&amp;lt;&amp;lt;16)|(((char)*(buff+2))&amp;lt;&amp;lt;8)|((char)*(buff+3));&lt;/P&gt;&lt;P style="min-height: 8pt; padding: 0px;"&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;where i concatenate 4 byte in a 32bit variable. the problem is when i shift the buff+1 &amp;lt;&amp;lt; 16. The number that i have to obtain could be 0x3b9b2a00 but i have 0xff9b2a00. The problem is visible in the image, the other shift work well.&lt;span class="lia-inline-image-display-wrapper" image-alt="16882_16882.jpg"&gt;&lt;img src="https://community.nxp.com/t5/image/serverpage/image-id/120047iEF1634C670D37FF0/image-size/large?v=v2&amp;amp;px=999" role="button" title="16882_16882.jpg" alt="16882_16882.jpg" /&gt;&lt;/span&gt;&lt;span class="lia-inline-image-display-wrapper" image-alt="Immagine.JPG.jpg"&gt;&lt;img src="https://community.nxp.com/t5/image/serverpage/image-id/47519i928A525C1C341353/image-size/large?v=v2&amp;amp;px=999" role="button" title="Immagine.JPG.jpg" alt="Immagine.JPG.jpg" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Mon, 24 Nov 2014 15:29:51 GMT</pubDate>
    <dc:creator>danielecortella</dc:creator>
    <dc:date>2014-11-24T15:29:51Z</dc:date>
    <item>
      <title>Problem with shift operation</title>
      <link>https://community.nxp.com/t5/Kinetis-Design-Studio/Problem-with-shift-operation/m-p/365060#M1702</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi, i have this simple operation : image-&amp;gt;start_sector = (((char)(*buff))&amp;lt;&amp;lt;24)|(((char)*(buff+1))&amp;lt;&amp;lt;16)|(((char)*(buff+2))&amp;lt;&amp;lt;8)|((char)*(buff+3));&lt;/P&gt;&lt;P style="min-height: 8pt; padding: 0px;"&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;where i concatenate 4 byte in a 32bit variable. the problem is when i shift the buff+1 &amp;lt;&amp;lt; 16. The number that i have to obtain could be 0x3b9b2a00 but i have 0xff9b2a00. The problem is visible in the image, the other shift work well.&lt;span class="lia-inline-image-display-wrapper" image-alt="16882_16882.jpg"&gt;&lt;img src="https://community.nxp.com/t5/image/serverpage/image-id/120047iEF1634C670D37FF0/image-size/large?v=v2&amp;amp;px=999" role="button" title="16882_16882.jpg" alt="16882_16882.jpg" /&gt;&lt;/span&gt;&lt;span class="lia-inline-image-display-wrapper" image-alt="Immagine.JPG.jpg"&gt;&lt;img src="https://community.nxp.com/t5/image/serverpage/image-id/47519i928A525C1C341353/image-size/large?v=v2&amp;amp;px=999" role="button" title="Immagine.JPG.jpg" alt="Immagine.JPG.jpg" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 24 Nov 2014 15:29:51 GMT</pubDate>
      <guid>https://community.nxp.com/t5/Kinetis-Design-Studio/Problem-with-shift-operation/m-p/365060#M1702</guid>
      <dc:creator>danielecortella</dc:creator>
      <dc:date>2014-11-24T15:29:51Z</dc:date>
    </item>
    <item>
      <title>Re: Problem with shift operation</title>
      <link>https://community.nxp.com/t5/Kinetis-Design-Studio/Problem-with-shift-operation/m-p/365061#M1703</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Daniele,&lt;/P&gt;&lt;P&gt;your code assumes that plain 'char' is unsigned. However, plain char is 'signed', or at least this is what I believe you are using.&lt;/P&gt;&lt;P&gt;Check your compiler settings:&lt;/P&gt;&lt;P&gt;&lt;SPAN class="lia-inline-image-display-wrapper" image-alt="pastedImage_0.png"&gt;&lt;IMG alt="pastedImage_0.png" src="https://community.nxp.com/t5/image/serverpage/image-id/47521i3F0673ADDC1497FC/image-size/large?v=v2&amp;amp;px=999" title="pastedImage_0.png" /&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The correct way to write your calculation is&lt;/P&gt;&lt;PRE __default_attr="c++" __jive_macro_name="code" class="jive_text_macro _jivemacro_uid_1416844066483972 jive_macro_code" jivemacro_uid="_1416844066483972" modifiedtitle="true"&gt;
&lt;P&gt;&amp;nbsp; result = (((unsigned char)(*buff))&amp;lt;&amp;lt;24)|(((unsigned char)*(buff+1))&amp;lt;&amp;lt;16)|(((unsigned char)*(buff+2))&amp;lt;&amp;lt;8)|((unsigned char)*(buff+3));&lt;/P&gt;
&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Explanation:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;*(buff+1)&lt;/P&gt;&lt;P&gt;is a char (signed char)&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Then you even cast it to a (signed) char again (redundant from the compiler point of view)&lt;/P&gt;&lt;P&gt;((char)*(buff+1))&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Then, before the shift (&amp;lt;&amp;lt;) operation, the compiler has to do integral promotion.&lt;/P&gt;&lt;P&gt;because this is a signed char (0x9b) and negative, it gets promoted to&lt;/P&gt;&lt;P&gt;0xff9b&lt;/P&gt;&lt;P&gt;and then ORed into your result.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;So this is why you get 0xff9b. &lt;SPAN class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;&lt;LI-EMOJI id="lia_slightly-smiling-face" title=":slightly_smiling_face:"&gt;&lt;/LI-EMOJI&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Solution: take care of signed or unsigned char, and use unsigend char in that particular case.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I hope this helps,&lt;/P&gt;&lt;P&gt;Erich&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 02 Nov 2020 13:13:31 GMT</pubDate>
      <guid>https://community.nxp.com/t5/Kinetis-Design-Studio/Problem-with-shift-operation/m-p/365061#M1703</guid>
      <dc:creator>BlackNight</dc:creator>
      <dc:date>2020-11-02T13:13:31Z</dc:date>
    </item>
  </channel>
</rss>

