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:
private BitArray ToBitArray(byte b)
{
BitArray ba = new BitArray(8);
for (int i = 7; i >= 0; i--) {
if (b >= Convert.ToByte(Math.Pow(2d, (double)i))) {
ba[i] = true;
b = Convert.ToByte(b - Math.Pow(2d, (double)i));
} else {
ba[i] = false;
}
}
return ba;
}
private BitArray ToBitArray(byte b)
{
BitArray ba = new BitArray(8);
for (int i = 7; i >= 0; i--) {
if (b >= Convert.ToByte(Math.Pow(2d, (double)i))) {
ba[i] = true;
b = Convert.ToByte(b - Math.Pow(2d, (double)i));
} else {
ba[i] = false;
}
}
return ba;
}
To copy to clipboard, switch view to plain text mode
so far in qt I have this:
int flags = 6;
for (int i = 7; i >= 0; i--){
if(flags >= int(pow(double(i),2))){
ba[i] = true;
flags = int(flags - pow(double(i),2));
}else{
ba[i] = false;
}
}
for (int i = 0; i < ba.size(); i++){
if(ba[i]){
cout << i << ": true" << endl;
}else {
cout << i << ": false" << endl;
}
}
int flags = 6;
QBitArray ba; ba.resize(8);
for (int i = 7; i >= 0; i--){
if(flags >= int(pow(double(i),2))){
ba[i] = true;
flags = int(flags - pow(double(i),2));
}else{
ba[i] = false;
}
}
for (int i = 0; i < ba.size(); i++){
if(ba[i]){
cout << i << ": true" << endl;
}else {
cout << i << ": false" << endl;
}
}
To copy to clipboard, switch view to plain text mode
I get some true and false correct but most of them are wrong.
Bookmarks