Value | Value2 etc

BlitzMax Forums/BlitzMax Programming/Value | Value2 etc

Snixx(Posted 2010) [#1]
How would you parse this, is it just a simple thing? for instance

const enableMouse:int = 100, enableKeyBoard:int = 101
...

test(enableMouse | enableKeyboard)

...

function test(cmdLine:int)
...work out what combination of ints the cmdLine contains here
end function

I would be looking to do the same as for instance... createwindow does for its style in maxgui.


GfK(Posted 2010) [#2]
const enableMouse:int = 1, enableKeyBoard:int = 2, somethingelse:int = 4, morestuff:int = 8 '... and so on.

...

test(enableMouse | enableKeyboard)

...

function test(cmdLine:int)
  if cmdLine & enableMouse
  EndIf

  If cmdLine & enableKeyboard
  Endif
end function



Who was John Galt?(Posted 2010) [#3]
What Gfk said. Important point to observe is that your flag definitions have to be powers of 2.


Snixx(Posted 2010) [#4]
ahhh thanks great.


Mahan(Posted 2010) [#5]
Just for clarity of the mechanism used (it's always nice to know why and not only how imho):

What the "power of 2" above actually does is to define names name to certain bits (you know the 1 and 0 of computers) in an variable.

Lets say you have a byte (values: 0-255) you can also write it binary as values 00000000-11111111.

Now let's say we give each bit-position a name:

0 0 0 0 0 0 0 0
e s s f f t t o
i e i i o h w n
g v x v u r o e
h e   e r e 
t n       e


The values of a bit-value of 1 in each position is as follows:

one = 1
two = 2
three= 4
four = 8
five = 16
six = 32
seven = 64
eight = 128

Here you have the "power of 2" values, right?

And just as Gfk shows in his example one can use bit-manipulation operators like "|" and "&" to set bits or test if they are set.


Shortwind(Posted 2010) [#6]
Mahan, while your example should allow the orignal poster a better understanding of what your trying to convay, it should be noted how to compute the actual bit position values...

Position:
2^7+2^6+2^5+2^4+2^3+2^2+2^1+2^0

So a binary number such as 01101001 would be:
2^7*0 + 2^6*1 + 2^5*1 + 2^4*0 + 2^3*1 + 2^2*0 + 2^1*0 + 2^0*1 = 105 decimal

or more simply:
0+64+32+0+8+0+0+1 = 105

:D