# Thread: Uint32 math

1. ## Uint32 math

I feel like spamming, but this I suspect could go terribly wrong if it fails...

So, I have some Uint32 flags which I'll pass to SDL_SetVideoMode (Width, Height, BPP, flags)

Reference is here http://docs.huihoo.com/sdl/1.2/sdlsetvideomode.html

Now, is it possible to do Uint32 math?
Say I have some flag variables

flag1 = SDL_HWSURFACE;
flag2 = SDL_OPENGL;
flag3 = SDL_FULLSCREEN;

then do a call like this

Uint32 flags = flag1 + flag2 + flag3;

And then pass it to SetVideoMode

SDL_SetVideoMode(800, 600, 32, flags);

Is this possible? Is it wise? And are there better ways?

Also, when I initialize a flag variable like: flag4 = NULL;
But still add it to 'flags' what will this do? Shouldn't I do this?

2. Surely you mean you would initialize a flag variable like: flag4 = 0.
And I'm pretty sure adding 0 does nothing whatsoever.

3. And what about the other stuff?

4. Of course you can add unsigned int variables together.

If they are intended to be bitflags, it is more idiomatic to use bitwise operations on them rather than arithmetic operations; but if all you are doing are turning things on (that you know are on) or turning things off (that you know are on) you can get away with + and -. If you don't know for sure whether a flag is already set, then you'll need to use &.

5. But if it works to just add them, how can SDL know what to turn on o.O?
When I add them, don't they turn into numbers? Or do they stay as the text type thing ?

6. The conventional way is to use "|", the bitwise OR operator.

For example, those constants could be declared as something like
SDL_HWSURFACE = 0x1;
SDL_SWSURFACE = 0x2;
SDL_OPENGL 0x4;
SDL_FULLSCREEN = 0x8;

notice how they all only have 1 bit set, so you can do, (SDL_HWSURFACE | SDL_OPENGL), and in their function, they can check whether you have one flag selected by something like
Code:
```if (x & SDL_OPENGL) {
...
}```
Using + in place of | will only work if the bit is NOT already set, and none of the constants have overlapping bits.

Just use |.

7. Originally Posted by Akkernight
But if it works to just add them, how can SDL know what to turn on o.O?
When I add them, don't they turn into numbers? Or do they stay as the text type thing ?
They weren't ever "text type thing"s to start with.

8. so when I declare the 'flags' Uint32 variable, I should do like

Uint32 flags = flag1 | flag2 | flag3 ?

9. Yeap.

10. Awesome, thanks!

11. And NULL is for pointers, not integers and not strings!

12. The use of NULL is actually discouraged over the use of 0. Personal preference and I really don't care one way or the other.

13. I really cannot see how, as long as you use NULL only for pointers.

Popular pages Recent additions