<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://devzone.nordicsemi.com/cfs-file/__key/system/syndication/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/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>how to force the bit order in a struct</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/15517/how-to-force-the-bit-order-in-a-struct</link><description>Hi, 
 I saw several structs that you have in your library and I am curious as how you are forcing the alignment, since I want to do the same. 
 This is one of them: 
 typedef union
{
 struct
 {
 uint32_t _reserved0:16; /*!&amp;lt; bit: 0..15 Reserved</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Wed, 03 Aug 2016 16:35:01 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/15517/how-to-force-the-bit-order-in-a-struct" /><item><title>RE: how to force the bit order in a struct</title><link>https://devzone.nordicsemi.com/thread/59260?ContentTypeID=1</link><pubDate>Wed, 03 Aug 2016 16:35:01 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:68dc9c3b-5a85-4ca3-bd3c-f6d8c0a86e3e</guid><dc:creator>Antonio</dc:creator><description>&lt;p&gt;Thank you Aryan, Vidar and Alex!&lt;/p&gt;
&lt;p&gt;What I did not know was &amp;quot;Least significant bits always declared at the top of structure &amp;quot;. That did the trick!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: how to force the bit order in a struct</title><link>https://devzone.nordicsemi.com/thread/59265?ContentTypeID=1</link><pubDate>Wed, 03 Aug 2016 16:34:02 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:f084abd5-9ca6-4ea0-a056-74549a50dc3e</guid><dc:creator>Antonio</dc:creator><description>&lt;p&gt;Thank you Aryan, Vidar and Alex!&lt;/p&gt;
&lt;p&gt;What I did not know was &amp;quot;Least significant bits always declared at the top of structure &amp;quot;. That did the trick!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: how to force the bit order in a struct</title><link>https://devzone.nordicsemi.com/thread/59264?ContentTypeID=1</link><pubDate>Wed, 03 Aug 2016 14:16:12 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:5d65399c-9919-4752-b376-a3b66036b6ce</guid><dc:creator>Alex</dc:creator><description>&lt;p&gt;To be honest I can&amp;#39;t see any problem here. Least significant bits always declared at the top of structure and you have it.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: how to force the bit order in a struct</title><link>https://devzone.nordicsemi.com/thread/59263?ContentTypeID=1</link><pubDate>Wed, 03 Aug 2016 14:01:45 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:13e9be1a-00cc-498f-a640-cd5bcd22ccfa</guid><dc:creator>Vidar Berg</dc:creator><description>&lt;p&gt;Are you sure you are reading out the bytes in the right order? The cortex M0 is little endian so it&amp;#39;s more readable if you change the format to &amp;#39;unsigned long&amp;#39; in your memory viewer.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: how to force the bit order in a struct</title><link>https://devzone.nordicsemi.com/thread/59262?ContentTypeID=1</link><pubDate>Wed, 03 Aug 2016 13:35:02 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:d30fbc44-0d7d-4a75-a363-7d378958722c</guid><dc:creator>Antonio</dc:creator><description>&lt;p&gt;sh (short) is for accessing the whole half world, hence the union with the bits. I tried uint32_t and did not worked either. The compiler shuffled the fields all around. This is the order that it came out after looking at the memory, as if it placed the number of bits in descending order:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;typedef union {
	struct {
		uint32_t			sw_min  :5; //1 00001
		uint32_t			sw_maj	:3;	//0 000
		uint32_t			brd_num	:3;	//3 011
		uint32_t			prod_num:3;	//4 100
		uint32_t			pk_type	:2;	//0 00
	}				b;
	
	uint32_t 		w;			// 08 70 = 0000 1000 0111 0000
	} ps_btn_dev_info_t;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;I am using Keil 5.2, SD 132 and SDK 11.0&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: how to force the bit order in a struct</title><link>https://devzone.nordicsemi.com/thread/59261?ContentTypeID=1</link><pubDate>Wed, 03 Aug 2016 07:34:43 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:b7e1e274-3f7c-475a-ac37-03650fe3628f</guid><dc:creator>Susheel Nuguru</dc:creator><description>&lt;p&gt;not sure what the compiler is doing with half words. It should have worked. Can you try uint32_t and see if you get desired output?&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;typedef union {
    struct {
        uint32_t            pk_type :2; // 00
        uint32_t            prod_num:3; // 100
        uint32_t            brd_num :3; // 011
        uint32_t            sw_maj  :3; // 000
        uint32_t            sw_min  :5; // 00001
        uint32_t            sh  :16 ;
    }               b;
    } ps_btn_dev_info_t;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;notice that i moved sh into the union.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>