<?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[2 Int in 1 Float umwandeln]]></title><description><![CDATA[<p>Hallöchen,</p>
<p>Ich habe folgendes Problem:<br />
Ich habe 2 Integer gegeben, von denen jeweils die untersten 16 Bit genutzt werden.<br />
Die beiden 16 Bit Werte zusammen ergeben mir meine Binärdarstellung des Float-Wertes.</p>
<p>Wie bekomme ich nun den Compiler dazu diese 32 bit als Float zu interpretieren.</p>
<pre><code class="language-cpp">int var1=0x3f00;
int var2=0x0000;
int binaerdarstellung = (var1&lt;&lt;16)|var2;
// binaerdarstellung = 0x3f000000
</code></pre>
<p>Die Zeile<br />
float ergebnis = (float) binaerdarstellung;<br />
bringt mich nicht zum gewünschten ergebnis von 0,5.</p>
<p>Hat jemand zufälig eine Idee wie man das machen könnte, ausgehend von den 2 Integervariablen?</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/194226/2-int-in-1-float-umwandeln</link><generator>RSS for Node</generator><lastBuildDate>Mon, 28 Sep 2026 09:43:24 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/194226.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 04 Oct 2007 12:00:49 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to 2 Int in 1 Float umwandeln on Thu, 04 Oct 2007 12:00:49 GMT]]></title><description><![CDATA[<p>Hallöchen,</p>
<p>Ich habe folgendes Problem:<br />
Ich habe 2 Integer gegeben, von denen jeweils die untersten 16 Bit genutzt werden.<br />
Die beiden 16 Bit Werte zusammen ergeben mir meine Binärdarstellung des Float-Wertes.</p>
<p>Wie bekomme ich nun den Compiler dazu diese 32 bit als Float zu interpretieren.</p>
<pre><code class="language-cpp">int var1=0x3f00;
int var2=0x0000;
int binaerdarstellung = (var1&lt;&lt;16)|var2;
// binaerdarstellung = 0x3f000000
</code></pre>
<p>Die Zeile<br />
float ergebnis = (float) binaerdarstellung;<br />
bringt mich nicht zum gewünschten ergebnis von 0,5.</p>
<p>Hat jemand zufälig eine Idee wie man das machen könnte, ausgehend von den 2 Integervariablen?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1377801</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1377801</guid><dc:creator><![CDATA[hasibutz]]></dc:creator><pubDate>Thu, 04 Oct 2007 12:00:49 GMT</pubDate></item><item><title><![CDATA[Reply to 2 Int in 1 Float umwandeln on Thu, 04 Oct 2007 12:03:39 GMT]]></title><description><![CDATA[<p>In den int-Werten steht die binäre Repräsentation des float, oder wie? In dem Fall solltest du dir mal reinterpret_cast&lt;&gt; ansehen. (aber besonders portabel ist so ein Ansatz in keinem Fall)</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1377804</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1377804</guid><dc:creator><![CDATA[CStoll]]></dc:creator><pubDate>Thu, 04 Oct 2007 12:03:39 GMT</pubDate></item><item><title><![CDATA[Reply to 2 Int in 1 Float umwandeln on Thu, 04 Oct 2007 12:06:41 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">float f = *(reinterpret_cast&lt;float*&gt;(&amp;binaerdarstellung));
</code></pre>
<p>Aber wie gesagt, unportabel!</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1377808</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1377808</guid><dc:creator><![CDATA[TomasRiker]]></dc:creator><pubDate>Thu, 04 Oct 2007 12:06:41 GMT</pubDate></item><item><title><![CDATA[Reply to 2 Int in 1 Float umwandeln on Thu, 04 Oct 2007 13:21:13 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">int var1=0x3f00;
int var2=0x0000;
int binaerdarstellung = (var1&lt;&lt;16)|var2;
// binaerdarstellung = 0x3f000000

float f;
// entweder
memcpy(&amp;f,&amp;binaerdarstellung,sizeof f);
</code></pre>
<p>Das ist portabel unter den gegebenen Voraussetzungen. Endianess spielt keine Rolle, da sie für Quelle und Ziel logischerweise gleich sein muss. Gewöhnt euch ab, überhaupt erst an reinterpret_cast zu denken, wenn schon auf Portabilität hingewiesen werden soll.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1377882</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1377882</guid><dc:creator><![CDATA[camper]]></dc:creator><pubDate>Thu, 04 Oct 2007 13:21:13 GMT</pubDate></item><item><title><![CDATA[Reply to 2 Int in 1 Float umwandeln on Thu, 04 Oct 2007 13:23:38 GMT]]></title><description><![CDATA[<p>camper schrieb:</p>
<blockquote>
<p>Gewöhnt euch ab, überhaupt erst an reinterpret_cast zu denken, wenn schon auf Portabilität hingewiesen werden soll.</p>
</blockquote>
<p>memcpy() ist genauso gut oder schlecht wie der ansatz über reinterpret_cast&lt;&gt;.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1377886</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1377886</guid><dc:creator><![CDATA[CStoll]]></dc:creator><pubDate>Thu, 04 Oct 2007 13:23:38 GMT</pubDate></item><item><title><![CDATA[Reply to 2 Int in 1 Float umwandeln on Thu, 04 Oct 2007 13:47:00 GMT]]></title><description><![CDATA[<p>CStoll schrieb:</p>
<blockquote>
<p>camper schrieb:</p>
<blockquote>
<p>Gewöhnt euch ab, überhaupt erst an reinterpret_cast zu denken, wenn schon auf Portabilität hingewiesen werden soll.</p>
</blockquote>
<p>memcpy() ist genauso gut oder schlecht wie der ansatz über reinterpret_cast&lt;&gt;.</p>
</blockquote>
<p>keineswegs. Die Probleme gehen bei alignment los und hören bei Verletzung der Alias-Regeln nicht auf. Im Gegensatz dazu begnügt sich memcpy mit Fragen der Repräsentation - die war aber schon gegeben.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1377909</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1377909</guid><dc:creator><![CDATA[camper]]></dc:creator><pubDate>Thu, 04 Oct 2007 13:47:00 GMT</pubDate></item></channel></rss>