<?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 Fields in Structures(Erklärung)]]></title><description><![CDATA[<p>Hallo,<br />
wie oben in Titel schon steht geht es um Bit Fields. Das Thema kam grade im Buch was ich lese(C++ Primer Plus 6th edition) und versteh das gar nicht was genau das bringt.<br />
Da sonst immer im Buch steht wenn ein Thema später genau erklärt wird und hier nicht der Fall ist habe ich mich halt dazu entschlossen hier zu fragen. Hoffe mir kann da jemand weiter helfen.<br />
Unten noch mal der Absatz aus den Buch.</p>
<blockquote>
<p>Bit Fields in Structures<br />
C++, like C, enables you to specify structure members that occupy a particular number<br />
of bits.This can be handy for creating a data structure that corresponds, say, to a register<br />
on some hardware device.The field type should be an integral or enumeration type (enumerations<br />
are discussed later in this chapter), and a colon followed by a number indicates<br />
the actual number of bits to be used.You can use unnamed fields to provide spacing. Each<br />
member is termed a bit field. Here’s an example:<br />
struct torgle_register<br />
{<br />
unsigned int SN : 4; // 4 bits for SN value<br />
unsigned int : 4; // 4 bits unused<br />
bool goodIn : 1; // valid input (1 bit)<br />
bool goodTorgle : 1; // successful torgling<br />
};<br />
You can initialize the fields in the usual manner, and you use standard structure notation<br />
to access bit fields:<br />
torgle_register tr = { 14, true, false };<br />
...<br />
if (tr.goodIn) // if statement covered in Chapter 6<br />
...<br />
Bit fields are typically used in low-level programming. Often, using an integral type<br />
and the bitwise operators listed in Appendix E,“Other Operators,” provides an alternative<br />
approach.</p>
</blockquote>
<p>Quelle: C++ Primer Plus 6th edition by Stephen Prata</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/317439/bit-fields-in-structures-erklärung</link><generator>RSS for Node</generator><lastBuildDate>Tue, 28 Jul 2026 20:09:28 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/317439.rss" rel="self" type="application/rss+xml"/><pubDate>Sat, 08 Jun 2013 17:49:05 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Bit Fields in Structures(Erklärung) on Sat, 08 Jun 2013 17:49:05 GMT]]></title><description><![CDATA[<p>Hallo,<br />
wie oben in Titel schon steht geht es um Bit Fields. Das Thema kam grade im Buch was ich lese(C++ Primer Plus 6th edition) und versteh das gar nicht was genau das bringt.<br />
Da sonst immer im Buch steht wenn ein Thema später genau erklärt wird und hier nicht der Fall ist habe ich mich halt dazu entschlossen hier zu fragen. Hoffe mir kann da jemand weiter helfen.<br />
Unten noch mal der Absatz aus den Buch.</p>
<blockquote>
<p>Bit Fields in Structures<br />
C++, like C, enables you to specify structure members that occupy a particular number<br />
of bits.This can be handy for creating a data structure that corresponds, say, to a register<br />
on some hardware device.The field type should be an integral or enumeration type (enumerations<br />
are discussed later in this chapter), and a colon followed by a number indicates<br />
the actual number of bits to be used.You can use unnamed fields to provide spacing. Each<br />
member is termed a bit field. Here’s an example:<br />
struct torgle_register<br />
{<br />
unsigned int SN : 4; // 4 bits for SN value<br />
unsigned int : 4; // 4 bits unused<br />
bool goodIn : 1; // valid input (1 bit)<br />
bool goodTorgle : 1; // successful torgling<br />
};<br />
You can initialize the fields in the usual manner, and you use standard structure notation<br />
to access bit fields:<br />
torgle_register tr = { 14, true, false };<br />
...<br />
if (tr.goodIn) // if statement covered in Chapter 6<br />
...<br />
Bit fields are typically used in low-level programming. Often, using an integral type<br />
and the bitwise operators listed in Appendix E,“Other Operators,” provides an alternative<br />
approach.</p>
</blockquote>
<p>Quelle: C++ Primer Plus 6th edition by Stephen Prata</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2329435</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2329435</guid><dc:creator><![CDATA[CraftPlorer]]></dc:creator><pubDate>Sat, 08 Jun 2013 17:49:05 GMT</pubDate></item><item><title><![CDATA[Reply to Bit Fields in Structures(Erklärung) on Sat, 08 Jun 2013 18:27:58 GMT]]></title><description><![CDATA[<p>Das ist eine sehr spezielle hardwarenahe Optimierung (hauptsächlich zu Gunsten von Speicherplatz, was sich dann je nach Situation negativ oder positiv auf andere Aspekte auswirken kann), die du wahrscheinlich niemals im Leben benötigen wirst. Falls du doch mal in die Situation kommst, sie zu benötigen, dann wirst du bis dahin voraussichtlich schon ganz alleine wissen, ob und warum du das brauchen könntest.</p>
<p>Behalte bloß im Hinterkopf, dass es das gibt. Probier es einmal selber aus. Die Hauptlektion, die du hier lernen wirst ist, dass du später nicht so überrascht bist wie andere Leute, wenn sie das erste Mal einen Doppelpunkt in fremden Code sehen und nicht wissen, was das überhaupt ist.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2329438</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2329438</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Sat, 08 Jun 2013 18:27:58 GMT</pubDate></item><item><title><![CDATA[Reply to Bit Fields in Structures(Erklärung) on Sat, 08 Jun 2013 18:28:18 GMT]]></title><description><![CDATA[<p>Wie bereits erkennbar ist es ein guter Weg, Speicher zu sparen. Ein Boolean brauch nicht mehr als 1 Bit, Zahlen können auf einen kleineren, möglichen Bereich beschränkt werden.<br />
Persönlich habe ich es noch nie verwenden müssen, aber für Programmierer von Microcontrollern, dessen Speicher doch stark beschränkt ist, kann es äußert hilfreich sein, keine Bits zu verschwenden.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2329439</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2329439</guid><dc:creator><![CDATA[Youka]]></dc:creator><pubDate>Sat, 08 Jun 2013 18:28:18 GMT</pubDate></item><item><title><![CDATA[Reply to Bit Fields in Structures(Erklärung) on Sat, 08 Jun 2013 18:31:26 GMT]]></title><description><![CDATA[<p>CraftPlorer schrieb:</p>
<blockquote>
<p>[...] und [ich] versteh das gar nicht was genau das bringt.</p>
</blockquote>
<blockquote>
<p>This can be handy for creating a data structure that corresponds, say, to a register on some hardware device.</p>
</blockquote>
<p>Man kann damit auch Flags abbilden, aber meist benutzt man Alternativen. Weiterhin kann man damit ein paar Bits sparen. Es wird allerdings eher selten benutzt.</p>
<p>Sowas in der Richtung geht damit auch:</p>
<pre><code>union Float{
 float f;
 struct{
  bool vorzeichen : 1;
  unsigned int mantisse : 23;
  unsigned char exponent : 8;
 };
};
</code></pre>
<p>Das Problem dabei war aber, dass der Compiler die Reihenfolge drehen darf, sodass das je nach Optimierung, Compiler und Plattform mal funktioniert und mal nicht.</p>
<p>//zu lahm -.-<br />
//irgendwer wird sicher meine float-union in der Luft zerreißen <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f603.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--grinning_face_with_big_eyes"
      title=":D"
      alt="😃"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2329440</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2329440</guid><dc:creator><![CDATA[nwp3]]></dc:creator><pubDate>Sat, 08 Jun 2013 18:31:26 GMT</pubDate></item><item><title><![CDATA[Reply to Bit Fields in Structures(Erklärung) on Sat, 08 Jun 2013 19:00:59 GMT]]></title><description><![CDATA[<p>Vielen Dank!</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2329442</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2329442</guid><dc:creator><![CDATA[CraftPlorer]]></dc:creator><pubDate>Sat, 08 Jun 2013 19:00:59 GMT</pubDate></item><item><title><![CDATA[Reply to Bit Fields in Structures(Erklärung) on Thu, 12 Jun 2014 00:01:30 GMT]]></title><description><![CDATA[<p>...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2329451</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2329451</guid><dc:creator><![CDATA[Swordfish]]></dc:creator><pubDate>Thu, 12 Jun 2014 00:01:30 GMT</pubDate></item></channel></rss>