Thread: Using | in function params??

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    132

    Using | in function params??

    I have a function that takes flags, it looks like this:
    Code:
    void Something1(en1 flags)
    {
        ...
        ...
    }
    
    //where en1 is an enum as:
    enum en1
    {
       FLAG1,
       FLAG2,
       FLAG3,
       FLAG4,
    }
    //I need to select more than one flag using the bitwise or operator | like that:
    Something1(en1(FLAG1 | FLAG3));
    How can I do that? I tried in the Something1 function an if clause with if (flags & FLAG1) for example, but it didnt work as expected. Any ideas?

    Thanks
    Y3K Network http://www.y3knetwork.com
    Bringing the software of the future on the net.

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Something1(FLAG1 | FLAG2);

    should do the trick.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    Registered User
    Join Date
    Jun 2002
    Posts
    132
    Hello? I did that as you see.
    My problem lies in the Something1 definition. Here is what I have so far:
    Code:
    void Something1(en1 flags)
    {
       if (flags & FLAG1)
          { do something for flag1 }
       if (flags & FLAG2)
         { do something for flag2 }
       etc etc etc..
    }
    It wont work correctly.
    Y3K Network http://www.y3knetwork.com
    Bringing the software of the future on the net.

  4. #4
    Registered User
    Join Date
    Jun 2002
    Posts
    132
    It worked with defined but for some reason it didnt with enum. In the enum, I dont shift, in the defines I do.
    Whats going on here?? :S
    Y3K Network http://www.y3knetwork.com
    Bringing the software of the future on the net.

  5. #5
    Registered User
    Join Date
    Jun 2002
    Posts
    132
    Oh, it works now.

    Uhh, can you explain me why we used those exact values and any other value wont work?
    Y3K Network http://www.y3knetwork.com
    Bringing the software of the future on the net.

  6. #6
    Registered User
    Join Date
    Jun 2002
    Posts
    132
    Oh...

    Also.. Is there a way to combine that with hex numbers like 0x0400 for example? So i would have in an enum:

    Code:
    enum en1
    {
       flag1 = 0x0400,
       flag2 = 0x0500,
       flag3 = 1<<flag3
    }
    would that work?
    Y3K Network http://www.y3knetwork.com
    Bringing the software of the future on the net.

  7. #7
    Registered User
    Join Date
    Jun 2002
    Posts
    132
    I see. Since I need to use this in a very few situations, I will stick with the shifting thing. Thanks a lot!
    Y3K Network http://www.y3knetwork.com
    Bringing the software of the future on the net.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  4. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  5. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 07:05 PM