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

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
teamAlpha
Posts: 68
Joined: Sun Mar 08, 2009 4:14 pm

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

Post 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:
Last edited by teamAlpha on Fri Apr 24, 2009 12:52 am, edited 1 time in total.
Alpha::AppleBoy
Acki
Posts: 3496
Joined: Tue Jun 29, 2004 12:04 am
Location: Nobody's Place (Venlo NL)
Contact:

Post 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 ???
while(!asleep) sheep++;
IrrExtensions:Image
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
teamAlpha
Posts: 68
Joined: Sun Mar 08, 2009 4:14 pm

Post 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
Alpha::AppleBoy
teamAlpha
Posts: 68
Joined: Sun Mar 08, 2009 4:14 pm

Post by teamAlpha »

Damn! Thanks to this demo i spotted a stupid mistake! i had a flag out of range (0x99..instead 0x9)...lol :oops:
Alpha::AppleBoy
Acki
Posts: 3496
Joined: Tue Jun 29, 2004 12:04 am
Location: Nobody's Place (Venlo NL)
Contact:

Post 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... ;)
while(!asleep) sheep++;
IrrExtensions:Image
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
teamAlpha
Posts: 68
Joined: Sun Mar 08, 2009 4:14 pm

Post 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
Alpha::AppleBoy
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

Well, in that case 9 is also out of bounds, because 1<<9 overflows the uchar you are using :wink:
teamAlpha
Posts: 68
Joined: Sun Mar 08, 2009 4:14 pm

Post 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...
Alpha::AppleBoy
Post Reply