<?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[Bitwise OR]]></title><description><![CDATA[<p>Ich steh auf dem Schlauch...</p>
<p>Ich lese in einem Consolenprogramm den Sectiontable von PE-files (executables) aus. In jedem dieser Sectiontables befindet sich ein Feld names 'Characteristics'.<br />
Dieses Feld möchte ich auf Ausführungs-, Lese- und Schreibrechte überprüfen. Ist das jeweilige Recht vorhanden sollen dazu die Buchstaben...<br />
<strong>x</strong> für execute<br />
<strong>r</strong> für read<br />
<strong>w</strong> für write<br />
...ausgegeben werden.</p>
<p>Nun ist dieses Feld über ein bitweises ODER (bitwise OR) abgespeichert. &quot;|&quot; entspricht bitwise OR in C++. z.B.:</p>
<pre><code class="language-cpp">long characteristic = IMAGE_SCN_MEM_EXECUTE | IMAGE_SCN_MEM_READ; // Diese Sektion hätte Ausführungs- und Leserechte.
</code></pre>
<p>kleiner Auszug aus (Es sind eigentlich viel mehr Werte):<br />
&quot;Microsoft Portable Executable and Common Object File Format Specification&quot;</p>
<blockquote>
<p>IMAGE_SCN_MEM_EXECUTE 0x20000000 The section can be executed as code.<br />
IMAGE_SCN_MEM_READ 0x40000000 The section can be read.<br />
IMAGE_SCN_MEM_WRITE 0x80000000 The section can be written to.</p>
</blockquote>
<p>Was ich bräuchte ist ca. so eine Funktion. Was müsste in die 'If's rein?:</p>
<pre><code class="language-cpp">void GetSectionCharacteristic(long characteristic)
{
	std::string tmp;

	if ( /*characteristic has execute rights*/ )
		tmp.append(&quot;x&quot;);
	std::cout &lt;&lt; std::hex &lt;&lt; &quot;0x&quot; &lt;&lt; characteristic &lt;&lt; std::endl;

	if ( /*characteristic has read rights*/ )
		tmp.append(&quot;r&quot;);
	std::cout &lt;&lt; std::hex &lt;&lt; &quot;0x&quot; &lt;&lt; characteristic &lt;&lt; std::endl;

	if ( /*characteristic has write rights*/ )
		tmp.append(&quot;w&quot;);
	std::cout &lt;&lt; std::hex &lt;&lt; &quot;0x&quot; &lt;&lt; characteristic &lt;&lt; std::endl;

	std::cout &lt;&lt; tmp.c_str() &lt;&lt; std::endl;
}
</code></pre>
<p>Mir ist aufgefallen das Microsoft in PE-files sehr oft 'bitwiso OR' verwendet, scheint ja auch eine sehr effektive Methode zu sein Möglichst viele Informationen auf kleinem Raum zu speichern. Nur wie man das wieder auswertet ist mir unklar.</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/225940/bitwise-or</link><generator>RSS for Node</generator><lastBuildDate>Thu, 01 Oct 2026 06:06:08 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/225940.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 27 Oct 2008 11:25:10 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Bitwise OR on Mon, 27 Oct 2008 11:25:10 GMT]]></title><description><![CDATA[<p>Ich steh auf dem Schlauch...</p>
<p>Ich lese in einem Consolenprogramm den Sectiontable von PE-files (executables) aus. In jedem dieser Sectiontables befindet sich ein Feld names 'Characteristics'.<br />
Dieses Feld möchte ich auf Ausführungs-, Lese- und Schreibrechte überprüfen. Ist das jeweilige Recht vorhanden sollen dazu die Buchstaben...<br />
<strong>x</strong> für execute<br />
<strong>r</strong> für read<br />
<strong>w</strong> für write<br />
...ausgegeben werden.</p>
<p>Nun ist dieses Feld über ein bitweises ODER (bitwise OR) abgespeichert. &quot;|&quot; entspricht bitwise OR in C++. z.B.:</p>
<pre><code class="language-cpp">long characteristic = IMAGE_SCN_MEM_EXECUTE | IMAGE_SCN_MEM_READ; // Diese Sektion hätte Ausführungs- und Leserechte.
</code></pre>
<p>kleiner Auszug aus (Es sind eigentlich viel mehr Werte):<br />
&quot;Microsoft Portable Executable and Common Object File Format Specification&quot;</p>
<blockquote>
<p>IMAGE_SCN_MEM_EXECUTE 0x20000000 The section can be executed as code.<br />
IMAGE_SCN_MEM_READ 0x40000000 The section can be read.<br />
IMAGE_SCN_MEM_WRITE 0x80000000 The section can be written to.</p>
</blockquote>
<p>Was ich bräuchte ist ca. so eine Funktion. Was müsste in die 'If's rein?:</p>
<pre><code class="language-cpp">void GetSectionCharacteristic(long characteristic)
{
	std::string tmp;

	if ( /*characteristic has execute rights*/ )
		tmp.append(&quot;x&quot;);
	std::cout &lt;&lt; std::hex &lt;&lt; &quot;0x&quot; &lt;&lt; characteristic &lt;&lt; std::endl;

	if ( /*characteristic has read rights*/ )
		tmp.append(&quot;r&quot;);
	std::cout &lt;&lt; std::hex &lt;&lt; &quot;0x&quot; &lt;&lt; characteristic &lt;&lt; std::endl;

	if ( /*characteristic has write rights*/ )
		tmp.append(&quot;w&quot;);
	std::cout &lt;&lt; std::hex &lt;&lt; &quot;0x&quot; &lt;&lt; characteristic &lt;&lt; std::endl;

	std::cout &lt;&lt; tmp.c_str() &lt;&lt; std::endl;
}
</code></pre>
<p>Mir ist aufgefallen das Microsoft in PE-files sehr oft 'bitwiso OR' verwendet, scheint ja auch eine sehr effektive Methode zu sein Möglichst viele Informationen auf kleinem Raum zu speichern. Nur wie man das wieder auswertet ist mir unklar.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1605452</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1605452</guid><dc:creator><![CDATA[Spekulatius]]></dc:creator><pubDate>Mon, 27 Oct 2008 11:25:10 GMT</pubDate></item><item><title><![CDATA[Reply to Bitwise OR on Mon, 27 Oct 2008 11:33:51 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">if(characteristic &amp; IMAGE_SCN_MEM_EXECUTE == IMAGE_SCN_MEM_EXECUTE) {
	cout&lt;&lt;&quot;ausführbar&quot;&lt;&lt;endl;
}
</code></pre>
<p>Die beiden anderen Rechte können analog dazu geprüft werden.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1605457</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1605457</guid><dc:creator><![CDATA[moagnus]]></dc:creator><pubDate>Mon, 27 Oct 2008 11:33:51 GMT</pubDate></item><item><title><![CDATA[Reply to Bitwise OR on Mon, 27 Oct 2008 11:35:47 GMT]]></title><description><![CDATA[<p>Spekulatius schrieb:</p>
<blockquote>
<p>Nun ist dieses Feld über ein bitweises ODER (bitwise OR) abgespeichert. &quot;|&quot; entspricht bitwise OR in C++. z.B.:</p>
</blockquote>
<p>Danke, das weiss ich auch ... *SCNR*</p>
<p>Ehm zu deinem Problem. Einfach das bitwise AND anwenden:</p>
<pre><code class="language-cpp">if(characteristic &amp; IMAGE_SCN_MEM_EXECUTE) // Ergibt 0 oder eine Zahl
{
  // Dann ist IMAGE_SCN_MEM_EXECUTE gesetzt.
}
</code></pre>
<p>Sowas nennt man übrigens Flags, falls du mal danach suchen möchtest <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f609.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--winking_face"
      title=";)"
      alt="😉"
    /></p>
<p>Grüssli</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1605460</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1605460</guid><dc:creator><![CDATA[Dravere]]></dc:creator><pubDate>Mon, 27 Oct 2008 11:35:47 GMT</pubDate></item><item><title><![CDATA[Reply to Bitwise OR on Mon, 27 Oct 2008 11:45:52 GMT]]></title><description><![CDATA[<p>Etwas flexibler geht es z.B. auch so:</p>
<pre><code class="language-cpp">std::string GetSectionCharacteristic(long characteristic)
{
    //alle moeglichen flags
    const long characteristics_map[] =
    {
        IMAGE_SCN_MEM_EXECUTE,
        IMAGE_SCN_MEM_READ,
        IMAGE_SCN_MEM_WRITE
        //mehr faelle
    };

    std::string tmp;
    //alle flags durchgehen...
    for(int n = 0; n != (sizeof(characteristics_map) / sizeof(*characteristics_map)); ++n)
    {
        long masked = characteristic &amp; characteristics_map[n];
        switch(masked)
        {
        case IMAGE_SCN_MEM_EXECUTE:
            tmp.append(&quot;x&quot;);
            break;
        case IMAGE_SCN_MEM_READ:
            tmp.append(&quot;r&quot;);
            break;
        case IMAGE_SCN_MEM_WRITE:
            tmp.append(&quot;w&quot;);
            break;
        //case...
        default:
            //irgenwas passt nicht
            break;
        }
    }
    return tmp;
}
</code></pre>
<p>Oder noch einfacher mit <code>std::map</code> .</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1605462</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1605462</guid><dc:creator><![CDATA[Tachyon]]></dc:creator><pubDate>Mon, 27 Oct 2008 11:45:52 GMT</pubDate></item><item><title><![CDATA[Reply to Bitwise OR on Mon, 27 Oct 2008 11:42:44 GMT]]></title><description><![CDATA[<p>moagnus schrieb:</p>
<blockquote>
<pre><code class="language-cpp">if(characteristic &amp; IMAGE_SCN_MEM_EXECUTE == IMAGE_SCN_MEM_EXECUTE) {
	cout&lt;&lt;&quot;ausführbar&quot;&lt;&lt;endl;
}
</code></pre>
<p>Die beiden anderen Rechte können analog dazu geprüft werden.</p>
</blockquote>
<p>Das dürfte nicht funktionieren, denn die Precedence von == liegt höher als von &amp; Also:</p>
<pre><code class="language-cpp">if(characteristic &amp; (IMAGE_SCN_MEM_EXECUTE == IMAGE_SCN_MEM_EXECUTE)) {
	cout&lt;&lt;&quot;ausführbar&quot;&lt;&lt;endl;
}
</code></pre>
<p>Dann liefert Dein Ausdruck nur true, wenn in characteristic das niedrigste Bit gesetzt hat!</p>
<p>Korrekt wäre</p>
<pre><code class="language-cpp">if((characteristic &amp; IMAGE_SCN_MEM_EXECUTE) == IMAGE_SCN_MEM_EXECUTE) {
	cout&lt;&lt;&quot;ausführbar&quot;&lt;&lt;endl;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1605463</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1605463</guid><dc:creator><![CDATA[Martin Richter]]></dc:creator><pubDate>Mon, 27 Oct 2008 11:42:44 GMT</pubDate></item><item><title><![CDATA[Reply to Bitwise OR on Mon, 27 Oct 2008 18:21:11 GMT]]></title><description><![CDATA[<p>Der STL-Container <code>std::bitset</code> kann auch zur konfortablen, speicherschonenden Verwaltung von Bitmasken verwendet werden.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1605695</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1605695</guid><dc:creator><![CDATA[Nexus]]></dc:creator><pubDate>Mon, 27 Oct 2008 18:21:11 GMT</pubDate></item><item><title><![CDATA[Reply to Bitwise OR on Mon, 27 Oct 2008 19:20:27 GMT]]></title><description><![CDATA[<p><a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/35992">@Martin</a> Richter: Sorry, die Operatorenpräzedenz hab ich nicht bedacht. Bin nur selten im bitweisen Bereich unterwegs... <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f609.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--winking_face"
      title=";)"
      alt="😉"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1605740</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1605740</guid><dc:creator><![CDATA[moagnus]]></dc:creator><pubDate>Mon, 27 Oct 2008 19:20:27 GMT</pubDate></item></channel></rss>