John
John

Reputation: 804

is it possible to tell which array element comparison resulted in true?

i have a three dimensional array which i am using as a bit table

   char bit_table_[X][Y][Z];

X is not larger than 20 but Y and Z will be very large. the contents of every X's Y and Z will be compared parallely as follows (here real values of Y and Z will be computed using some hash functions). My problem is; I don't know if there is any way at all to tell as to which of the X's give true in the condition checking of the if statment

if (((bit_table_[0][i][bit_index / bits_per_char]|
bit_table_[1][i][bit_index / bits_per_char])& bit_mask[bit]) != bit_mask[bit])
     return true;

or is there anyother way of doing it?

Upvotes: 0

Views: 88

Answers (2)

Hicham
Hicham

Reputation: 979

the solutionof your question is simple : the if statement is true only if the array content for x=0 and x=1 have the same value : 0. assumin that the array contain only binary values (0 or 1) if ( (0 | 0 )& 1 ! = 1) is true all other cases for bit_array or bit_mask make the statement false !!


if the array can contain other values than 0 or 1, you can neither say that the content of the array for x=0 or for x=1 that makes the statement true. it is the association of the both of them :

simple example if you write if((0x0F | 0xF0) == 0) when the condition is true, neither 0x0F or 0xF0 makes it true ! it is the association of the two that make the condition true.

it is the same thing in your if statement.

Upvotes: 0

Joe
Joe

Reputation: 57179

You have to test them individually to know which one resulted in true. Here is an example using a logical rather than bitwise OR to determine which one resulted in true.

bool x1 = false, x2 = false;

if(
   (x1 = (bit_table_[0][i][bit_index/bits_per_char] & bit_mask[bit]) != bit_mask[bit]) ||
   (x2 = (bit_table_[1][i][bit_index/bits_per_char] & bit_mask[bit]) != bit_mask[bit])
  )
{
    //Check x1 and x2 here
    return true;
}

Edit:

To expand on the previous example, and to satisfy the results of your original post you could also check if the combination of both is the reason it passes like so:

bool x1 = false, x2 = false, both = false;
size_t zindex = bit_index/bits_per_char;
if(
   (x1 = (bit_table_[0][i][zindex] & bit_mask[bit]) != bit_mask[bit]) ||
   (x2 = (bit_table_[1][i][zindex] & bit_mask[bit]) != bit_mask[bit]) ||
   (both = ((bit_table_[0][i][zindex] | bit_table_[1][i][zindex]) &
            bit_mask[bit]) != bit_mask[bit])
  )
{
    //Check x1 and x2 here
    //If both is true then neither x1 or x2 resulted in true alone

    return true;
}

Upvotes: 1

Related Questions