Uint32 math

This is a discussion on Uint32 math within the C++ Programming forums, part of the General Programming Boards category; I feel like spamming, but this I suspect could go terribly wrong if it fails... So, I have some Uint32 ...

  1. #1
    Hail to the king, baby. Akkernight's Avatar
    Join Date
    Oct 2008
    Location
    Faroe Islands
    Posts
    717

    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?

    Thanks in advance!
    Currently research OpenGL

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Surely you mean you would initialize a flag variable like: flag4 = 0.
    And I'm pretty sure adding 0 does nothing whatsoever.

  3. #3
    Hail to the king, baby. Akkernight's Avatar
    Join Date
    Oct 2008
    Location
    Faroe Islands
    Posts
    717
    And what about the other stuff?
    Currently research OpenGL

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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. #5
    Hail to the king, baby. Akkernight's Avatar
    Join Date
    Oct 2008
    Location
    Faroe Islands
    Posts
    717
    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 ?
    Currently research OpenGL

  6. #6
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,167
    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. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by Akkernight View Post
    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. #8
    Hail to the king, baby. Akkernight's Avatar
    Join Date
    Oct 2008
    Location
    Faroe Islands
    Posts
    717
    so when I declare the 'flags' Uint32 variable, I should do like

    Uint32 flags = flag1 | flag2 | flag3 ?
    Currently research OpenGL

  9. #9
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,167
    Yeap.

  10. #10
    Hail to the king, baby. Akkernight's Avatar
    Join Date
    Oct 2008
    Location
    Faroe Islands
    Posts
    717
    Awesome, thanks!
    Currently research OpenGL

  11. #11
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,420
    And NULL is for pointers, not integers and not strings!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,590
    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. #13
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,420
    I really cannot see how, as long as you use NULL only for pointers.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. brace-enclosed error
    By jdc18 in forum C++ Programming
    Replies: 53
    Last Post: 05-03-2007, 05:49 PM
  2. MD5 hashing problems =(
    By Uncle Rico in forum C Programming
    Replies: 4
    Last Post: 02-28-2005, 09:31 AM
  3. MD5 Algorithm
    By Inquirer in forum C Programming
    Replies: 2
    Last Post: 12-28-2003, 10:55 PM
  4. toughest math course
    By axon in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 10-28-2003, 09:06 PM
  5. libtiff: TIFFWriteRGBAImage()?
    By dug in forum Game Programming
    Replies: 1
    Last Post: 07-09-2003, 09:01 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21