Thread: logical operator

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    21

    logical operator

    Code:
            if ( opcode == (0x0) || (0x1)|| (0x2) || (0x4)|| (0x6)|| (0x1)) {  
              
                //blah
    
                
            }
    I get a warning saying: Use of logical "||" with constant operand. And it doesn't work the way I intend.

    Is there a proper syntax for this kind of operation? I just want to see if the variable "opcode" is equivalent to either one of the hexadecimals on the right.

    Thanks

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    This really should go into the "Frequently Asked Rank Beginner Question" list.

    The proper syntax for "this type of operation" is
    Code:
    if (opcode == 0x0 || opcode == 0x1 || opcode == 0x2 || opcode == 0x4 || opcode == 0x6 || opcode == 0x1)
    {
         // blah
    }
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Apr 2012
    Posts
    21
    yeah i just tried that. thanks.

  4. #4
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    IMO, a switch-case construct is better in this situation to make the code more compact and legible.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    So nobody else noticed the 0x01 repeated?

    FYI, there is an old trick that allows you to check for equality against a bunch of possible values from 0 to 31 all in one hit. It goes like this:
    Code:
    if (opcode < 32 && ((1 << opcode) & ((1<<0) | (1<<1) | (1<<2) | (1<<4) | (1<<6))) != 0)
    {
        // blah
    }
    And of course it doesn't look that pretty, but it can be tidied up by writing a bunch of macros for it.

    But yeah this is not something you want to use before you understand it, and is premature optimisation here anyway.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by iMalc View Post
    So nobody else noticed the 0x01 repeated?
    Yeah, I noticed it, but I left it in my response anyway. For all I knew, opcode was a volatile variable
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Curious Problem using Logical Or operator
    By Chance in forum C Programming
    Replies: 2
    Last Post: 12-11-2010, 11:16 PM
  2. Preincrement and Logical Operator
    By NKP in forum C Programming
    Replies: 2
    Last Post: 01-26-2010, 02:14 AM
  3. logical operator OR not working?
    By echo49 in forum C Programming
    Replies: 4
    Last Post: 09-12-2006, 04:26 AM
  4. logical operator !
    By scrapper in forum C Programming
    Replies: 1
    Last Post: 02-18-2005, 08:30 AM
  5. Logical Operator not working?
    By xshapirox in forum C++ Programming
    Replies: 2
    Last Post: 09-27-2004, 05:21 AM