<?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>Bit operation</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/16309/bit-operation</link><description>Hello.
I use nRF52 , PCA10040 , S132 , SDK 11 &amp;amp; Keil5. 
 I want nrf to do bit operation.
The code about bit operation is following. 
 unsigned char tmp[2];
unsigned short us;
tmp[0] = 0xf8;
tmp[1] = 0x59;
us = (tmp[0] &amp;lt;&amp;lt; 8) | tmp[1];
printf(</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Mon, 12 Sep 2016 00:44:28 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/16309/bit-operation" /><item><title>RE: Bit operation</title><link>https://devzone.nordicsemi.com/thread/62353?ContentTypeID=1</link><pubDate>Mon, 12 Sep 2016 00:44:28 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:0c9576f3-223a-4c58-ba18-a8db52bb23c1</guid><dc:creator>Sagisawa Yukichi</dc:creator><description>&lt;p&gt;Thanks many reply! and I&amp;#39;m sorry to reply late.
Now I set Keil to c99, I think I use C language.
I changed %d to %u, the code output 63577.
From this point of view, I think RK-san said is true.&lt;/p&gt;
&lt;p&gt;Thanks!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Bit operation</title><link>https://devzone.nordicsemi.com/thread/62352?ContentTypeID=1</link><pubDate>Fri, 09 Sep 2016 22:26:57 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:c0aba012-cc93-4b82-92d1-6eca8c1af5a1</guid><dc:creator>Roger Clark</dc:creator><description>&lt;p&gt;BTW.
I was referring to gcc a toolchain rather than the compiler (perhaps I should have made this explicit)&lt;/p&gt;
&lt;p&gt;I don&amp;#39;t have time to try all the different gcc libs, I suspect they all behave the same&lt;/p&gt;
&lt;p&gt;But as a matter of interest, I compiled the code in Visual Studio, targeted for Win32 and it also works fine.&lt;/p&gt;
&lt;p&gt;I don&amp;#39;t have any other 32 bit compilers, but perhaps someone else has IAR and can confirm the behavior under that toolchain&lt;/p&gt;
&lt;p&gt;Perhaps the OP can try the different libs in Keil&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Bit operation</title><link>https://devzone.nordicsemi.com/thread/62351?ContentTypeID=1</link><pubDate>Fri, 09 Sep 2016 12:06:18 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:0204a5ca-88b4-4962-a79b-0c89182d19e8</guid><dc:creator>RK</dc:creator><description>&lt;p&gt;yes but that bothers me too. Because printf() is always defined with an ellipsis and so you&amp;#39;d expect the compiler would have to turn it into a va_arg call, because you&amp;#39;d expect the library would use va_arg to parse it. So there is some kind of link between the compiler and the library. I know gcc has a __builtin_va* so perhaps that&amp;#39;s the glue.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Bit operation</title><link>https://devzone.nordicsemi.com/thread/62350?ContentTypeID=1</link><pubDate>Fri, 09 Sep 2016 11:07:32 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:231b6d4b-4198-4fa0-ae60-1c40afca0d6a</guid><dc:creator>awneil</dc:creator><description>&lt;p&gt;So this would depend on the particular &lt;strong&gt;library&lt;/strong&gt; you&amp;#39;re using - less directly on the actual compiler.&lt;/p&gt;
&lt;p&gt;Keil has a number of options, and there are various libraries that can be used with GCC...&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Bit operation</title><link>https://devzone.nordicsemi.com/thread/62349?ContentTypeID=1</link><pubDate>Fri, 09 Sep 2016 10:47:49 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:b664eaae-48dd-4f37-a206-106ab0d6678c</guid><dc:creator>RK</dc:creator><description>&lt;p&gt;gcc (at least this version) treats printf as an ordinary function with lots of arguments, so the first few go in registers, the next go on the stack, but all promoted to 32 bit as per the usual ARM ABI. Keil, I believe from what I have read, treats printf as a va_arg function and concatenates all the arguments into a block of memory then passes that and printf reads it with the equivalent of va_start() .. etc. So if you get the format specifier wrong on keil, you read (in this case) two extra bytes of random junk.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Bit operation</title><link>https://devzone.nordicsemi.com/thread/62348?ContentTypeID=1</link><pubDate>Fri, 09 Sep 2016 10:30:35 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:1e75a0fb-cd6d-4562-99c8-72e5325e04b9</guid><dc:creator>Roger Clark</dc:creator><description>&lt;p&gt;OK. Thanks. I didn&amp;#39;t see that.&lt;/p&gt;
&lt;p&gt;gcc seems to behave differently to keil&lt;/p&gt;
&lt;p&gt;I tried&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;char buf[64];
sprintf(buf,&amp;quot;%d&amp;quot;,us); 
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;and it gave the result the OP expected.&lt;/p&gt;
&lt;p&gt;I tried using   us   as an int as a short (still no problem using gcc )&lt;/p&gt;
&lt;p&gt;Perhaps gcc automatically casts unsigned short to unsigned int in sprintf&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Bit operation</title><link>https://devzone.nordicsemi.com/thread/62347?ContentTypeID=1</link><pubDate>Fri, 09 Sep 2016 08:19:33 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:f5047025-fce7-40a3-8b4b-c140cf8e4481</guid><dc:creator>awneil</dc:creator><description>&lt;p&gt;Stated in OP: it&amp;#39;s &lt;strong&gt;Keil&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.keil.com/support/man/docs/armcc/armcc_chr1359125009502.htm"&gt;www.keil.com/.../armcc_chr1359125009502.htm&lt;/a&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Bit operation</title><link>https://devzone.nordicsemi.com/thread/62343?ContentTypeID=1</link><pubDate>Fri, 09 Sep 2016 07:58:38 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:2df581e7-ec15-415d-a9b4-e1e880a18479</guid><dc:creator>Roger Clark</dc:creator><description>&lt;p&gt;What compiler are you using.&lt;/p&gt;
&lt;p&gt;I&amp;#39;ve tried to replicate this using gcc and I get the correct value.&lt;/p&gt;
&lt;p&gt;The number you quoted as being display by printf  is 0x2553F444FB which makes no sense at all&lt;/p&gt;
&lt;p&gt;You could try&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;us = (tmp[0] *256 ) + tmp[1]; 
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;As the compiler is smart enough to realize that * 256 can be done by shifting.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Bit operation</title><link>https://devzone.nordicsemi.com/thread/62346?ContentTypeID=1</link><pubDate>Fri, 09 Sep 2016 07:46:32 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:00fed631-2e6a-4c67-a435-2474a276359a</guid><dc:creator>Sagisawa Yukichi</dc:creator><description>&lt;p&gt;I mistake printf format.
Thank you!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Bit operation</title><link>https://devzone.nordicsemi.com/thread/62345?ContentTypeID=1</link><pubDate>Fri, 09 Sep 2016 07:39:41 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:6407b14b-7fbd-475c-af48-3f111bfa8337</guid><dc:creator>Sagisawa Yukichi</dc:creator><description>&lt;p&gt;Oh, I think short is 2byte integer data.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Bit operation</title><link>https://devzone.nordicsemi.com/thread/62344?ContentTypeID=1</link><pubDate>Fri, 09 Sep 2016 07:37:59 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:e88dd0cb-1c2e-4de8-8822-8c5b39b1649d</guid><dc:creator>Sagisawa Yukichi</dc:creator><description>&lt;p&gt;while(1){printf(&amp;quot;%d\r\n&amp;quot;,us);}
It did print out 160, 322, 307, 323.&lt;/p&gt;
&lt;p&gt;In detail my question, I use sensor what
communication i2c.
tmp is data from the sensor.
I try the following code,&lt;/p&gt;
&lt;p&gt;unsigned short us = (tmp[0] &amp;lt;&amp;lt; 8) | tmp[1];
printf(&amp;quot;%x,%x -&amp;gt; %d\r\n&amp;quot;,tmp[0],tmp[1],us);&lt;/p&gt;
&lt;p&gt;It output f8,59,322 .&lt;/p&gt;
&lt;p&gt;But
unsigned short us = (0xf8 &amp;lt;&amp;lt; 8) | 0x59;
printf(&amp;quot;%x,%x -&amp;gt; %d\r\n&amp;quot;,tmp[0],tmp[1],us);
It output f8,59,63577 .
What shall I do?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Bit operation</title><link>https://devzone.nordicsemi.com/thread/62354?ContentTypeID=1</link><pubDate>Fri, 09 Sep 2016 07:30:21 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:f6169b27-858d-4932-afea-52087b5c4e27</guid><dc:creator>awneil</dc:creator><description>&lt;p&gt;Agreed: this is standard &amp;#39;C&amp;#39; - nothing specific to Nordic.&lt;/p&gt;
&lt;p&gt;What number does it print? Have you thought about the significance of that number?&lt;/p&gt;
&lt;p&gt;%d expects an int - not a short.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.cplusplus.com/reference/cstdio/printf/"&gt;www.cplusplus.com/.../&lt;/a&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Bit operation</title><link>https://devzone.nordicsemi.com/thread/62342?ContentTypeID=1</link><pubDate>Fri, 09 Sep 2016 07:22:31 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:a653ad3f-e19b-4d4b-a648-94ea8f2f6c9e</guid><dc:creator>RK</dc:creator><description>&lt;p&gt;Not really a Nordic question at all in any way - what number does it print out?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>