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.
teamAlpha
Posts: 68 Joined: Sun Mar 08, 2009 4:14 pm
Post
by teamAlpha » Thu Apr 23, 2009 6:24 pm
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 ?
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 » Thu Apr 23, 2009 6:51 pm
I can't reproduce the error with this code...
what other flags are you using and what values are asigned to them ???
teamAlpha
Posts: 68 Joined: Sun Mar 08, 2009 4:14 pm
Post
by teamAlpha » Thu Apr 23, 2009 7:10 pm
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 » Thu Apr 23, 2009 7:17 pm
Damn! Thanks to this demo i spotted a stupid mistake! i had a flag out of range (0x99..instead 0x9)...lol
Alpha::AppleBoy
Acki
Posts: 3496 Joined: Tue Jun 29, 2004 12:04 am
Location: Nobody's Place (Venlo NL)
Contact:
Post
by Acki » Thu Apr 23, 2009 9:30 pm
teamAlpha wrote: i had a flag out of range (0x99..instead 0x9)...lol
isn't 9 a bad flag !?!?!
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 ) !!!
the flags should be 2^n (1, 2, 4, 8, 16, 32, ...), that's why I asked about the values...
teamAlpha
Posts: 68 Joined: Sun Mar 08, 2009 4:14 pm
Post
by teamAlpha » Fri Apr 24, 2009 12:49 am
Acki wrote: teamAlpha wrote: i had a flag out of range (0x99..instead 0x9)...lol
isn't 9 a bad flag !?!?!
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 ) !!!
the flags should be 2^n (1, 2, 4, 8, 16, 32, ...), that's why I asked about the values...
GeeeeeeeeeK detected (eeeeeek!
)...
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.
Alpha::AppleBoy
hybrid
Admin
Posts: 14143 Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:
Post
by hybrid » Fri Apr 24, 2009 7:30 am
Well, in that case 9 is also out of bounds, because 1<<9 overflows the uchar you are using
teamAlpha
Posts: 68 Joined: Sun Mar 08, 2009 4:14 pm
Post
by teamAlpha » Fri Apr 24, 2009 10:26 am
hybrid wrote: Well, in that case 9 is also out of bounds, because 1<<9 overflows the uchar you are using
So far i have 8 config states( = 0x7 = 128) , so i didn't noticed it
...
I'll upgrade to s32 or u32 then...
Alpha::AppleBoy