2017-10-25 5 views
-1

Ich habe 3 bitmap-Arrays und ich mag, basierend auf Bedingungen unten ein bool Ergebnis berechnen:C, bitmap Betrieb

1) Neighboring even and odd index bits are a pair (the pair relationship will be provided by a bitmap). For example: pair0 = (bit1, bit0); pair1 = (bit3, bit2); etc. 

2) For a given bit, if its pair has been already set to 0, then return false; else, return true. 

Zum Beispiel:

Bit index -----------> 3 2 1 0 
______________________________________________________________ 
1) Bitmap#1: 1 1 1 1 1 ? 1 1 (here, "?" could be 0 or 1) 
2) Bitmap#2: 0 0 0 0 1 1 0 0 
3) Bitmap#3: 0 0 0 0 1 0 0 0 

In diesem Beispiel bit3 und bit2 sind ein Paar (siehe Bitmap # 2). Nennen Sie uns die BIT3 1 (siehe Bitmap # 3), dann gilt:

1) If bit #2 (the "?") in Bitmap#1 is 0, then return false; 
2) If bit #2 (the "?") in Bitmap#1 is 1, then return true; 

Wie verwende ich Bit-Betrieb um das Ergebnis zu berechnen?

danke!

+2

Was haben Sie versucht? Wie hat dein Versuch funktioniert oder nicht funktioniert? Bitte nehmen Sie sich etwas Zeit [lesen Sie, wie Sie gute Fragen stellen können] (http://stackoverflow.com/help/how-to-ask) und erfahren Sie, wie Sie ein [minimales, vollständiges und verifizierbares Beispiel] erstellen können (http://stackoverflow.com/help/mcve). –

Antwort

0

Dieser Pseudocode zeigt, wie Sie ein 2-Bit-Paar für jedes Ergebnis testen können. Sie können Paar 0.3 in einer 8-Bitbitmap verwenden;

unsigned char mask = 0x3; // 00000011 -> mask for two bits 
unsigned int pair=1;  // pair with bit #2 and #3 
unsigned char result = (bitmap & (mask<<(pair*2)) // shift the mask pair * 2 bits to left (00001100) 
result >>= (pair*2);   // shift result back pair * 2 bits 

if(result == 0x3) return both;  // bit #3 and #2 are 1 
if(result == 0x2) return onlyleft; // bit #3 is 1 #2 is 0 
if(result == 0x1) return onlyright; // bit #3 is 0 #2 is 1 
if(result == 0x0) return none;  // bit #3 and #2 are 0