0

I have a binary code originated from bitse<16>binary_form (decimal_form). I want to check if the output code is 0010********1001 or 1001********0010 or 1001********0001. So i want to check the value of the first half byte and the value of the last half byte. I used if &if else statements but i am searching for more professional way to perform this check rather than check every bit.

Here is what i tried :

if(binary_form[0]==1&& binary_form[1]==0&& binary_form[2]==0&& binary_form[3]==1 &&binary_form[12]==0 &&binary_form[13]==1 &&binary_form[14]==0  &&binary_form[15]==0)
{
    cout<<"Jhon\n";
}
else if(binary_form[0]==0 &&binary_form[1]==1 &&binary_form[2]==0 &&binary_form[3]==0 &&binary_form[12]==1 &&binary_form[13]==0 &&binary_form[14]==0&& binary_form[15]==1)
{
    cout<<"Remon\n";
}
else if(binary_form[0]==1 &&binary_form[1]==0 &&binary_form[2]==0 && binary_form[3]==0 &&binary_form[12]==1 &&binary_form[13]==0 &&binary_form[14]==0 && binary_form[15]==1)
{
    cout<<"Soliman\n";
}

The bits marked with (*) are not necessary for my check.

Ahmed
  • 21
  • 6
  • 1
    couldn't you just mask out the irrelevant bits and test the whole thing directly for equality? `(bform & b1111000000001111) == `b0010000000001001`, etc...? – Marc B Oct 11 '16 at 18:50
  • as long as you are interested in this such long checking you should use switch rather than if/else. – Raindrop7 Oct 11 '16 at 19:26
  • will the bits values denoted by (*),change after masking??how the change will be permanently or temporary? – Ahmed Oct 11 '16 at 19:38

1 Answers1

0

** Let V[] be an array of unsigned characters sufficient to hold ** up to N bits. Let I be an integer between 0 and N. 0<=I

#define SETBIT(V,I)      V[I>>3] |= (1<<(I&7))
#define CLEARBIT(V,I)    V[I>>3] &= ~(1<<(I&7))
#define TESTBIT(V,I)     (V[I>>3]&(1<<(I&7)))!=0
Michael Haephrati
  • 3,660
  • 1
  • 33
  • 56