<?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[Is there a possibility to set a QBitArray using a long int?]]></title><description><![CDATA[<p dir="auto">Hello,<br />
I have a long int variable and I have a QBitArray of size(32).<br />
Is it possible to write the long int value to the QBitArray and read individual bits?<br />
Or is there any other better option in Qt to read bits of an integer?</p>
<p dir="auto">Thanks again!</p>
]]></description><link>https://forum.qt.io/topic/139360/is-there-a-possibility-to-set-a-qbitarray-using-a-long-int</link><generator>RSS for Node</generator><lastBuildDate>Fri, 06 Nov 2026 10:31:16 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/139360.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 16 Sep 2022 09:40:19 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Is there a possibility to set a QBitArray using a long int? on Fri, 16 Sep 2022 11:24:20 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jonb">@<bdi>JonB</bdi></a> said in <a href="/post/729000">Is there a possibility to set a QBitArray using a long int?</a>:</p>
<blockquote>
<p dir="auto">[Separate: did I see recently that C++20 or whatever has introduced yet another new somethingelse_cast&lt;&gt; construct beyond these 4, or do you think I'm imagining this?]</p>
</blockquote>
<p dir="auto"><strong>std::bit_cast</strong></p>
<p dir="auto">but,  it's a library feature not a language feature.</p>
<p dir="auto">Technically, if the OP uses c++20 it would be the "saver" option, otherwise reinterpret_cast</p>
]]></description><link>https://forum.qt.io/post/729002</link><guid isPermaLink="true">https://forum.qt.io/post/729002</guid><dc:creator><![CDATA[J.Hilk]]></dc:creator><pubDate>Fri, 16 Sep 2022 11:24:20 GMT</pubDate></item><item><title><![CDATA[Reply to Is there a possibility to set a QBitArray using a long int? on Fri, 16 Sep 2022 11:17:16 GMT]]></title><description><![CDATA[<p dir="auto">@J-Hilk said in <a href="/post/728995">Is there a possibility to set a QBitArray using a long int?</a>:</p>
<blockquote>
<p dir="auto">reinterpret_cast is for special circumstances and you think you know better than the compiler.  Converting between pointers or references and completely unrelated types.</p>
</blockquote>
<p dir="auto">And that (<code>reinterpret_cast&lt;&gt;</code>) is what should be used in the case we are discussing (<code>int *</code> -&gt; <code>char *</code>), right?</p>
<p dir="auto">[Separate: did I see recently that C++20 or whatever has introduced yet another new <code>somethingelse_cast&lt;&gt;</code> construct beyond these 4, or do you think I'm imagining this?]</p>
<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/christian-ehrlicher">@<bdi>Christian-Ehrlicher</bdi></a> said in <a href="/post/728997">Is there a possibility to set a QBitArray using a long int?</a>:</p>
<blockquote>
<p dir="auto">They add validation if the cast is valid which is not available with the c-style cast.</p>
</blockquote>
<p dir="auto">But <em>not</em> in the <code>reinterpret_cast&lt;&gt;</code> case which (I believe) we are going to use here, right?</p>
]]></description><link>https://forum.qt.io/post/729000</link><guid isPermaLink="true">https://forum.qt.io/post/729000</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Fri, 16 Sep 2022 11:17:16 GMT</pubDate></item><item><title><![CDATA[Reply to Is there a possibility to set a QBitArray using a long int? on Fri, 16 Sep 2022 11:13:13 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jonb">@<bdi>JonB</bdi></a> said in <a href="/post/728991">Is there a possibility to set a QBitArray using a long int?</a>:</p>
<blockquote>
<p dir="auto">which seems to me to make a nonsense of half of these castings anyway...!</p>
</blockquote>
<p dir="auto">They add validation if the cast is valid which is not available with the c-style cast.</p>
]]></description><link>https://forum.qt.io/post/728997</link><guid isPermaLink="true">https://forum.qt.io/post/728997</guid><dc:creator><![CDATA[Christian Ehrlicher]]></dc:creator><pubDate>Fri, 16 Sep 2022 11:13:13 GMT</pubDate></item><item><title><![CDATA[Reply to Is there a possibility to set a QBitArray using a long int? on Fri, 16 Sep 2022 11:13:03 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jonb">@<bdi>JonB</bdi></a></p>
<ul>
<li>static_cast for basic types</li>
<li>dynamic_cast for pointers or references to polymorphic class types;</li>
<li>const_cast const or volatile qualifiers;</li>
<li>reinterpret_cast is for special circumstances and you think you know better than the compiler.  Converting between pointers or references and completely unrelated types.</li>
</ul>
]]></description><link>https://forum.qt.io/post/728995</link><guid isPermaLink="true">https://forum.qt.io/post/728995</guid><dc:creator><![CDATA[J.Hilk]]></dc:creator><pubDate>Fri, 16 Sep 2022 11:13:03 GMT</pubDate></item><item><title><![CDATA[Reply to Is there a possibility to set a QBitArray using a long int? on Fri, 16 Sep 2022 11:05:02 GMT]]></title><description><![CDATA[<p dir="auto">@J-Hilk<br />
I always have to go look it up, should this one be <code>reinterpret_cast&lt;&gt;</code>, or has C++ introduced yet another new one of these casting templates to use for this case?</p>
<p dir="auto">Not to mention that the other day <a class="plugin-mentions-user plugin-mentions-a" href="/user/chris-kawa">@<bdi>Chris-Kawa</bdi></a> wrote in <a href="https://forum.qt.io/topic/139176/qgraphicsitem-conversion-into-long-using-reinterpret_cast/2">https://forum.qt.io/topic/139176/qgraphicsitem-conversion-into-long-using-reinterpret_cast/2</a></p>
<blockquote>
<p dir="auto">I'll just mention that on some platforms pointers to different types can have different sizes too i.e. <code>sizeof(char*) != sizeof(int*)</code>, so, although unlikely that it affects you, what you're doing is a risky business.</p>
</blockquote>
<p dir="auto">which seems to me to make a nonsense of half of these castings anyway...!</p>
]]></description><link>https://forum.qt.io/post/728991</link><guid isPermaLink="true">https://forum.qt.io/post/728991</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Fri, 16 Sep 2022 11:05:02 GMT</pubDate></item><item><title><![CDATA[Reply to Is there a possibility to set a QBitArray using a long int? on Fri, 16 Sep 2022 10:51:34 GMT]]></title><description><![CDATA[<p dir="auto">@J-Hilk said in <a href="/post/728982">Is there a possibility to set a QBitArray using a long int?</a>:</p>
<blockquote>
<p dir="auto">where's the downvote button</p>
</blockquote>
<p dir="auto">Casted to <code>(void)downvoteBtn;</code></p>
<p dir="auto">:)</p>
]]></description><link>https://forum.qt.io/post/728983</link><guid isPermaLink="true">https://forum.qt.io/post/728983</guid><dc:creator><![CDATA[Pl45m4]]></dc:creator><pubDate>Fri, 16 Sep 2022 10:51:34 GMT</pubDate></item><item><title><![CDATA[Reply to Is there a possibility to set a QBitArray using a long int? on Fri, 16 Sep 2022 10:43:41 GMT]]></title><description><![CDATA[<p dir="auto">where's the downvote button!?!?! c-style casts 🫢</p>
]]></description><link>https://forum.qt.io/post/728982</link><guid isPermaLink="true">https://forum.qt.io/post/728982</guid><dc:creator><![CDATA[J.Hilk]]></dc:creator><pubDate>Fri, 16 Sep 2022 10:43:41 GMT</pubDate></item><item><title><![CDATA[Reply to Is there a possibility to set a QBitArray using a long int? on Fri, 16 Sep 2022 10:41:40 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/kevin470">@<bdi>Kevin470</bdi></a> Then please mark this topic as solved, thx.</p>
]]></description><link>https://forum.qt.io/post/728981</link><guid isPermaLink="true">https://forum.qt.io/post/728981</guid><dc:creator><![CDATA[Christian Ehrlicher]]></dc:creator><pubDate>Fri, 16 Sep 2022 10:41:40 GMT</pubDate></item><item><title><![CDATA[Reply to Is there a possibility to set a QBitArray using a long int? on Fri, 16 Sep 2022 10:22:17 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jsulm">@<bdi>jsulm</bdi></a> Thanks a lot. Works Perfectly!</p>
<pre><code>QBitArray bitArray;
    quint32 intvalue = 9999999;

    bitArray = QBitArray::fromBits((char*)(&amp;intvalue),32);
    qInfo() &lt;&lt; "Size:" &lt;&lt; bitArray.size();
</code></pre>
]]></description><link>https://forum.qt.io/post/728974</link><guid isPermaLink="true">https://forum.qt.io/post/728974</guid><dc:creator><![CDATA[Kevin470]]></dc:creator><pubDate>Fri, 16 Sep 2022 10:22:17 GMT</pubDate></item><item><title><![CDATA[Reply to Is there a possibility to set a QBitArray using a long int? on Fri, 16 Sep 2022 10:17:42 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/kevin470">@<bdi>Kevin470</bdi></a> said in <a href="/post/728970">Is there a possibility to set a QBitArray using a long int?</a>:</p>
<blockquote>
<p dir="auto">But it still prints Size: 0</p>
</blockquote>
<p dir="auto">Yes, because <a href="https://doc.qt.io/qt-6/qbitarray.html#fromBits" target="_blank" rel="noopener noreferrer nofollow ugc">https://doc.qt.io/qt-6/qbitarray.html#fromBits</a> is a static method...</p>
]]></description><link>https://forum.qt.io/post/728972</link><guid isPermaLink="true">https://forum.qt.io/post/728972</guid><dc:creator><![CDATA[jsulm]]></dc:creator><pubDate>Fri, 16 Sep 2022 10:17:42 GMT</pubDate></item><item><title><![CDATA[Reply to Is there a possibility to set a QBitArray using a long int? on Fri, 16 Sep 2022 10:15:58 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jsulm">@<bdi>jsulm</bdi></a> Oops sorry. Yes it should be <code>bitArray.fromBits((char*)(&amp;intvalue), 32);</code></p>
<p dir="auto">But it still prints <code>Size: 0</code></p>
]]></description><link>https://forum.qt.io/post/728970</link><guid isPermaLink="true">https://forum.qt.io/post/728970</guid><dc:creator><![CDATA[Kevin470]]></dc:creator><pubDate>Fri, 16 Sep 2022 10:15:58 GMT</pubDate></item><item><title><![CDATA[Reply to Is there a possibility to set a QBitArray using a long int? on Fri, 16 Sep 2022 10:10:35 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/kevin470">@<bdi>Kevin470</bdi></a> said in <a href="/post/728965">Is there a possibility to set a QBitArray using a long int?</a>:</p>
<blockquote>
<p dir="auto">bitArray.fromBits((char*)(intvalue),32);</p>
</blockquote>
<p dir="auto">Shouldn't it be</p>
<pre><code>bitArray.fromBits((char*)(&amp;intvalue), 32);
</code></pre>
<p dir="auto">?</p>
]]></description><link>https://forum.qt.io/post/728967</link><guid isPermaLink="true">https://forum.qt.io/post/728967</guid><dc:creator><![CDATA[jsulm]]></dc:creator><pubDate>Fri, 16 Sep 2022 10:10:35 GMT</pubDate></item><item><title><![CDATA[Reply to Is there a possibility to set a QBitArray using a long int? on Fri, 16 Sep 2022 10:09:00 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/christian-ehrlicher">@<bdi>Christian-Ehrlicher</bdi></a><br />
Could you help me out with an example please?<br />
I tried</p>
<pre><code>    QBitArray bitArray;
    quint32 intvalue = 9999999;

    bitArray.fromBits(intvalue,32);
    qInfo() &lt;&lt; "Size:" &lt;&lt; bitArray.size();
</code></pre>
<p dir="auto">and it throws an error saying <code>main.cpp:12:23: Cannot initialize a parameter of type 'const char *' with an lvalue of type 'quint32' (aka 'unsigned int')</code></p>
<p dir="auto">If I try the one below, the program crashes.</p>
<pre><code>    QBitArray bitArray;
    quint32 intvalue = 9999999;

    bitArray.fromBits((char*)(intvalue),32);
    qInfo() &lt;&lt; "Size:" &lt;&lt; bitArray.size();
</code></pre>
]]></description><link>https://forum.qt.io/post/728965</link><guid isPermaLink="true">https://forum.qt.io/post/728965</guid><dc:creator><![CDATA[Kevin470]]></dc:creator><pubDate>Fri, 16 Sep 2022 10:09:00 GMT</pubDate></item><item><title><![CDATA[Reply to Is there a possibility to set a QBitArray using a long int? on Fri, 16 Sep 2022 09:45:32 GMT]]></title><description><![CDATA[<p dir="auto">Then use <a href="https://doc.qt.io/qt-6/qbitarray.html#fromBits" target="_blank" rel="noopener noreferrer nofollow ugc">https://doc.qt.io/qt-6/qbitarray.html#fromBits</a></p>
]]></description><link>https://forum.qt.io/post/728961</link><guid isPermaLink="true">https://forum.qt.io/post/728961</guid><dc:creator><![CDATA[Christian Ehrlicher]]></dc:creator><pubDate>Fri, 16 Sep 2022 09:45:32 GMT</pubDate></item><item><title><![CDATA[Reply to Is there a possibility to set a QBitArray using a long int? on Fri, 16 Sep 2022 09:43:58 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/christian-ehrlicher">@<bdi>Christian-Ehrlicher</bdi></a> Qt6.<br />
But I am looking for fromUInt32 and not toUInt32 unfortunately.</p>
]]></description><link>https://forum.qt.io/post/728960</link><guid isPermaLink="true">https://forum.qt.io/post/728960</guid><dc:creator><![CDATA[Kevin470]]></dc:creator><pubDate>Fri, 16 Sep 2022 09:43:58 GMT</pubDate></item><item><title><![CDATA[Reply to Is there a possibility to set a QBitArray using a long int? on Fri, 16 Sep 2022 09:42:44 GMT]]></title><description><![CDATA[<p dir="auto">Qt5 or Qt6?</p>
<p dir="auto"><a href="https://doc.qt.io/qt-6/qbitarray.html#toUInt32" target="_blank" rel="noopener noreferrer nofollow ugc">https://doc.qt.io/qt-6/qbitarray.html#toUInt32</a></p>
]]></description><link>https://forum.qt.io/post/728959</link><guid isPermaLink="true">https://forum.qt.io/post/728959</guid><dc:creator><![CDATA[Christian Ehrlicher]]></dc:creator><pubDate>Fri, 16 Sep 2022 09:42:44 GMT</pubDate></item></channel></rss>