944,011 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 9247
  • C RSS
Jan 27th, 2005
0

bitwise operations and masking

Expand Post »
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:
  1.  
  2. for(x=size; x > 1; x--)
  3. {
  4. for(int y=0; y < 8; y++)
  5. {
  6. r = (int) (rand()%800);
  7. if(r < 400)
  8. {
  9. chrom[y] = (float) population[x] && mask[y];
  10. }//end if
  11.  
  12. if(r > 400)
  13. {
  14. chrom[y] = (float) population[x-1] && mask[y];
  15. }//end if
  16.  
  17. nGen[x] = (float) chrom[0] || chrom[1] || chrom[2] ||
  18. chrom[3] || chrom[4] || chrom[5] ||
  19. chrom[6] || chrom[7];
  20.  
  21. } //end y
  22. } // 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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mr_mooz is offline Offline
11 posts
since Oct 2004
Jan 27th, 2005
0

Re: bitwise operations and masking

>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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Jan 27th, 2005
0

Re: bitwise operations and masking

So how can I get round that? when i use just & or | i get an error.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mr_mooz is offline Offline
11 posts
since Oct 2004
Jan 27th, 2005
0

Re: bitwise operations and masking

Quote originally posted by mr_mooz ...
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.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Jan 27th, 2005
0

Re: bitwise operations and masking

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!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mr_mooz is offline Offline
11 posts
since Oct 2004
Jan 27th, 2005
0

Re: bitwise operations and masking

Quote originally posted by mr_mooz ...
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.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Inputting from files
Next Thread in C Forum Timeline: how do you set a variable to be a filename?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC