2017-10-25 14 views
-1

I 3 개 맵 배열을 갖고 I 이하의 조건에 기초하여 부울 결과를 산출 할 :이 예에서는, 비트 3과 비트 2된다에서C, 비트 맵 조작

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 

:

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. 

한 쌍 (비트 맵 # 2 참조). bit3에 1을 지정하고 (비트 맵 # 3 참조) 다음을 입력합니다.

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; 

어떻게 비트 연산을 사용하여 결과를 계산합니까?

감사합니다.

+2

당신은 무엇을 시도? 당신의 시도는 어떻게 작동 했습니까? [좋은 질문을하는 방법에 대해 읽으십시오] (http://stackoverflow.com/help/how-to-ask) 및 [최소한의 완전하고 검증 가능한 예] (http : /stackoverflow.com/help/mcve). –

답변

0

이 의사는 당신이 어떤 결과에 대한 2 비트 쌍을 테스트 할 수있는 방법을 보여줍니다. 8 비트 비트 맵에서 0..3 쌍을 사용할 수 있습니다.

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