<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://community.arm.com/utility/feedstylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Struct in union</title><link>https://community.arm.com/developer/tools-software/tools/f/keil-forum/36676/struct-in-union</link><description> Hi 
I have some problems with struct inside a union 
This is my code. 
 
union{
	unsigned char id_byte[2];

	struct {
 unsigned char DLC:4;
 unsigned char RTR:1;
 unsigned int identifier:11;
 }del_element;
}can_descriptor;
 
 
when I write an_descriptor</description><dc:language>en-US</dc:language><generator>Telligent Community 10</generator><item><title>RE: Struct in union</title><link>https://community.arm.com/thread/152511?ContentTypeID=1</link><pubDate>Mon, 11 Oct 2004 11:15:54 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:26c4dfde-7f2b-4ae7-b3f3-29c4b09ba668</guid><dc:creator>Soren Hansen</dc:creator><description>&lt;p&gt;thanks i try  that.&lt;br /&gt;
&lt;br /&gt;
soren&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Struct in union</title><link>https://community.arm.com/thread/151720?ContentTypeID=1</link><pubDate>Mon, 11 Oct 2004 11:13:03 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:8579c1b0-7550-4bd4-bd6e-c782089481e7</guid><dc:creator>Andy Neil</dc:creator><description>&lt;p&gt;&lt;i&gt;&amp;quot;You say that i shoud use mask and shift?&lt;br /&gt;
dose&amp;#39;t it give more code?&amp;quot;&lt;/i&gt;&lt;br /&gt;
&lt;br /&gt;
Try re-reading the previous posts!&lt;br /&gt;
&lt;br /&gt;
Here&amp;#39;s a reminder of what&amp;#39;s been said so far:&lt;br /&gt;
&lt;br /&gt;
&lt;i&gt;&amp;quot;many compilers generate poor code for bitfields. It&amp;#39;s more portable and often more efficient just to use the &amp;amp; and | operators directly.&amp;quot;&lt;/i&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;i&gt;&amp;quot;I don&amp;#39;t think there&amp;#39;s any reason to believe that bitfields will give you (significantly) better code than shifts and masks and some bitfield implementations are notoriously inefficient - so shifts and masks can actually be better!&amp;quot;&lt;/i&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;i&gt;&amp;quot;[shift &amp; mask] is probably how the compiler actually implements the bitfields!&amp;quot;&lt;/i&gt;&lt;br /&gt;
&lt;br /&gt;
That should answer your question!&lt;br /&gt;
&lt;br /&gt;
And again, shifts &amp;amp; masks are portable; bitfields are not.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Struct in union</title><link>https://community.arm.com/thread/147259?ContentTypeID=1</link><pubDate>Mon, 11 Oct 2004 08:50:15 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:36ebd7ee-1f6d-471c-a73e-4c451a6b8615</guid><dc:creator>Soren Hansen</dc:creator><description>&lt;p&gt;year ok.&lt;br /&gt;
&lt;br /&gt;
yes as small as passebel, no time to waste.&lt;br /&gt;
&lt;br /&gt;
You say that i shoud use mask and shift?&lt;br /&gt;
dose&amp;#39;t it give more code?&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Soren&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Struct in union</title><link>https://community.arm.com/thread/145909?ContentTypeID=1</link><pubDate>Mon, 11 Oct 2004 08:47:05 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:0676bf7b-cbd2-41e7-9192-da7e69d2996c</guid><dc:creator>Andy Neil</dc:creator><description>&lt;p&gt;&lt;i&gt;&amp;quot;if I shifts, I need more code ... I don&amp;#39;t have that problem in union struct.&amp;quot;&lt;/i&gt;&lt;br /&gt;
&lt;br /&gt;
It may look like more &lt;i&gt;&lt;b&gt;source&lt;/b&gt;&lt;/i&gt; code, but have you checked the generated &lt;i&gt;&lt;b&gt;object&lt;/b&gt;&lt;/i&gt; code?&lt;br /&gt;
&lt;br /&gt;
Does it make any difference to the amount of code that the processor actually has to run?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Struct in union</title><link>https://community.arm.com/thread/145917?ContentTypeID=1</link><pubDate>Mon, 11 Oct 2004 08:44:23 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:12eac90c-2580-413e-80cc-072ae9235698</guid><dc:creator>HansBernhard Broeker</dc:creator><description>&lt;p&gt;&lt;i&gt;if I shifts, I need more code, becourse I need to shift different if i have a RTR bit set.&lt;/i&gt;&lt;br /&gt;
&lt;br /&gt;
I rather strongly suspect the opposite to be true.  If the shift is, indeed, different depending on the status of some bits, that&amp;#39;s &lt;b&gt;impossible&lt;/b&gt; to model using bitfields alone, because the lengths, and thus by implication the positions, of bitfields inside a struct are fixed at compile time.&lt;br /&gt;
It&amp;#39;s much more transparent to have the argument of a shift operator be a variable or expression than having to do something like&lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;if(rtr)
   my_union.rtr_set.somebit = 1;
else
   my_union.rtr_clr.somebit = 1;
&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Struct in union</title><link>https://community.arm.com/thread/143988?ContentTypeID=1</link><pubDate>Mon, 11 Oct 2004 08:30:01 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:4762ac5f-84fd-4249-b5af-c88de476800c</guid><dc:creator>Soren Hansen</dc:creator><description>&lt;p&gt;I uses the union struct to place bit into my SJA1000 controller.&lt;br /&gt;
&lt;br /&gt;
I uses the funktion to make TX id byts.&lt;br /&gt;
and to decode RX bytes.&lt;br /&gt;
&lt;br /&gt;
if I shifts, I need more code, becourse I need to shift different if i have a RTR bit set.&lt;br /&gt;
I don&amp;#39;t have that problem in union struct.&lt;br /&gt;
&lt;br /&gt;
Soren&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Struct in union</title><link>https://community.arm.com/thread/141417?ContentTypeID=1</link><pubDate>Mon, 11 Oct 2004 08:25:55 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:071c666a-8341-4dfb-a1aa-9a436c10d990</guid><dc:creator>Andy Neil</dc:creator><description>&lt;p&gt;&lt;i&gt;&amp;quot;I tryed not to use shift and mask too much.&amp;quot;&lt;/i&gt;&lt;br /&gt;
&lt;br /&gt;
Why not?&lt;br /&gt;
It&amp;#39;s probably how the compiler actually implements the bitfields!&lt;br /&gt;
&lt;br /&gt;
&lt;i&gt;&amp;quot;I have now implantet the union struct, with some modifikations.....and i works.&amp;quot;&lt;/i&gt;&lt;br /&gt;
&lt;br /&gt;
Be sure to test it thoroughly - you don&amp;#39;t want any nasty surprises with the sign bit, for example...&lt;br /&gt;
&lt;br /&gt;
&lt;i&gt;&amp;quot;Shifting and masking, is not good if you need to convert 2 ways...&amp;quot;&lt;/i&gt;&lt;br /&gt;
&lt;br /&gt;
Why not?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Struct in union</title><link>https://community.arm.com/thread/137915?ContentTypeID=1</link><pubDate>Mon, 11 Oct 2004 07:57:44 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:17544545-d017-4a27-89dd-390c52ee2f30</guid><dc:creator>Soren Hansen</dc:creator><description>&lt;p&gt;I tryed not to use shift and mask too much.&lt;br /&gt;
&lt;br /&gt;
I have now implantet the union struct, with some modifikations.....and i works.&lt;br /&gt;
&lt;br /&gt;
Shifting and masking, is not good if you need to convert 2 ways...&lt;br /&gt;
&lt;br /&gt;
Soren&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Struct in union</title><link>https://community.arm.com/thread/134447?ContentTypeID=1</link><pubDate>Mon, 11 Oct 2004 07:53:47 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:938536bb-8146-483f-82d3-14bb030c1af1</guid><dc:creator>Andy Neil</dc:creator><description>&lt;p&gt;Actually, you &lt;i&gt;can&lt;/i&gt; do it - but, for the reasons already discussed, it&amp;#39;s best avoided.&lt;br /&gt;
&lt;br /&gt;
I don&amp;#39;t think there&amp;#39;s any reason to believe that bitfields will give you (significantly) better code than shifts and masks and some bitfield implementations are notoriously inefficient - so shifts and masks can actually be better!&lt;br /&gt;
&lt;br /&gt;
Your problem is that you have not fully understood Keil&amp;#39;s bitfield implementation.&lt;br /&gt;
If you really do want to use bitfields, you will have to read-up on Keil&amp;#39;s implementation, and adapt your union to match.&lt;br /&gt;
&lt;br /&gt;
There are articles in the Knowledgebase that will help you with this - use the site &amp;#39;&lt;b&gt;Search&lt;/b&gt;&amp;#39; facility.&lt;br /&gt;
&lt;br /&gt;
You can always use the memory viewing facilities in the simulator to help you see what&amp;#39;s going on...&lt;br /&gt;
&lt;br /&gt;
But, again, why bother?&lt;br /&gt;
Just use shifts &amp;amp; masks - you know where you are with shifts &amp;amp; masks!&lt;br /&gt;
And they&amp;#39;re portable!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Struct in union</title><link>https://community.arm.com/thread/122534?ContentTypeID=1</link><pubDate>Mon, 11 Oct 2004 06:39:38 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:71325776-a840-44be-a861-4caf54bfc2b2</guid><dc:creator>HansBernhard Broeker</dc:creator><description>&lt;p&gt;&lt;i&gt;This means that I never can do what i had in intention?&lt;/i&gt;&lt;br /&gt;
&lt;br /&gt;
In a nutshell: yes.  The language definition for C left so many details of bitfield behaviour for the implementor to specify that there&amp;#39;s essentially nothing usable left for application like yours, trying to recreate some hardware bit addressing schemes in C structs.&lt;br /&gt;
&lt;br /&gt;
In short, C data structures will only ever match any pre-determined physical address layout by coincidence, but not by design.&lt;br /&gt;
&lt;br /&gt;
Use #define&amp;#39;d shifting and masking operations instead.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Struct in union</title><link>https://community.arm.com/thread/111117?ContentTypeID=1</link><pubDate>Mon, 11 Oct 2004 02:14:03 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:f261798b-536b-4466-8f4c-9d46ea12b2d7</guid><dc:creator>Soren Hansen</dc:creator><description>&lt;p&gt;This means that I never can do what i had in  intention?&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Soren&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Struct in union</title><link>https://community.arm.com/thread/96638?ContentTypeID=1</link><pubDate>Sun, 10 Oct 2004 21:26:47 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:2cdf0f01-b8de-4343-b98c-e756506a7973</guid><dc:creator>Drew Davis</dc:creator><description>&lt;p&gt;Keil C has a somewhat non-intuitive order for bitfields.&lt;br /&gt;
&lt;br /&gt;
&lt;a href="http://www.keil.com/support/docs/1948.htm"&gt;http://www.keil.com/support/docs/1948.htm&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
Per the C standard, the &amp;quot;correct&amp;quot; storage pattern for bitfields is up the implementation.  A program cannot rely on the order of bitfields within an int, nor can it depend on the signed-ness of a bitfield.  For that matter, per spec all bitfields must occupy an &amp;quot;int&amp;quot; (whatever that may be), though every compiler I&amp;#39;ve ever used lets you specify other byte widths for them.&lt;br /&gt;
&lt;br /&gt;
In fact, I avoid using this C feature at all because the results are so different for every compiler and platform.  In addition, many compilers generate poor code for bitfields.  It&amp;#39;s more portable and often more efficient just to use the &amp;amp; and | operators directly.  If you&amp;#39;re sending messages to another processor, you can use the same declarations without worrying about different implementation orders.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Struct in union</title><link>https://community.arm.com/thread/72780?ContentTypeID=1</link><pubDate>Sun, 10 Oct 2004 12:38:24 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:dc9b1f24-db49-4e04-896d-8d9028d1c473</guid><dc:creator>Soren Hansen</dc:creator><description>&lt;p&gt;tryede that.&lt;br /&gt;
now comes the identifier,DLC and RTR into the array.&lt;br /&gt;
but wrong:&lt;br /&gt;
&lt;br /&gt;
identifier=0x0A&lt;br /&gt;
DLC=0x04&lt;br /&gt;
RTR=1&lt;br /&gt;
&lt;br /&gt;
gives:&lt;br /&gt;
array[0]=0xA0&lt;br /&gt;
array[1]=0x0A&lt;br /&gt;
&lt;br /&gt;
corect is&lt;br /&gt;
array[0]=0x64&lt;br /&gt;
array[1]=0x01&lt;br /&gt;
&lt;br /&gt;
the identifier is the 11. higist bit, RTR bit and last 4 DLC bits.&lt;br /&gt;
is something wrong with my order of my struct?&lt;br /&gt;
&lt;br /&gt;
Soren&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Struct in union</title><link>https://community.arm.com/thread/42940?ContentTypeID=1</link><pubDate>Sun, 10 Oct 2004 08:52:10 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:04b06a19-4c59-4c2e-9ff8-e30b3708e818</guid><dc:creator>Jon Ward</dc:creator><description>&lt;p&gt;The bit-fields in your struct are different types:&lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;
     unsigned char DLC:4;
     unsigned char RTR:1;
     unsigned int identifier:11;
&lt;/pre&gt;
&lt;br /&gt;
Try making them all unsigned int rather than mixing types.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Jon&lt;/b&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>