bitwise operations and masking

Reply

Join Date: Oct 2004
Posts: 11
Reputation: mr_mooz is an unknown quantity at this point 
Solved Threads: 0
mr_mooz mr_mooz is offline Offline
Newbie Poster

bitwise operations and masking

 
0
  #1
Jan 27th, 2005
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,625
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 715
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: bitwise operations and masking

 
0
  #2
Jan 27th, 2005
>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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 11
Reputation: mr_mooz is an unknown quantity at this point 
Solved Threads: 0
mr_mooz mr_mooz is offline Offline
Newbie Poster

Re: bitwise operations and masking

 
0
  #3
Jan 27th, 2005
So how can I get round that? when i use just & or | i get an error.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,343
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 237
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: bitwise operations and masking

 
0
  #4
Jan 27th, 2005
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.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 11
Reputation: mr_mooz is an unknown quantity at this point 
Solved Threads: 0
mr_mooz mr_mooz is offline Offline
Newbie Poster

Re: bitwise operations and masking

 
0
  #5
Jan 27th, 2005
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!
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,343
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 237
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: bitwise operations and masking

 
0
  #6
Jan 27th, 2005
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.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC