Hi all!

I have a function that was given to me from a c++ project and I have to use it in qt. The function receives a byte value and according to that byte value creates a bitarray with true and false values that represent flags.

Lets say that I have 7 flags {cancel, retry, confirm, stop, manual, resume, skip}

Then for example the value:
- 1 means that the cancel is active.
- 2 means that the retry is active.
- 2 means that the cancel and the retry are active.
- 4 means that the confirm is active.
- 5 means that the cancel and the confirm are active
...
...
and so on until 127 where all are active

a co-worker of mine wrote a function to do this in C++ but I can use it well in Qt:

Qt Code:
  1. private BitArray ToBitArray(byte b)
  2. {
  3. BitArray ba = new BitArray(8);
  4.  
  5. for (int i = 7; i >= 0; i--) {
  6.  
  7. if (b >= Convert.ToByte(Math.Pow(2d, (double)i))) {
  8.  
  9. ba[i] = true;
  10. b = Convert.ToByte(b - Math.Pow(2d, (double)i));
  11. } else {
  12. ba[i] = false;
  13. }
  14. }
  15.  
  16. return ba;
  17. }
To copy to clipboard, switch view to plain text mode 

so far in qt I have this:

Qt Code:
  1. int flags = 6;
  2.  
  3. QBitArray ba; ba.resize(8);
  4.  
  5. for (int i = 7; i >= 0; i--){
  6.  
  7. if(flags >= int(pow(double(i),2))){
  8. ba[i] = true;
  9. flags = int(flags - pow(double(i),2));
  10. }else{
  11. ba[i] = false;
  12. }
  13. }
  14.  
  15. for (int i = 0; i < ba.size(); i++){
  16. if(ba[i]){
  17. cout << i << ": true" << endl;
  18. }else {
  19. cout << i << ": false" << endl;
  20. }
  21. }
To copy to clipboard, switch view to plain text mode 

I get some true and false correct but most of them are wrong.