Graal Forums

Graal Forums (https://forums.graalonline.com/forums/index.php)
-   NPC Scripting (https://forums.graalonline.com/forums/forumdisplay.php?f=8)
-   -   Detecting multiple values in a bitflag (https://forums.graalonline.com/forums/showthread.php?t=66244)

jake13jake 05-26-2006 06:44 AM

Detecting multiple values in a bitflag
 
Is there any other way to test for multiple values in a bitflag other than iterating through it?

Rick 05-26-2006 08:23 AM

if (flags & (flag1 | flag2))

jake13jake 05-26-2006 03:12 PM

Quote:

Originally Posted by Rick
if (flags & (flag1 | flag2))

uhhhhhhh... not really what I meant.
you could have
100000000000000000000000000000001
or
000000000000000010000000000000000

I mean, of course I would know the range of this flag. it certainly wouldn't take all 32 places.

What would be the best way to detect if there is more than one value stored in it though?

for (i=0; i<flagsize; i++) {
if (flag & 2^i)
this.flagcounter++;
if (this.flagcounter == 2)
return true;
}
return false;

Rick 05-27-2006 12:22 AM

First of all, Graal doesn't support above 32bits for numbers (I think).
Second, what I said was correct

if (flags & (0x100000001 | 0x10000))

[edit]

We had a discussion about this on IRC and we came to that I misunderstood your original question, mostly because your original question was not written very well.

Anyway, my point of view: you're using bitflags for the wrong reason.

But uhh, here's how I would do it:

PHP Code:

function getFlagCount(flags)
{
  
temp.count 0;
  while (
temp.flags 0)
  {
    
temp.count += temp.flags 1;
    
temp.flags >>= 1;
  }
  return 
temp.count;



jake13jake 05-27-2006 03:16 AM

I'm using bit flags to conserve variable space actually.

Admins 05-27-2006 08:00 PM

You need to loop through all bits you want to check, there is no countbits() function or so yet

jake13jake 05-28-2006 04:49 AM

ok


All times are GMT +2. The time now is 12:51 PM.

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2026, vBulletin Solutions Inc.
Copyright (C) 1998-2019 Toonslab All Rights Reserved.