<?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-Problem]]></title><description><![CDATA[<p>Hi Leute!</p>
<p>Ich hab hier einen etwas längeren Code. Ich möchte in der Funktion getSlice() ein Teilwort extrahieren. Leider steh ich da grad etwas auf'm Schlauch. Könnt ihr mir helfen?</p>
<pre><code class="language-cpp">#include&lt;iostream&gt;
using namespace std;

struct bitvector
{
	unsigned char *bits;		//deklaration der Membervariable
};

char *printBitVector(struct bitvector *bv, unsigned int anzahl)
{
	int j = 0;
	char str_ausgabe[1000];

	for(int i=anzahl-1; i&gt;=0; i=i-1)
	{
		if((bool(bv-&gt;bits[i/8] &amp; (1 &lt;&lt; i%8))) == true)
		{
			str_ausgabe[j] = '1';	
		}
		else
		{
			str_ausgabe[j] = '0';
		}
		j = j + 1;
		//cout &lt;&lt; bool(bv-&gt;bits[i/8] &amp; (1 &lt;&lt; i%8));		//gibt die Bits mit Datentyp bool aus
	}
	str_ausgabe[j] = '\0';
	cout &lt;&lt; str_ausgabe &lt;&lt; endl &lt;&lt; endl;

return str_ausgabe;
}

struct bitvector *createBitVector(struct bitvector *bv, unsigned int anzahl)
{
	int var;

	if(anzahl%8 != 0)
	{
		var = (anzahl / 8) + 1;
	}
	else
	{
		var = anzahl / 8;
	}

	bv-&gt;bits = new unsigned char[var];

	for(int i=0; i&lt;var; i=i+1)		//Array wird standardmäßig auf 0 gesetzt!
	{
		bv-&gt;bits[i] = 0;
	}

return bv;
}

void setBit(struct bitvector *bv, unsigned int anzahl, unsigned int stelle)
{
	cout &lt;&lt; &quot;!!!setBit!!!&quot; &lt;&lt; endl;
	cout &lt;&lt; &quot;Stelle eingeben: &quot;;
	cin &gt;&gt; stelle;

	stelle = stelle - 1;	//&quot;richtige&quot; Stellenangabe wieder in einen Index konvertieren

	//Bit setzen an die Stelle von 'stelle'
	bv-&gt;bits[stelle/8] = bv-&gt;bits[stelle/8] | (1 &lt;&lt; stelle%8);			//(1 &lt;&lt; 5) erste Zahl darf nur '1' sein
														//zweite Zahl gibt die zu schreibende Stelle an
														//!VORSICHT! die Wertigkeit bei Binärzahlen beginnt bei 0
}

void resetBit(struct bitvector *bv, unsigned int anzahl, unsigned int stelle)
{
	cout &lt;&lt; &quot;!!!resetBit!!!&quot; &lt;&lt; endl;
	cout &lt;&lt; &quot;Stelle eingeben: &quot;;
	cin &gt;&gt; stelle;

	stelle = stelle - 1;	//&quot;richtige&quot; Stellenangabe wieder in einen Index konvertieren

	bv-&gt;bits[stelle/8] = bv-&gt;bits[stelle/8] &amp; ~(1 &lt;&lt; stelle%8);
}

unsigned int getBit(struct bitvector *bv, unsigned int pos)
{
	return (bv-&gt;bits[pos/8] &gt;&gt; ((pos%8) &amp; 1));
}

struct bitvector *getSlice(struct bitvector *bv, unsigned int anzahl, unsigned int pos, unsigned int n)
{
	bitvector maske;
	int i;

	createBitVector(&amp;maske, anzahl);
	printBitVector(bv, anzahl);

	pos = pos + 1;		//(pos+1) ist die konvertierung zurück von Index nach &quot;richtiger&quot; Stelle

	getBit(bv, pos);

	cout &lt;&lt; &quot;verschoben: &quot;;
	printBitVector(bv, anzahl);

	cout &lt;&lt; &quot;extrahierte Bits: &quot;;
	printBitVector(&amp;maske, n);

return bv;
}

int main()
{
	bitvector bv;		//Variable für struct bitvector
	bitvector maske;
	unsigned int anzahl = 0, stelle = 0, pos = 0, n = 0;

	cout &lt;&lt; &quot;Bitanzahl der Bit-Kette eingeben: &quot;;
	cin &gt;&gt; anzahl;
	cout &lt;&lt; endl;

	createBitVector(&amp;bv, anzahl);		//hier kann nur Adresse übergeben werden, deswegen Adressoperator (&amp;)

	printBitVector(&amp;bv, anzahl);

	for(int i=0; i&lt;3; i=i+1)		//nur für Testzwecke
	{
		setBit(&amp;bv, anzahl, stelle);
		printBitVector(&amp;bv, anzahl);
	}
	resetBit(&amp;bv, anzahl, stelle);
	printBitVector(&amp;bv, anzahl);

	cout &lt;&lt; &quot;Position der zu extrahierenden Bits eingeben: &quot;;
	cin &gt;&gt; pos;
	pos = pos - 1;		//&quot;richtige&quot; Stellenangabe wieder in den Index zur Adressierung konvertieren
	cout &lt;&lt; endl;

	cout &lt;&lt; &quot;Anzahl der zu extrahierenden Bits eingeben: &quot;;
	cin &gt;&gt; n;
	cout &lt;&lt; endl;

	getSlice(&amp;bv, anzahl, pos, n);
	printBitVector(&amp;bv, anzahl);

	delete[](bv.bits);

	return 0;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/topic/286206/bit-problem</link><generator>RSS for Node</generator><lastBuildDate>Fri, 21 Aug 2026 00:50:45 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/286206.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 05 May 2011 15:29:23 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Bit-Problem on Thu, 05 May 2011 15:29:23 GMT]]></title><description><![CDATA[<p>Hi Leute!</p>
<p>Ich hab hier einen etwas längeren Code. Ich möchte in der Funktion getSlice() ein Teilwort extrahieren. Leider steh ich da grad etwas auf'm Schlauch. Könnt ihr mir helfen?</p>
<pre><code class="language-cpp">#include&lt;iostream&gt;
using namespace std;

struct bitvector
{
	unsigned char *bits;		//deklaration der Membervariable
};

char *printBitVector(struct bitvector *bv, unsigned int anzahl)
{
	int j = 0;
	char str_ausgabe[1000];

	for(int i=anzahl-1; i&gt;=0; i=i-1)
	{
		if((bool(bv-&gt;bits[i/8] &amp; (1 &lt;&lt; i%8))) == true)
		{
			str_ausgabe[j] = '1';	
		}
		else
		{
			str_ausgabe[j] = '0';
		}
		j = j + 1;
		//cout &lt;&lt; bool(bv-&gt;bits[i/8] &amp; (1 &lt;&lt; i%8));		//gibt die Bits mit Datentyp bool aus
	}
	str_ausgabe[j] = '\0';
	cout &lt;&lt; str_ausgabe &lt;&lt; endl &lt;&lt; endl;

return str_ausgabe;
}

struct bitvector *createBitVector(struct bitvector *bv, unsigned int anzahl)
{
	int var;

	if(anzahl%8 != 0)
	{
		var = (anzahl / 8) + 1;
	}
	else
	{
		var = anzahl / 8;
	}

	bv-&gt;bits = new unsigned char[var];

	for(int i=0; i&lt;var; i=i+1)		//Array wird standardmäßig auf 0 gesetzt!
	{
		bv-&gt;bits[i] = 0;
	}

return bv;
}

void setBit(struct bitvector *bv, unsigned int anzahl, unsigned int stelle)
{
	cout &lt;&lt; &quot;!!!setBit!!!&quot; &lt;&lt; endl;
	cout &lt;&lt; &quot;Stelle eingeben: &quot;;
	cin &gt;&gt; stelle;

	stelle = stelle - 1;	//&quot;richtige&quot; Stellenangabe wieder in einen Index konvertieren

	//Bit setzen an die Stelle von 'stelle'
	bv-&gt;bits[stelle/8] = bv-&gt;bits[stelle/8] | (1 &lt;&lt; stelle%8);			//(1 &lt;&lt; 5) erste Zahl darf nur '1' sein
														//zweite Zahl gibt die zu schreibende Stelle an
														//!VORSICHT! die Wertigkeit bei Binärzahlen beginnt bei 0
}

void resetBit(struct bitvector *bv, unsigned int anzahl, unsigned int stelle)
{
	cout &lt;&lt; &quot;!!!resetBit!!!&quot; &lt;&lt; endl;
	cout &lt;&lt; &quot;Stelle eingeben: &quot;;
	cin &gt;&gt; stelle;

	stelle = stelle - 1;	//&quot;richtige&quot; Stellenangabe wieder in einen Index konvertieren

	bv-&gt;bits[stelle/8] = bv-&gt;bits[stelle/8] &amp; ~(1 &lt;&lt; stelle%8);
}

unsigned int getBit(struct bitvector *bv, unsigned int pos)
{
	return (bv-&gt;bits[pos/8] &gt;&gt; ((pos%8) &amp; 1));
}

struct bitvector *getSlice(struct bitvector *bv, unsigned int anzahl, unsigned int pos, unsigned int n)
{
	bitvector maske;
	int i;

	createBitVector(&amp;maske, anzahl);
	printBitVector(bv, anzahl);

	pos = pos + 1;		//(pos+1) ist die konvertierung zurück von Index nach &quot;richtiger&quot; Stelle

	getBit(bv, pos);

	cout &lt;&lt; &quot;verschoben: &quot;;
	printBitVector(bv, anzahl);

	cout &lt;&lt; &quot;extrahierte Bits: &quot;;
	printBitVector(&amp;maske, n);

return bv;
}

int main()
{
	bitvector bv;		//Variable für struct bitvector
	bitvector maske;
	unsigned int anzahl = 0, stelle = 0, pos = 0, n = 0;

	cout &lt;&lt; &quot;Bitanzahl der Bit-Kette eingeben: &quot;;
	cin &gt;&gt; anzahl;
	cout &lt;&lt; endl;

	createBitVector(&amp;bv, anzahl);		//hier kann nur Adresse übergeben werden, deswegen Adressoperator (&amp;)

	printBitVector(&amp;bv, anzahl);

	for(int i=0; i&lt;3; i=i+1)		//nur für Testzwecke
	{
		setBit(&amp;bv, anzahl, stelle);
		printBitVector(&amp;bv, anzahl);
	}
	resetBit(&amp;bv, anzahl, stelle);
	printBitVector(&amp;bv, anzahl);

	cout &lt;&lt; &quot;Position der zu extrahierenden Bits eingeben: &quot;;
	cin &gt;&gt; pos;
	pos = pos - 1;		//&quot;richtige&quot; Stellenangabe wieder in den Index zur Adressierung konvertieren
	cout &lt;&lt; endl;

	cout &lt;&lt; &quot;Anzahl der zu extrahierenden Bits eingeben: &quot;;
	cin &gt;&gt; n;
	cout &lt;&lt; endl;

	getSlice(&amp;bv, anzahl, pos, n);
	printBitVector(&amp;bv, anzahl);

	delete[](bv.bits);

	return 0;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2059056</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2059056</guid><dc:creator><![CDATA[parallelmax]]></dc:creator><pubDate>Thu, 05 May 2011 15:29:23 GMT</pubDate></item><item><title><![CDATA[Reply to Bit-Problem on Thu, 05 May 2011 15:33:06 GMT]]></title><description><![CDATA[<p>Störend C-lastiger C++-Code. Altes Buch.</p>
<p>Muß so ein slice wirklich eine Kopie der Ausgangsdaten bereitstellen, oder reicht es, eine Ansicht zu sein?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2059058</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2059058</guid><dc:creator><![CDATA[volkard]]></dc:creator><pubDate>Thu, 05 May 2011 15:33:06 GMT</pubDate></item><item><title><![CDATA[Reply to Bit-Problem on Thu, 05 May 2011 15:35:42 GMT]]></title><description><![CDATA[<p>Ich denke mal es reicht, wenn das extrahierte Teilwort in dem von mir &quot;maske&quot; genannten BitVector steht. Es muss keine Kopie angelegt werden. Das mit dem schlechten C++-Code kann ich leider nicht ändern, da wir das momentan so machen sollen.</p>
<p>Wie soll ich denn nun weiter vorgehen?</p>
<p>Das getBit soll übrigens die einzelnen Bits holen. Aber irgendwie funktioniert das alles nicht so wirklich!</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2059060</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2059060</guid><dc:creator><![CDATA[parallelmax]]></dc:creator><pubDate>Thu, 05 May 2011 15:35:42 GMT</pubDate></item><item><title><![CDATA[Reply to Bit-Problem on Thu, 05 May 2011 15:44:00 GMT]]></title><description><![CDATA[<p>Ich hab übrigens noch ein Problem welches ich nicht zu Lösen vermag.</p>
<p>Wenn ich z.B. bei getBit()</p>
<pre><code class="language-cpp">return (bv-&gt;bits[pos/8] &gt;&gt; ((pos%8) &amp; 1));
</code></pre>
<p>schreibe, dann adressiere ich ja mit &quot;pos/8&quot; das array. Da mein LSB im Array aber an der rechten Stelle steht, adressiere ich quasi mit pos/8 das falsche Element des Arrays. Ich weiß nicht wie ich da eben dann das zweite Element adressieren könnte...</p>
<pre><code>0000001011000000
 |              |
MSB            LSB
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2059065</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2059065</guid><dc:creator><![CDATA[parallelmax]]></dc:creator><pubDate>Thu, 05 May 2011 15:44:00 GMT</pubDate></item><item><title><![CDATA[Reply to Bit-Problem on Fri, 06 May 2011 07:48:25 GMT]]></title><description><![CDATA[<p>BTW: mit C++/CLI hat das überhaupt nichts zu tun <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/27a1.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--right_arrow"
      title=":arrow_right:"
      alt="➡"
    /> Völlig falsches Forum!</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2059360</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2059360</guid><dc:creator><![CDATA[theta]]></dc:creator><pubDate>Fri, 06 May 2011 07:48:25 GMT</pubDate></item><item><title><![CDATA[Reply to Bit-Problem on Fri, 06 May 2011 08:14:09 GMT]]></title><description><![CDATA[<p>Dieser Thread wurde von Moderator/in <a href="http://www.c-plusplus.net/forum/u18363" rel="nofollow">Jochen Kalmbach</a> aus dem Forum <a href="http://www.c-plusplus.net/forum/f58" rel="nofollow">C++/CLI mit .NET</a> in das Forum <a href="http://www.c-plusplus.net/forum/f15" rel="nofollow">C++ (auch C++0x)</a> verschoben.</p>
<p>Im Zweifelsfall bitte auch folgende Hinweise beachten:<br />
<a href="http://www.c-plusplus.net/forum/39405" rel="nofollow">C/C++ Forum :: FAQ - Sonstiges :: Wohin mit meiner Frage?</a></p>
<p><em>Dieses Posting wurde automatisch erzeugt.</em></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2059371</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2059371</guid><dc:creator><![CDATA[C++ Forumbot]]></dc:creator><pubDate>Fri, 06 May 2011 08:14:09 GMT</pubDate></item></channel></rss>