bitwise OR in function parameters

This is a discussion on bitwise OR in function parameters within the C++ Programming forums, part of the General Programming Boards category; Does anyone know how bitwise OR parameters are used in calling a function? Like: function( value1 | value2 ); does ...

  1. #1
    Unregistered
    Guest

    bitwise OR in function parameters

    Does anyone know how bitwise OR parameters are used in calling a function?

    Like:
    function( value1 | value2 );

    does anyone know how this works?

  2. #2
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,267
    bit masking, or checking if certain bits are set


    eg if i had a Mask of 1

    0000001 in binary

    and say i set a 'var' to 21

    0010101 in binary

    in both the mask an var the 1 bit is set, so do whatever when Mask is true

    i would use the bitwise '&' operator to check and see if the bit's were set

    if(var & Mask)
    // do something

    example:

    Code:
    #define MASK_1  1 // 1(00000001)
    #define MASK_2  1 << 1  // 2(00000010)
    #define MASK_3  1 << 2  // 4(00000100)
    
    bool checkmask(int var)
    {
        if(var & MASK_1)
    //        do something
    
        if(var & MASK_2)
    //        do something else
    
        if(var & MASK_3)
    //        do something
    
    }
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

  3. #3
    Registered User
    Join Date
    Jul 2002
    Posts
    273

    Re: bitwise OR in function parameters

    Originally posted by Unregistered
    Does anyone know how bitwise OR parameters are used in calling a function?

    Like:
    function( value1 | value2 );

    does anyone know how this works?
    it combines the bits. All bits set in both values are passed in.

    value1 = 00110000
    value2 = 01100110

    value1 | value2 = 01110110

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    362
    Unregistered,

    Hershlag and no-one have both posted great information, but I'm uncertain that either response goes to your question.

    You ask about a function "call", but what about the "prototype/function definition"?

    Uncharted territory: You've taken advantage of the bitwise operator within a function "call", (assuming no compiler error), but ...

    This would seem to be something that the compilation of your code would "unearth". That is, it either "flies", or it doesn't.

    Offhand, I would suggest that it won't work. The reason? Unlike the "call", the definition requires a "type", i.e. a char, an int, a float, a reference, etc.

    Unless you pre-define the type of the parameter, I don't see how you can access the function.

    More specifically, we're assuming an int (for argument's sake). Now, the user (go figure) re-defines the values to 'long doubles'.

    Your "bitwise OR" may, and I stress may, work, but what about the call to the function?

    Can it be done? Possibly.

    Good idea? Probably not.

    -Skipper
    "When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow

  5. #5
    Registered User
    Join Date
    Jul 2002
    Posts
    273
    uh that was interesting.... I don't think he's trying to declare a function prototype that way. I think he's talking about passing the two values or'd together into the function.

    Anyway, I'm sure he's asking cause he saw it somewhere. WindowsAPI uses combined bitflags in lots of places and I use them myself. They are actually very common. You lost me somewhere.

  6. #6
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Yeah skipper that is a good angle to tackle but it does somewhat insult the intelligence of our unregistered friend. This has NOTHING to do with your initial question but it goes along with what you are doing. This is called a bitfield:

    Code:
    typedef unsigned char byte;  //this just saves you some typing
    union bitfield {
        byte Byte;
        struct {
            byte hi : 4;   /*the number after the colon is how many bits this variable takes up*/
            byte lo : 4;
        } bits;
    };
    
    int main() {
        bitfield bf;
     
        bf.Byte = 0x88;
        cout << bf.bits.hi << " " << bf.bits.lo;
    
        return 0;
    }
    You don't need to use a union for this sort of thing however some compilers are weird about this stuff and mis-align data.
    Last edited by master5001; 07-23-2002 at 05:13 AM.

  7. #7
    Registered User
    Join Date
    Apr 2002
    Posts
    362
    Master5001,

    Sorry about the "pregnant pause". (A death in the family.)

    Yeah skipper that is a good angle to tackle but it does somewhat insult the intelligence of our unregistered friend.
    My intent was certainly never to insult anyone's intelligence though I appreciate your insight. (My own is limited enough without disparaging someone else's. )

    Sorry if I "lost" anyone but I don't make assumptions regarding an unregistered guest's knowledge/understanding of the subject matter.

    -Skipper
    "When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 11:03 AM
  4. Ask about function parameters and pass a reference.
    By ooosawaddee3 in forum C++ Programming
    Replies: 1
    Last Post: 11-04-2002, 12:14 PM

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