<?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[bit shifting]]></title><description><![CDATA[<p>hi<br />
hätte mal eine frage zum bit shifting. und zwar habe ich vier werte:<br />
0x0 - 0x0 0x10 0x40<br />
Diese sind in einem char-array definiert und sollen jetzt in einen float konvertiert werden. nach meiner überlegung sollte es so aussehen (little endian):</p>
<pre><code class="language-cpp">float wert = { 0x0 &lt;&lt; 4 | 0x0 &lt;&lt; 8 | 0x10 &lt;&lt; 16 | 0x40 &lt;&lt; 24 };
</code></pre>
<p>das ergebnis muss genau 1 sein... kann mir einer erklären was ich falsch mache und warum? stehe etwas auf dem schlauch</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/243944/bit-shifting</link><generator>RSS for Node</generator><lastBuildDate>Sat, 19 Sep 2026 14:47:51 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/243944.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 23 Jun 2009 10:09:55 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to bit shifting on Tue, 23 Jun 2009 10:09:55 GMT]]></title><description><![CDATA[<p>hi<br />
hätte mal eine frage zum bit shifting. und zwar habe ich vier werte:<br />
0x0 - 0x0 0x10 0x40<br />
Diese sind in einem char-array definiert und sollen jetzt in einen float konvertiert werden. nach meiner überlegung sollte es so aussehen (little endian):</p>
<pre><code class="language-cpp">float wert = { 0x0 &lt;&lt; 4 | 0x0 &lt;&lt; 8 | 0x10 &lt;&lt; 16 | 0x40 &lt;&lt; 24 };
</code></pre>
<p>das ergebnis muss genau 1 sein... kann mir einer erklären was ich falsch mache und warum? stehe etwas auf dem schlauch</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1731486</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1731486</guid><dc:creator><![CDATA[fkrauthahn]]></dc:creator><pubDate>Tue, 23 Jun 2009 10:09:55 GMT</pubDate></item><item><title><![CDATA[Reply to bit shifting on Tue, 23 Jun 2009 11:03:03 GMT]]></title><description><![CDATA[<p>Das hier:</p>
<pre><code>{ 0x0 &lt;&lt; 4 | 0x0 &lt;&lt; 8 | 0x10 &lt;&lt; 16 | 0x40 &lt;&lt; 24 }
</code></pre>
<p>ist ein (unsigned) integer. Und mit den Bits, die Du da setzt, kenne ich keine Darstellung, die da eine 1 ergeben würde.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1731522</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1731522</guid><dc:creator><![CDATA[jencas]]></dc:creator><pubDate>Tue, 23 Jun 2009 11:03:03 GMT</pubDate></item><item><title><![CDATA[Reply to bit shifting on Tue, 23 Jun 2009 11:22:23 GMT]]></title><description><![CDATA[<p>fkrauthahn schrieb:</p>
<blockquote>
<pre><code class="language-cpp">float wert = { 0x0 &lt;&lt; 4 | 0x0 &lt;&lt; 8 | 0x10 &lt;&lt; 16 | 0x40 &lt;&lt; 24 };
</code></pre>
</blockquote>
<p>Das <code>&lt;&lt; 4</code> sieht da nicht ganz richtig aus. Abgesehen davon bekommst Du einen float, der der Ganzzahl (0x40100000) entspricht und nicht 1.</p>
<p>Probier mal folgendes:</p>
<pre><code class="language-cpp">#include&lt;iostream&gt;

#include&lt;string.h&gt;
#include&lt;stdint.h&gt;

int main()
{
    uint32_t i = 0x3F800000;
    float f;
    memcpy(&amp;f,&amp;i,sizeof(f));
    std::cout &lt;&lt; f &lt;&lt; '\n';
}
</code></pre>
<p>Warnung: Das Verhalten ist &quot;implementation defined&quot;. Es funktioniert nicht auf jeder Maschine. Der C++ Standard schreibt nicht vor, dass &quot;float&quot; die IEEE 754 32bit Fließkommazahl-Repräsentierung benutzen muss. Außerdem, könnte es sein, dass die Byteordnung zwischen Ganzzahlen und Fließkommazahlen inkompatibel ist.</p>
<p>Um Dich diesbezüglich abzusichern, kannst Du noch Unit-Tests schreiben und/oder einige der C++-Implementierungseigenschaften über die Header &lt;climits&gt; und <a href="http://www.cplusplus.com/reference/std/limits/" rel="nofollow">&lt;limits&gt;</a> (std::numeric_limits&lt;&gt;) erfragen.</p>
<p>Gruß,<br />
SP</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1731535</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1731535</guid><dc:creator><![CDATA[Sebastian Pizer]]></dc:creator><pubDate>Tue, 23 Jun 2009 11:22:23 GMT</pubDate></item><item><title><![CDATA[Reply to bit shifting on Tue, 23 Jun 2009 15:13:17 GMT]]></title><description><![CDATA[<p>hi,<br />
sorry, 0x1040 war der falsche wert, es muss 0x803F0000 sein. ich, frkauthahn bin halt etwas sehr doof.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1731650</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1731650</guid><dc:creator><![CDATA[frkauthahn]]></dc:creator><pubDate>Tue, 23 Jun 2009 15:13:17 GMT</pubDate></item><item><title><![CDATA[Reply to bit shifting on Wed, 24 Jun 2009 06:08:02 GMT]]></title><description><![CDATA[<p>das problem ist halt, dass ich die daten in einem char-array habe. außerdem funzt der code von dir nicht, kommt 1.#QNA oder so raus.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1731907</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1731907</guid><dc:creator><![CDATA[fkrauthahn]]></dc:creator><pubDate>Wed, 24 Jun 2009 06:08:02 GMT</pubDate></item><item><title><![CDATA[Reply to bit shifting on Wed, 24 Jun 2009 07:22:39 GMT]]></title><description><![CDATA[<p>fkrauthahn schrieb:</p>
<blockquote>
<p>das problem ist halt, dass ich die daten in einem char-array habe.</p>
</blockquote>
<p>In welchem Format denn? Im nativen Format Deiner Maschine? Du kannst natürlich per memcpy ein float zu char[sizeof(float)] kopieren und wieder zurück. Das ist garantiert lauffähig und verlustfrei, da eines von beiden ein char array ist und beide Typen Skalare sind. Du darfst Dich aber nicht darauf verlassen, dass wenn Du ein float auf einer Maschine zu char[] konvertierst und auf einer anderen wieder zu float konvertierst, dass da noch das gleiche herauskommt.</p>
<p>fkrauthahn schrieb:</p>
<blockquote>
<p>außerdem funzt der code von dir nicht, kommt 1.#QNA oder so raus.</p>
</blockquote>
<p>Bei <em>mir</em> geht es (EMT64T-Architektur im 32bit Modus unter Linux, sizeof(uint32_t)==sizeof(float), std::numeric_limits&lt;float&gt;::is_iec559==true).</p>
<p>Gruß,<br />
SP</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1731935</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1731935</guid><dc:creator><![CDATA[Sebastian Pizer]]></dc:creator><pubDate>Wed, 24 Jun 2009 07:22:39 GMT</pubDate></item><item><title><![CDATA[Reply to bit shifting on Wed, 24 Jun 2009 07:41:07 GMT]]></title><description><![CDATA[<p>ok, danke danke. ich habe btw 64 bit vista...</p>
<blockquote>
<p>In welchem Format denn?</p>
</blockquote>
<p>? ich habe die dezimalwerte von den hex-werten. bzw ist der char ein buffer, in den der komplette inhalt einer binären datei kommt.</p>
<blockquote>
<p>Im nativen Format Deiner Maschine?</p>
</blockquote>
<p>ja.</p>
<blockquote>
<p>Du darfst Dich aber nicht darauf verlassen,...</p>
</blockquote>
<p>ist da die little- bigendian formatierung mit inbegriffen?</p>
<p>ich versuche das mal mit dem memcpy.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1731946</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1731946</guid><dc:creator><![CDATA[fkrauthahn]]></dc:creator><pubDate>Wed, 24 Jun 2009 07:41:07 GMT</pubDate></item><item><title><![CDATA[Reply to bit shifting on Wed, 24 Jun 2009 07:57:55 GMT]]></title><description><![CDATA[<p>fkrauthahn schrieb:</p>
<blockquote>
<pre><code class="language-cpp">0x0 &lt;&lt; 4 | 0x0 &lt;&lt; 8
</code></pre>
</blockquote>
<p>0 um 4 oder 8 oder irgendwas zu shiften macht nicht wirklich Sinn.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1731951</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1731951</guid><dc:creator><![CDATA[EOP]]></dc:creator><pubDate>Wed, 24 Jun 2009 07:57:55 GMT</pubDate></item><item><title><![CDATA[Reply to bit shifting on Wed, 24 Jun 2009 10:59:30 GMT]]></title><description><![CDATA[<p>Mal abgesehen davon sind die 4 unteren Bits immer 0. Ob das so richtig ist?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1732047</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1732047</guid><dc:creator><![CDATA[jencas]]></dc:creator><pubDate>Wed, 24 Jun 2009 10:59:30 GMT</pubDate></item></channel></rss>