Page 1 of 1

[x][solved]C++ - issue with bit flags

Posted: Thu Apr 23, 2009 6:24 pm
by teamAlpha
Hi!!

Im trying to add bit flags in my 'options' class for efficiency , but i must be doing something wrong , and not all flags are working... :?

Here is my code:

Code: Select all

unisgned char flags = 0x00;

const unsigned char VSYNC      = 0x01;
const unsigned char FULLSCREEN = 0x02;
more...


void add(unsigned char option)
{
	flags |= option;
}

void remove(unsigned char option)
{
	flags &= ~option;
}

bool check(unsigned char option)
{
	return (( flags & option ) != 0);
}


Example :

add(VSYNC);
add(FULLSCREEN);
add(More..);

remove(some unused flag)

if( check(VSYNC) ) < -- returns always 0

Any ideas why it isn't working ?
:cry:

Posted: Thu Apr 23, 2009 6:51 pm
by Acki
I can't reproduce the error with this code... :shock:
what other flags are you using and what values are asigned to them ???

Posted: Thu Apr 23, 2009 7:10 pm
by teamAlpha
I made a quick demo , please try it :

Code: Select all

#include <iostream>

using namespace std;

unsigned char flags = 0x0;

const unsigned char VSYNC      = 0x1;
const unsigned char FULLSCREEN = 0x2;

void add(unsigned char option)
{
   flags |= option;
}

void remove(unsigned char option)
{
   flags &= ~option;
}

bool check(unsigned char option)
{
   return (( flags & option ) != 0);
}

void print(bool r)
{
    cout << check(r) <<endl;
}

void test1()
{
    cout << "TEST 1 - remove FULLSCREEN bit first" <<endl;
    add(FULLSCREEN);
    add(VSYNC);

    cout <<"[BEFORE] FS = " << check(FULLSCREEN) << " , VS = " <<check(VSYNC) <<endl;

    cout<<"[REMOVING]"<<endl;
    remove(FULLSCREEN);

    cout <<"[AFTER] FS = " << check(FULLSCREEN) << " , VS = " <<check(VSYNC) <<endl;
}

void test2()
{
    cout << "TEST 2 - remove VSYNC bit first" <<endl;
    add(FULLSCREEN);
    add(VSYNC);

    cout <<"[BEFORE] FS = " << check(FULLSCREEN) << " , VS = " <<check(VSYNC) <<endl;

    cout<<"[REMOVING]"<<endl;
    remove(VSYNC);

    cout <<"[AFTER] FS = " << check(FULLSCREEN) << " , VS = " <<check(VSYNC) <<endl;
}

void test3()
{
    cout << "TEST 3 - remove both" <<endl;
    add(FULLSCREEN);
    add(VSYNC);

    cout <<"[BEFORE] FS = " << check(FULLSCREEN) << " , VS = " <<check(VSYNC) <<endl;

    cout<<"[REMOVING]"<<endl;
    remove(VSYNC);

    cout<<"[REMOVING]"<<endl;
    remove(FULLSCREEN);

    cout <<"[AFTER] FS = " << check(FULLSCREEN) << " , VS = " <<check(VSYNC) <<endl;
}

int main()
{
    test1();
    cout<<endl<<endl;
    test2();
    cout<<endl<<endl;
    test3();
    cout<<endl<<endl;

    return 0;
}


Result:

Code: Select all

TEST 1 - remove FULLSCREEN bit first
[BEFORE] FS = 1 , VS = 1
[REMOVING]
[AFTER] FS = 0 , VS = 1


TEST 2 - remove VSYNC bit first
[BEFORE] FS = 1 , VS = 1
[REMOVING]
[AFTER] FS = 1 , VS = 0


TEST 3 - remove both
[BEFORE] FS = 1 , VS = 1
[REMOVING]
[REMOVING]
[AFTER] FS = 0 , VS = 0

Posted: Thu Apr 23, 2009 7:17 pm
by teamAlpha
Damn! Thanks to this demo i spotted a stupid mistake! i had a flag out of range (0x99..instead 0x9)...lol :oops:

Posted: Thu Apr 23, 2009 9:30 pm
by Acki
teamAlpha wrote: i had a flag out of range (0x99..instead 0x9)...lol :oops:
isn't 9 a bad flag !?!?! :shock:
because 9 is already 1 | 8 (= 00001001), so if you remove 9 you remove 1 and 8 (also if you set 9 you set 1 and 8 ) !!! :lol:
the flags should be 2^n (1, 2, 4, 8, 16, 32, ...), that's why I asked about the values... ;)

Posted: Fri Apr 24, 2009 12:49 am
by teamAlpha
Acki wrote:
teamAlpha wrote: i had a flag out of range (0x99..instead 0x9)...lol :oops:
isn't 9 a bad flag !?!?! :shock:
because 9 is already 1 | 8 (= 00001001), so if you remove 9 you remove 1 and 8 (also if you set 9 you set 1 and 8 ) !!! :lol:
the flags should be 2^n (1, 2, 4, 8, 16, 32, ...), that's why I asked about the values... ;)
GeeeeeeeeeK detected (eeeeeek! :lol: )...

Yeah , i noticed this earlier , so im just doing a bit of binary math to the constants (just shifting them by 1) and it converts the number to a power of 2. :D

Posted: Fri Apr 24, 2009 7:30 am
by hybrid
Well, in that case 9 is also out of bounds, because 1<<9 overflows the uchar you are using :wink:

Posted: Fri Apr 24, 2009 10:26 am
by teamAlpha
hybrid wrote:Well, in that case 9 is also out of bounds, because 1<<9 overflows the uchar you are using :wink:
So far i have 8 config states( = 0x7 = 128) , so i didn't noticed it :shock: ...
I'll upgrade to s32 or u32 then...