<?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 Re: Bitfileds on 5282 in ColdFire/68K Microcontrollers and Processors</title>
    <link>https://community.nxp.com/t5/ColdFire-68K-Microcontrollers/Bitfileds-on-5282/m-p/182099#M7444</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;DIV&gt;Hello&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;The problem with bitfields in ANSI C is that they are not portable across compiler vendor or architecture.&lt;/DIV&gt;&lt;DIV&gt;The ANSI C standard does not specify i how a bitfield might be allocated in memory.&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;Depending on the vendor and architecture it might be LSB first or MSB first.&lt;/DIV&gt;&lt;DIV&gt;There might also be some padding bits between the fields.&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;CodeWarrior for Coldfire uses MSB first as allocation scheme.&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;With the syntax you are using, the compiler will allocate the bits as follows:&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp; Source : bit 15-10&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp; msg_id: Bit 0-8&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp; msg_type: Bit 7&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp; request_type: bit 6&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp; dataType: bit 0-5&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;If you wish to get Source allocated on bit 0-4 you need to define your bitfield as follows:&lt;/DIV&gt;&lt;DIV&gt;typedef struct {&lt;BR /&gt;&amp;nbsp;unsigned int data_type: 6;&amp;nbsp;&lt;BR /&gt;&amp;nbsp;unsigned int request_type: 1;&amp;nbsp;&lt;BR /&gt;&amp;nbsp;unsigned int msg_type: 1;&amp;nbsp;&lt;BR /&gt;&amp;nbsp;unsigned int msg_id:&amp;nbsp; 3;&amp;nbsp;&lt;BR /&gt;&amp;nbsp;unsigned int source : 5;&lt;BR /&gt;} BitFields_T;&lt;BR /&gt;&lt;/DIV&gt;&lt;DIV&gt;CrasyCat&lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Wed, 27 Aug 2008 15:43:42 GMT</pubDate>
    <dc:creator>CrasyCat</dc:creator>
    <dc:date>2008-08-27T15:43:42Z</dc:date>
    <item>
      <title>Bitfileds on 5282</title>
      <link>https://community.nxp.com/t5/ColdFire-68K-Microcontrollers/Bitfileds-on-5282/m-p/182098#M7443</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;DIV&gt; &lt;/DIV&gt;&lt;DIV&gt;Hi,&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;Having hard time with bit fields. In the following snippets,&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;DIV class="msg_source_code"&gt;&lt;DIV class="text_smallest"&gt;Code:&lt;/DIV&gt;&lt;PRE&gt;//-----------------------------------------------typedef unsigned short int&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; uint16//-----------------------------------------------typedef struct { unsigned int source : 5;&amp;nbsp; unsigned int msg_id:&amp;nbsp; 3;&amp;nbsp; unsigned int msg_type: 1;&amp;nbsp; unsigned int request_type: 1;&amp;nbsp; unsigned int data_type: 6; } BitFields_T;//-----------------------------------------------typedef union { BitFields_T b; uint16 word16;} HEADER_T;//-----------------------------------------------int main(void){&amp;nbsp; HEADER_T Var; Var.word16=0;&amp;nbsp; Var.b.source=1;}&lt;/PRE&gt;&lt;/DIV&gt;&lt;BR /&gt;&amp;nbsp;&lt;BR /&gt;I was expecting to have Var.word16==1. Instead I have Var.word16==2048 (shifted by 12 ).&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;Any hints?&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;Thanks,&lt;/DIV&gt;&lt;DIV&gt;S.&lt;/DIV&gt;&lt;DIV&gt;&lt;BR /&gt;&amp;nbsp;&lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 29 Oct 2020 09:32:39 GMT</pubDate>
      <guid>https://community.nxp.com/t5/ColdFire-68K-Microcontrollers/Bitfileds-on-5282/m-p/182098#M7443</guid>
      <dc:creator>SVC2</dc:creator>
      <dc:date>2020-10-29T09:32:39Z</dc:date>
    </item>
    <item>
      <title>Re: Bitfileds on 5282</title>
      <link>https://community.nxp.com/t5/ColdFire-68K-Microcontrollers/Bitfileds-on-5282/m-p/182099#M7444</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;DIV&gt;Hello&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;The problem with bitfields in ANSI C is that they are not portable across compiler vendor or architecture.&lt;/DIV&gt;&lt;DIV&gt;The ANSI C standard does not specify i how a bitfield might be allocated in memory.&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;Depending on the vendor and architecture it might be LSB first or MSB first.&lt;/DIV&gt;&lt;DIV&gt;There might also be some padding bits between the fields.&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;CodeWarrior for Coldfire uses MSB first as allocation scheme.&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;With the syntax you are using, the compiler will allocate the bits as follows:&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp; Source : bit 15-10&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp; msg_id: Bit 0-8&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp; msg_type: Bit 7&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp; request_type: bit 6&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp; dataType: bit 0-5&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;If you wish to get Source allocated on bit 0-4 you need to define your bitfield as follows:&lt;/DIV&gt;&lt;DIV&gt;typedef struct {&lt;BR /&gt;&amp;nbsp;unsigned int data_type: 6;&amp;nbsp;&lt;BR /&gt;&amp;nbsp;unsigned int request_type: 1;&amp;nbsp;&lt;BR /&gt;&amp;nbsp;unsigned int msg_type: 1;&amp;nbsp;&lt;BR /&gt;&amp;nbsp;unsigned int msg_id:&amp;nbsp; 3;&amp;nbsp;&lt;BR /&gt;&amp;nbsp;unsigned int source : 5;&lt;BR /&gt;} BitFields_T;&lt;BR /&gt;&lt;/DIV&gt;&lt;DIV&gt;CrasyCat&lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 27 Aug 2008 15:43:42 GMT</pubDate>
      <guid>https://community.nxp.com/t5/ColdFire-68K-Microcontrollers/Bitfileds-on-5282/m-p/182099#M7444</guid>
      <dc:creator>CrasyCat</dc:creator>
      <dc:date>2008-08-27T15:43:42Z</dc:date>
    </item>
  </channel>
</rss>

