2014-11-21 1 views
0

0x0001 인 NSData 객체의 비트 0-6이 1과 같은지 어떻게 확인합니까?NSData 객체의 비트 검사

내 코드

const char *byte = [dataObject bytes]; 
for (int i=0; i<2; i++) { 
    char n = byte[i]; 
    char buffer[9]; 
    buffer[8] = 0; //for null 
    int j = 8; 
    while(j > 0) 
    { 
     if(n & 0x01) 
     { 
      buffer[--j] = '1'; 

      //a bit is equal to 1 from my understanding 
     } else 
     { 
      buffer[--j] = '0'; 
     } 

     n >>= 1; 
    } 
} 

비트 1은 분명 사실이 아니다 이는 한 것을 말한다. 이것은 작은 엔디안 시스템을 인 아이폰에서 실행

+0

설명은 확장 토론이 아닙니다. 이 대화는 [채팅으로 이동되었습니다] (http://chat.stackoverflow.com/rooms/65324/discussion-on-question-by-stackoverflew-checking-bits-in-an-nsdata-object). – Taryn

+0

'if (theByte & 0x01) ' –

+0

바이트에 대해 8 비트의 개별 비트 각각에 대해 무엇을 사는지 알겠습니까? – stackOverFlew

답변

1

이, 나는 비트 (5)는 예를 들어 설정되어 있는지 확인하려면

const char *byte = [fixtureStatusBasic bytes]; //objective c, puts the 2 bytes into *byte 
char n = byte[0]; //first byte is now called n 
if(n & 0b00111111){ //AND the byte "n" with 6 least significant bits set to 1 to see if any of the 6 bits is set to 1 

    //if this is true, and the program goes here, that means that one of the bits is set to 1 

} 

학습 결국 무엇

if(n & 0b00010000){ 

    //5th least significant byte is set to 1. 
} 

고맙습니다.

+1

'5'는 유효한 이진수가 아닙니다. '0b00010000'을 의미합니다. – rmaddy

+0

롤 진정한 해달라고 어떻게 내가 그걸 보지 못했는지 안다. – stackOverFlew