Hi

I have an array of float and i need to mask the values.

I want to be able to get arr[x] and arr[x+1] and then mask them and recombine them results in a radom order (im doing an evolutionary algorithm!)

But i cant get the masking to work, it always seems to return 1 as a reult.

This is the code that dosnt work:

for(x=size; x > 1; x--)
    {   
        for(int y=0; y < 8; y++)
        {
            r = (int) (rand()%800);
            if(r < 400) 
              {                          
              chrom[y] = (float) population[x] && mask[y];               
              }//end if
              
             if(r > 400)
              {
              chrom[y] = (float) population[x-1] && mask[y]; 
              }//end if
                        
    nGen[x] = (float) chrom[0] || chrom[1] || chrom[2] || 
              chrom[3] || chrom[4] || chrom[5] || 
              chrom[6] || chrom[7];
                                                                                                                                                                                                         
    }  //end y
}  // end x

mask[] is an arra of floats that stores the values I want to mask (as they need to be repeated through in order)

float mask[8] = { 0x0003, 0x0F0C, 0x0030, 0x00C0, 0x0300, 0x0C00, 0x3000, 0xC000 };


and chrom is where i want to store the incomplete values before ORing them together to make a complete float.

Any ideas on how to do the bitwise and with floats?? It just wont work!

Cheers.

Recommended Answers

All 5 Replies

>it always seems to return 1 as a reult.
I would imagine that's because both operands are non-zero. You're using the logical AND and logical OR rather than the bitwise AND and bitwise OR. The logical operators are && and ||, and the bitwise operators are & and |.

>It just wont work!
Correct. The bitwise operators only work with integral values.

So how can I get round that? when i use just & or | i get an error.

So how can I get round that? when i use just & or | i get an error.

You want to bitwise AND or bitwise OR two floating point values? The result may not be a number even if you were able to combine the bits of each object.

Thats ok, because i can check the validity of the floats that it would produce, they have to be within a certain range and its permissible to replace any malformed floats with a random but valid replacement.

So i dont mind if it dosnt always work, as long as it would work most of the time!

Thats ok, because i can check the validity of the floats that it would produce, they have to be within a certain range and its permissible to replace any malformed floats with a random but valid replacement.

So i dont mind if it dosnt always work, as long as it would work most of the time!

No, it's not okay. The result may be undefined and accessing such a value could cause a crash, after which it would be difficult to check the range.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.