Combining Flags, "+" or "OR"
 
 
mtsmox  
(2/26/00 5:16:42 pm)

This might be a silly question, but here it goes:

if you want to combine flags like

dim DDSD as DDSURFACEDESC2
DDSD.lFlags = DDSD_CAPS + DDSD_WIDTH + DDSD_HEIGHT

most of the time I see "Or" instead of "+"
Is there a (big) difference?

Joren



 
MetalWarrior  
(2/26/00 6:21:30 pm)

I really have no idea. I just always use Or.



 
black eyez  
(2/27/00 3:20:08 pm)

Or is technically the 'better' way..... I haven't really checked it out, but flags are MEANT to be combined using a binary operation just because of their nature.....

David



 
Eric Coleman  
(2/27/00 8:56:36 pm)

There is a HUGE difference between "or" and "+." 

Or combines binary flags, "+" add thems. 

for example:

flag = 1

if you do "flag + flag" then you'll get 2. If you do "flag or flag", which means (for this example) 1 or 1, then that's a big difference.

I'll use the example from the directX sdk.

red = 1
blue = 2
purple = 3
green = 4

purple = red or purple 'which makes sense, purple = red OR purple = purple. Obviously purple = purple, but only one of the statements has to be true for the whole thing to be true. purple equals one or the other.
green = red + purple 'This is obviously wrong. Numerically its correct, but the flags are all wrong.
the statement
purple = red or blue is true.

I'll show you in binary form, which is where the power of the boolean operations comes from.

(I'm using 4 bits here)
red = 1 = 0001
blue = 2 = 0010
purple = 3 = 0011
green = 4 = 0100

red or blue is this
0001 or
0010 
----- = purple
0011

red or red is this
0001 or
0001 
----- = red
0001

red + red is this
0001 or
0001
----- = blue
0010

I hope that helps.
Eric


mtsmox  
(2/29/00 7:04:02 am)

Thanks for the example, but I've got one problem:
If you combine flags using "Or", you don't get a dropdown box like you get after you type "=", but if you use "+" you do get a dropdownd box, so I always thought: that should be the way to use it.

I think I will type "+" to get the dropdown box and replace it with "Or" if I'm done.

PS.
I think I get it, but something is telling my I don't 

Joren



 
Eric Coleman  
(2/29/00 9:55:59 am)

 OR combines bits.
+ adds them like they're numbers, meaning you get the modulus added the next bit and the truncated quotient replaces the original value.
1 + 1 = 2
1 or 1 = 1

Everything below this line is from the SDK.................

Many object methods in DirectX for Visual Basic take a flags parameter, or a parameter with a similar name. Also, some types have a member containing flags. Flags are used to control miscellaneous aspects of the behavior of method calls or to report details about things like device capabilities. 

A flag is a single bit of information. It dictates or reports whether a certain behavior or capability is present or absent. A set of related flags is represented by an enumeration whose members generally do not share any bits. (Some flags, however, may represent combinations of other flags.)

Because flags represent single bits, multiple flags can be combined in a single value. For example, suppose you wanted to set the cooperative level of a DirectInput device to both foreground and exclusive. You would do so by combining two flags, using a logical Or operation, as follows:

' diDevice is a DirectInputDevice object, and hwnd is a window handle
diDevice.SetCooperativeLevel(hwnd, _
DISCL_FOREGROUND Or DISCL_EXCLUSIVE)
In most cases, combining two or more flags by using the Or operator is equivalent to summing them by using the addition operator. However, it is unwise to add flags together, because of the fact that some flags do represent combinations of bits. In the following hypothetical example, the flag CF_PURPLE represents a combination of CF_RED and CF_BLUE.

Enum COLORFLAGS
CF_RED = 1
CF_BLUE = 2
CF_PURPLE = 3
CF_GREEN = 4
End Enum

Dim Color As Integer
Color = CF_RED Or CF_PURPLE
The value of Color is now 3. CF_RED is redundant, because CF_PURPLE already contains that value.

The following operation, on the other hand, sets an incorrect value:

' This is wrong!
Color = CF_RED + CF_PURPLE
The variable is now set to 4, which is equivalent to CF_GREEN.

When you retrieve a value for flags, you can extract the setting of an individual flag by using a logical And operation. If the flags value contains the bit represented by a flag constant, the And operation returns a nonzero value and that flag is "on". If the And operation returns zero, the flag is "off". For example, the following call determines whether an input device, whose capabilities have been retrieved in a variable of type DIDEVCAPS, is physically attached to the system:

Dim IsAttached As Boolean
IsAttached = diDevCaps.lFlags And DIDC_ATTACHED
Note that you should not test for equality, as in the following example:

' This is wrong!
IsAttached = (diDevCaps.lFlags = DIDC_ATTACHED)
The reason this won't work is that the value in lFlags might contain other bits as well, such as DIDC_FORCEFEEDBACK. (See CONST_DIDEVCAPSFLAGS.)



 
mtsmox  
(3/2/00 11:02:12 am)

Thanks, the only thing I guess I didn't know/understand from your previous message was that purple was a combination off the other flags..

But since this was for silly questions, here's another one:

What exactly is the SDK?

I've just downloaded a help file form MSDN, is the SDK better? (I guess it should be for 128(?)MB )

Joren


 

Daniel Netz
(3/3/00 1:33:21 am)

SDK is a Software Development Kit, it exists for alot of API's like DirectX, Java etc. and is a kit of tools, code, maybe samples to help people get started with development.

The DirectX7 SDK is about 128 MB I think, and it contains DX7 Runtime files, DX for VB library, C++ and VB sourcecode, SDK documentation and some DirectX utilities. 
If you can, you should download this, it's really useful.