#### Archived

This topic is now archived and is closed to further replies.

# Enums, bitwise stuff, and DirectX

This topic is 5038 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

DirectX uses enums to do stuff - like, set the color mode of the device, set what size the buffers are, set the format of the floating point stuff - the stuff that enums are used for. The thing I''m wondering is how DirectX interprets a value composed of enums. Like, to tell the device what settings to use, I would pass it a value like:
#define VERTEX_STYLE (D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_DIFFUSE | D3DFVF_SPECULAR | D3DFVF_TEX1 )

What I''m wondering though is how, given that with all those bitwise OR operations that stack up on each other, DirectX decomposes the number that results to figure out which particular flags were passed. What I want to do is, for instance, to take a value like VERTEX_STYLE and decompose it and find out that D3DFVF_DIFFUSE was passed to it. I figure that if DirectX is able to decompose the value to figure it out, that I should be able to. The reason I want to do this is because I need to ascertain what format a given index buffer is in.

##### Share on other sites
The individual constants within an enumeration have integer values. By carefully selecting the values (as powers of two), they are bitwise exclusive of each other (they share no common bits). Thus, each constant can be tested for with a simple bitwise AND.

The bitwise OR used to create your VERTEX_STYLE should have given you a clue.

To see if VERTEX_STYLE contained D3DFVF_DIFFUSE, programmatically:
if( VERTEX_STYLE & D3DFVF_DIFFUSE ){  // blah}
By the way, you might want to consider using C++''s const modifier to create flexible vertex formats:
const DWORD VERTEX_STYLE = D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_DIFFUSE | D3DFVF_SPECULAR | D3DFVF_TEX1;
Just a suggestion in the interests of typesafety.

##### Share on other sites
There''s a DX8.1 Flexible Vertex Format Cracker at Inframez. Take a look at it.

Optimize
Bitwise account: MHaggag -

##### Share on other sites
it''s pretty simple, actually. There are less flags than bits in a dword, so one dword can be used (with a little bitwise logic) to represent the entire flag.

basically you have something like:

enum {FLAG_1 = 1,FLAG_2 = 2,FLAG_3 = 4,FLAG_4 = 8,...}D3DFLAGS;

then you can simply declare:

D3DFLAGS Flags = FLAG_1 | FLAG_2 | FLAG_4;if(Flags == FLAG_2 & FLAG_4){// do something}

It''s far easier (and more compact) than having an array of boolean values or whatnot for the flags. Consider that a bool is actually a 32 bit integer on an x86 machine anyway, and you can see why it''s a good way to implement these sorts of things.