<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[[Solved] Simple XOR&#x27;ing goes wrong]]></title><description><![CDATA[<p dir="auto">I have an issue about a simple XOR operation. I have the following code (which is simplified to norrow down the issue).</p>
<p dir="auto">@#include &lt;QCoreApplication&gt;<br />
#include &lt;QByteArray&gt;</p>
<p dir="auto">unsigned short CalculateCRC_ISO13239(QByteArray* data)<br />
{<br />
unsigned short crc_val = 0xFFFF;<br />
int i,j;</p>
<pre><code>for (i = 0; i &lt; data-&gt;length(); i++)
{
    crc_val = (unsigned short)(crc_val ^ (unsigned short)(*data)[i]);
    for (j = 0; j &lt; 8; j++)
    {
        if ((crc_val &amp; 0x0001) != 0)
        {
            crc_val = (unsigned short)((crc_val &gt;&gt; 1) ^ 0x8408);
        }
        else
        {
            crc_val = (unsigned short)(crc_val &gt;&gt; 1);
        }
    }
}

crc_val = (unsigned short)(~crc_val);

return crc_val;
</code></pre>
<p dir="auto">}</p>
<p dir="auto">int main(int argc, char *argv[])<br />
{<br />
QCoreApplication a(argc, argv);</p>
<pre><code>unsigned char ref[] = { 0, 1, 12, 5, 182 };
QByteArray* data = new QByteArray((char*)ref, 5);
unsigned char crc = CalculateCRC_ISO13239(data);

return a.exec&amp;#40;&amp;#41;;
</code></pre>
<p dir="auto">}@</p>
<p dir="auto">This calculation function does not work as expected. Followed the process step by step and seen that the XOR operation does not have the right result at step i=4. (I have a breakpoint at the "crc_val = (unsigned short)(crc_val ^ (unsigned short)(*data)[i]);" line. I'm pressing F5 4 times and then press F10 to see the line's step.</p>
<p dir="auto">The expected calculation is 0xa7f0 ^ 0xb6 = 0xa746. But somehow it gives the 0x5846 result. If you look bitwise, you'll see that the LSB is correct but the MSB is the opposite what it should be.</p>
<p dir="auto">I must be blind for not being able to see what's wrong, but here I am. Tried both MinGW and MSVC. The result is the same.</p>
<p dir="auto">What's wrong with this code (or with me :) ?</p>
<p dir="auto">P.S: A very similar code is running fine on C#.</p>
<p dir="auto">Tool: Qt 5.3.1<br />
IDE: QtCreator<br />
Compiler: MSVC 2012 &amp; MinGW 4.8.2</p>
]]></description><link>https://forum.qt.io/topic/43620/solved-simple-xor-ing-goes-wrong</link><generator>RSS for Node</generator><lastBuildDate>Thu, 09 Apr 2026 13:39:40 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/43620.rss" rel="self" type="application/rss+xml"/><pubDate>Sat, 12 Jul 2014 22:04:14 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to [Solved] Simple XOR&#x27;ing goes wrong on Tue, 15 Jul 2014 14:53:16 GMT]]></title><description><![CDATA[<p dir="auto">You're most welcome :) All the best with your project!</p>
]]></description><link>https://forum.qt.io/post/235928</link><guid isPermaLink="true">https://forum.qt.io/post/235928</guid><dc:creator><![CDATA[JKSH]]></dc:creator><pubDate>Tue, 15 Jul 2014 14:53:16 GMT</pubDate></item><item><title><![CDATA[Reply to [Solved] Simple XOR&#x27;ing goes wrong on Tue, 15 Jul 2014 14:50:12 GMT]]></title><description><![CDATA[<p dir="auto">Yes,  also had realized that while investigating the issue and covered that with changing the type char to unsigned char (not just the casting). The result was the same.</p>
<p dir="auto">Thanks for the support, again :)</p>
]]></description><link>https://forum.qt.io/post/235924</link><guid isPermaLink="true">https://forum.qt.io/post/235924</guid><dc:creator><![CDATA[MuratUrsavas]]></dc:creator><pubDate>Tue, 15 Jul 2014 14:50:12 GMT</pubDate></item><item><title><![CDATA[Reply to [Solved] Simple XOR&#x27;ing goes wrong on Tue, 15 Jul 2014 14:39:14 GMT]]></title><description><![CDATA[<p dir="auto">Thank you too for trying out my suggestions and reporting back :)</p>
<p dir="auto">By the way, I've just realized the reason for your error: unsigned char can hold values from 0 to 255, but signed char can only hold values from -128 to 127.</p>
<p dir="auto">The value 182 doesn't fit in a signed char, but you tried to cast it anyway in line #35 of your original post. 0xB6 is 182 (uchar) or -74 (char). When you cast that to 16 bit, you get the 16-bit representation of -74, which is 0xFFB6</p>
<p dir="auto">(And this is another example of why we need to be careful with casting)</p>
]]></description><link>https://forum.qt.io/post/235922</link><guid isPermaLink="true">https://forum.qt.io/post/235922</guid><dc:creator><![CDATA[JKSH]]></dc:creator><pubDate>Tue, 15 Jul 2014 14:39:14 GMT</pubDate></item><item><title><![CDATA[Reply to [Solved] Simple XOR&#x27;ing goes wrong on Mon, 14 Jul 2014 15:20:36 GMT]]></title><description><![CDATA[<p dir="auto">As expected, QVector solution works fine, since there is no requirement for converting a byte to word (just ushort to long). The effected byte is the second one which is already covered by the vector class.</p>
<p dir="auto">The static_cast didn't work. It was the first thing I tried after your first reply.</p>
<p dir="auto">bq. You shouldn’t create QByteArray, QVector, or other Qt containers using new, as they are implicitly shared [<a href="http://qt-project.org" target="_blank" rel="noopener noreferrer nofollow ugc">qt-project.org</a>]. Create the container on the stack, and pass it into the function by const-reference instead:</p>
<p dir="auto">This is a good suggestion for me, as I'm new to Qt and C++ :) Thanks again.</p>
]]></description><link>https://forum.qt.io/post/235781</link><guid isPermaLink="true">https://forum.qt.io/post/235781</guid><dc:creator><![CDATA[MuratUrsavas]]></dc:creator><pubDate>Mon, 14 Jul 2014 15:20:36 GMT</pubDate></item><item><title><![CDATA[Reply to [Solved] Simple XOR&#x27;ing goes wrong on Mon, 14 Jul 2014 14:48:26 GMT]]></title><description><![CDATA[<p dir="auto">What happens if you do either one of the following?</p>
<ul>
<li>Cast using <em>static_cast&lt;unsigned short&gt;(*data[i])</em> instead of the C-style cast (or having no cast).</li>
<li>Replace the <em>QByteArray</em> with a <em>QVector&lt;unsigned short&gt;</em>.</li>
</ul>
<p dir="auto">Some other general tips:</p>
<ul>
<li>Qt provides nice typedefs: You can write "quint16" instead of "unsigned short".</li>
<li>You shouldn't create QByteArray, QVector, or other Qt containers using <em>new</em>, as they are "implicitly shared":<a href="http://qt-project.org/doc/qt-5/implicit-sharing.html" target="_blank" rel="noopener noreferrer nofollow ugc">http://qt-project.org/doc/qt-5/implicit-sharing.html</a>. Create the container on the stack, and pass it into the function by const-reference instead:<br />
@<br />
// Function prototype<br />
quint16 CalculateCRC_ISO13239_16(const QByteArray&amp; data);<br />
@<br />
@<br />
// Function call<br />
QByteArray data = ...;<br />
quint16 crc = CalculateCRC_ISO13239_16(data);<br />
@</li>
</ul>
]]></description><link>https://forum.qt.io/post/235779</link><guid isPermaLink="true">https://forum.qt.io/post/235779</guid><dc:creator><![CDATA[JKSH]]></dc:creator><pubDate>Mon, 14 Jul 2014 14:48:26 GMT</pubDate></item><item><title><![CDATA[Reply to [Solved] Simple XOR&#x27;ing goes wrong on Mon, 14 Jul 2014 14:40:25 GMT]]></title><description><![CDATA[<p dir="auto">Please note that the function above works for MSVC. MinGW still has the same problem probably because not removing the cbtw instruction.</p>
<p dir="auto">For a workaround you can change this line:<br />
@crc_val ^= (*data)[i];@</p>
<p dir="auto">with this:<br />
@crc_val ^= (*data)[i] &amp; 0xFF;@</p>
<p dir="auto">It will solve the issue with a little performance penalty.</p>
]]></description><link>https://forum.qt.io/post/235777</link><guid isPermaLink="true">https://forum.qt.io/post/235777</guid><dc:creator><![CDATA[MuratUrsavas]]></dc:creator><pubDate>Mon, 14 Jul 2014 14:40:25 GMT</pubDate></item><item><title><![CDATA[Reply to [Solved] Simple XOR&#x27;ing goes wrong on Mon, 14 Jul 2014 13:16:16 GMT]]></title><description><![CDATA[<p dir="auto">I've used both MSVC 11.0 and MinGW 4.8.2. The results were the same. That's why I'm suspecting from an errata at chip level.</p>
<p dir="auto">My processor is an Intel Core i7-2675QM. I couldn't find the errata sheet of Sandy Bridge processors therefore couldn't find a proof related to this issue.</p>
]]></description><link>https://forum.qt.io/post/235759</link><guid isPermaLink="true">https://forum.qt.io/post/235759</guid><dc:creator><![CDATA[MuratUrsavas]]></dc:creator><pubDate>Mon, 14 Jul 2014 13:16:16 GMT</pubDate></item><item><title><![CDATA[Reply to [Solved] Simple XOR&#x27;ing goes wrong on Mon, 14 Jul 2014 01:35:21 GMT]]></title><description><![CDATA[<p dir="auto">I'm glad to hear that you've resolved the issue. Thanks for sharing your detailed investigations and solutions with the community!</p>
<p dir="auto">[quote]Code gets the QByteArray member from the array correctly but fills extra “0xff” for the second byte at “cbtw” call. This does not happen for the first four loop runs.</p>
<p dir="auto">Removing the casting and using directly the byte value solved the issue (since it removed the cbtw instruction) but I’m still suspecting from an errata (even at chip level).[/quote]That's an interesting quirk. What compiler/chip did you use?</p>
]]></description><link>https://forum.qt.io/post/235688</link><guid isPermaLink="true">https://forum.qt.io/post/235688</guid><dc:creator><![CDATA[JKSH]]></dc:creator><pubDate>Mon, 14 Jul 2014 01:35:21 GMT</pubDate></item><item><title><![CDATA[Reply to [Solved] Simple XOR&#x27;ing goes wrong on Sun, 13 Jul 2014 20:14:57 GMT]]></title><description><![CDATA[<p dir="auto">Well, it is really strange. I've investigated the issue at instruction level and seen that one instruction is acting weird at fifth step, which is "cbtw: convert byte to word".</p>
<p dir="auto">Code gets the QByteArray member from the array correctly but fills extra "0xff" for the second byte at "cbtw" call. This does not happen for the first four loop runs.</p>
<p dir="auto">Removing the casting and using directly the byte value solved the issue (since it removed the cbtw instruction) but I'm still suspecting from an errata (even at chip level).</p>
<p dir="auto">Anyway, here's the correct function.</p>
<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/unsigned">@<bdi>unsigned</bdi></a> short CalculateCRC_ISO13239_16(QByteArray* data)<br />
{<br />
unsigned short crc_val = 0xFFFF;<br />
int i,j;</p>
<pre><code>for (i = 0; i &lt; data-&gt;length(); i++)
{
    crc_val ^= (*data)[i];
    for (j = 0; j &lt; 8; j++)
    {
        if ((crc_val &amp; 0x0001) != 0)
        {
            crc_val = (crc_val &gt;&gt; 1) ^ 0x8408;
        }
        else
        {
            crc_val &gt;&gt;= 1;
        }
    }
}

crc_val = ~crc_val;

return crc_val;
</code></pre>
<p dir="auto">}@</p>
]]></description><link>https://forum.qt.io/post/235675</link><guid isPermaLink="true">https://forum.qt.io/post/235675</guid><dc:creator><![CDATA[MuratUrsavas]]></dc:creator><pubDate>Sun, 13 Jul 2014 20:14:57 GMT</pubDate></item><item><title><![CDATA[Reply to [Solved] Simple XOR&#x27;ing goes wrong on Sun, 13 Jul 2014 19:16:34 GMT]]></title><description><![CDATA[<p dir="auto">I've seen the issue via using an intermediate variable. The last operand "0xb6" (*data[4]) comes as "0xffb6" from the byte array. This makes the MSB reversed.</p>
<p dir="auto">How is that even possible?</p>
]]></description><link>https://forum.qt.io/post/235674</link><guid isPermaLink="true">https://forum.qt.io/post/235674</guid><dc:creator><![CDATA[MuratUrsavas]]></dc:creator><pubDate>Sun, 13 Jul 2014 19:16:34 GMT</pubDate></item><item><title><![CDATA[Reply to [Solved] Simple XOR&#x27;ing goes wrong on Sun, 13 Jul 2014 18:47:41 GMT]]></title><description><![CDATA[<p dir="auto">Hi,</p>
<p dir="auto">Thanks for the suggestions. Good to know that ISO13239 have two CRC lengths. Since I've only used 16 bits I didn't know there were a 32 bits option.</p>
<p dir="auto">If I want to XOR same length bits I have to use QVector like you said and cast from byte array to ushort with a padding byte. But as far as I know this validation does not need word aligning, i.e. 16 bits. And everything comes down to a simple XOR operation. The first four operation is correct but the fifth is not. Tried the static casting, the result is the same.</p>
<p dir="auto">"unsigned char crc" is just a quick typo while trying to create a simple project for the issue.</p>
<p dir="auto">And the heavy type casting is coming from my embedded development background. It gave me lots of headaches in the past and now I'm maybe using just too many. :)</p>
<p dir="auto">Long story short, problem is still there and I have no idea what's going wrong.</p>
]]></description><link>https://forum.qt.io/post/235672</link><guid isPermaLink="true">https://forum.qt.io/post/235672</guid><dc:creator><![CDATA[MuratUrsavas]]></dc:creator><pubDate>Sun, 13 Jul 2014 18:47:41 GMT</pubDate></item><item><title><![CDATA[Reply to [Solved] Simple XOR&#x27;ing goes wrong on Sun, 13 Jul 2014 01:15:13 GMT]]></title><description><![CDATA[<p dir="auto">Hi,</p>
<p dir="auto">Some notes:</p>
<h1>ISO 13239 supports both CRC-16 and CRC-32. Your function name could be more specific.</h1>
<h1><em>char</em> is 8 bits but <em>short</em> is 16 bits.</h1>
<h2>When you XOR, both values must have the same number of bits.</h2>
<h2>In your <em>main()</em> function, you converted your CRC result into an 8-bit value. Are you sure this is what you want?</h2>
<h1>Why do you have so many casts? Just use QVector&lt;unsigned short&gt; instead of QByteArray.</h1>
<h1>Anyway, if you really must use casts, don't use a C-style cast. These are unsafe because you might get a reinterpret_cast without realizing (this means your data gets corrupted).</h1>
<p dir="auto">@<br />
// Don't use a C-style cast...<br />
unsigned short value2 = (unsigned short)(value1);</p>
<p dir="auto">// ... use a static_cast instead<br />
unsigned short value2 = static_cast&lt;unsigned short&gt;(value1);<br />
@</p>
]]></description><link>https://forum.qt.io/post/235623</link><guid isPermaLink="true">https://forum.qt.io/post/235623</guid><dc:creator><![CDATA[JKSH]]></dc:creator><pubDate>Sun, 13 Jul 2014 01:15:13 GMT</pubDate></item></channel></rss>