<?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[char array in Bitfield struct?]]></title><description><![CDATA[<p>Hiho,</p>
<p>ich habe einen char array welchen ich in ein Bitfield struct casten möchte. Problem ist aber das wenn ich mehr als 8 Bit vom char array nehmen möchte, geht es einfach nicht.</p>
<pre><code>#pragma pack(1)
struct SENSOR {
	short x1 : 10;
	short y1 : 10;
	short x2 : 10;
	short y2 : 10;
}

unsigned char data[] = { 0x0B, 0xCE, 0x90, 0xD0, 0xEE, 0x88 };
// 0x0B      0xCE       0x90       0xD0       0xEE      0x88
// 1011      11001110   10010000   11010000   11101110  10001000
// [00001011 11][001110 1001][0000 110100][00 11101110] 10001000
//     = 47         = 233        = 52          = 238

auto sensor = reinterpret_cast&lt;SENSOR*&gt;(data);
</code></pre>
<p>Leider enthält sensor nicht die richtigen Daten (die als Kommentar gezeigten Daten sind das was eigentlich raus kommen soll. Wenn ich testweise immer nur 8 bit lesen, kommt binär genau das ras was auch drin steht.</p>
<p>Spoocy</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/333912/char-array-in-bitfield-struct</link><generator>RSS for Node</generator><lastBuildDate>Sun, 26 Apr 2026 05:28:34 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/333912.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 10 Aug 2015 11:21:24 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to char array in Bitfield struct? on Mon, 10 Aug 2015 11:21:24 GMT]]></title><description><![CDATA[<p>Hiho,</p>
<p>ich habe einen char array welchen ich in ein Bitfield struct casten möchte. Problem ist aber das wenn ich mehr als 8 Bit vom char array nehmen möchte, geht es einfach nicht.</p>
<pre><code>#pragma pack(1)
struct SENSOR {
	short x1 : 10;
	short y1 : 10;
	short x2 : 10;
	short y2 : 10;
}

unsigned char data[] = { 0x0B, 0xCE, 0x90, 0xD0, 0xEE, 0x88 };
// 0x0B      0xCE       0x90       0xD0       0xEE      0x88
// 1011      11001110   10010000   11010000   11101110  10001000
// [00001011 11][001110 1001][0000 110100][00 11101110] 10001000
//     = 47         = 233        = 52          = 238

auto sensor = reinterpret_cast&lt;SENSOR*&gt;(data);
</code></pre>
<p>Leider enthält sensor nicht die richtigen Daten (die als Kommentar gezeigten Daten sind das was eigentlich raus kommen soll. Wenn ich testweise immer nur 8 bit lesen, kommt binär genau das ras was auch drin steht.</p>
<p>Spoocy</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2463247</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2463247</guid><dc:creator><![CDATA[MrSpoocy]]></dc:creator><pubDate>Mon, 10 Aug 2015 11:21:24 GMT</pubDate></item><item><title><![CDATA[Reply to char array in Bitfield struct? on Mon, 10 Aug 2015 11:44:02 GMT]]></title><description><![CDATA[<p>Das <code>pack</code> brauchste nicht. Der Standard garantiert dass <code>x1</code> das erste Byte belegt und das alle Bitfields in der selben allocation unit residieren.<br />
Edit: Ahh, <code>pack</code> macht die besagte unit tatsächlich kleiner. Ändert aber ja nichts am Resultat.</p>
<p>Allerdings machst du hier eine ungerechtfertigte Annahme: Die Bits der Member liegen alle hintereinander und in der Reihenfolge der Deklaration. Das ist jedoch wahrscheinlich nicht der Fall. Beachte die Notiz:</p>
<p>§9.6/1 schrieb:</p>
<blockquote>
<p>[ <em>Note</em>: Bit-fields straddle allocation units on some machines and not on others. Bit-fields are assigned right-to-left on some machines, left-to-right on others. — <em>end note</em> ]</p>
</blockquote>
]]></description><link>https://www.c-plusplus.net/forum/post/2463252</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2463252</guid><dc:creator><![CDATA[Columbo]]></dc:creator><pubDate>Mon, 10 Aug 2015 11:44:02 GMT</pubDate></item><item><title><![CDATA[Reply to char array in Bitfield struct? on Mon, 10 Aug 2015 12:51:52 GMT]]></title><description><![CDATA[<p>Dieses Beispiel zeigt auf wie die Bitfields verteilt sind:</p>
<pre><code>struct &lsqb;&lsqb;gnu::packed&rsqb;&rsqb; SENSOR {
	short x1 : 10;
	short y1 : 10;
	short x2 : 10;
	short y2 : 10;
};

#include &lt;iostream&gt;
#include &lt;bitset&gt;
int main() {
	SENSOR s{0b1111111111, 0, 0b1100110011, 0b0101010101};
	for (auto c : reinterpret_cast&lt;unsigned char(&amp;)[sizeof s]&gt;(s))
		std::cout &lt;&lt; std::bitset&lt;8&gt;(c) &lt;&lt; ' ';
}
</code></pre>
<p>Du musst was eigenes Basteln um das gewünschte Verhalten zu bekommen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2463265</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2463265</guid><dc:creator><![CDATA[Columbo]]></dc:creator><pubDate>Mon, 10 Aug 2015 12:51:52 GMT</pubDate></item><item><title><![CDATA[Reply to char array in Bitfield struct? on Mon, 10 Aug 2015 14:19:04 GMT]]></title><description><![CDATA[<p>Warum muss es per casten passieren? Ist das ein Performance Problem? Wenn ja bedenke, das du (wuerde das casten funktionieren) das Performance Problem dann beim Lesen aus der Struktur haettest.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2463279</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2463279</guid><dc:creator><![CDATA[TGGC]]></dc:creator><pubDate>Mon, 10 Aug 2015 14:19:04 GMT</pubDate></item></channel></rss>