Thread: NOP in 'C'?

  1. #1
    Registered User
    Join Date
    Aug 2008
    Location
    Delaware
    Posts
    47

    NOP in 'C'?

    How do I write a No Operation in 'C'?

    The code looks like this.

    Code:
     
    (
    if ((A == 1) && (B == 2)
    {
        NOP;
    }
    else
    if ((C == 3) || (D == 4))
    {
       A = C;
       B = D;
    }
    )

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Just don't do anything. (And for that matter, why even check? Why not go to the next case?)

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    The closest you can get is a null statement:
    Code:
    if ( condition )
      ;
    else if ( condition ) {
      /* Do something */
    }
    My best code is written with the delete key.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Ideally, rewrite the code to not do "reverse checking" - instead:
    Code:
    if ((A != 1) && (B != 2))
    {
       if ((C == 3) || (D == 4))
       {
           ... do stuff ... 
       }
    }
    Alternatively, as one condition [you can array the values in any order, as long as the parenthesized expressions are retained]:
    Code:
    if (((C == 3) || (D == 4)) && (A != 1) && (B != 2))
    {
       ... do stuff ... 
    }
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    What matsp has used there is De Morgan's laws

    Basically if you ever have an expression formed of two sub-expressions, you can modify the expression as follows and end up with an expression that is exactly equivalent.
    Negate both sub-expressions, change the operator between then from 'and' to 'or' or vice-versa, negate the entire expression.
    E.g.
    (a && b) gives (!(!a || !b))
    (x>0 || y<0) gives (!(!(x>0) && !(y<0))) or better yet, simplify that to (!(x<=0 && y>=0))
    !(!c || d==42) gives !!(!!c && !(d==42)). Remove the double !'s and simplify to (c && d!=42)

    The other useful transformations here are to go from:
    Code:
    if (a) {
        if (b) {
            foo();
        }
    }
    
    // to
    
    if (a && b) {
        foo();
    }
    or from:
    Code:
    if (a) {
        foo();
    }
    else if (b) {
        foo();
    }
    
    // to
    
    if (a || b) {
        foo();
    }
    Last edited by iMalc; 01-24-2009 at 03:14 PM.
    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
    Aug 2008
    Location
    Delaware
    Posts
    47
    Quote Originally Posted by tabstop View Post
    Just don't do anything. (And for that matter, why even check? Why not go to the next case?)

    The next case is an else. The point of the code is if the first statement is true, then nothing happens. The else statement is not executed.

    Maybe by making the inverse of the first statement anded with the second is a way to handle it.

    I like the Null Statement as Prelude suggested best. I can comment it and have readable code, where DeMorganizing it makes the code unreadable.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by danlee58 View Post
    The next case is an else. The point of the code is if the first statement is true, then nothing happens. The else statement is not executed.

    Maybe by making the inverse of the first statement anded with the second is a way to handle it.

    I like the Null Statement as Prelude suggested best. I can comment it and have readable code, where DeMorganizing it makes the code unreadable.
    I suggested a form that is not unreadable, I beleive.

    In my programming life, I have never used a null-statement in a if-statement [aside from when macro expansion turns statements into nothing]. It's a bit like starting a statement with the excpetion, like this "except for ice-cream, our deserts are served warm". It is better (more straightforward and clear) to say "all our deserts are served warm, except ice-cream". If you want something to happen only when a != 1 and b != 2, then say so. Don't say "if a = 1 and b = 2 do nothing, else do ..."

    If you feel you must do that, then make sure you make a comment, like you say.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    You don't have to actually apply demorgan's rules if they make the code less clear. Just but the condition in parens and use !, as in :
    Code:
    (
    if ( !( (A == 1) && (B == 2) ) && (C == 3 || D == 4))
    {
       A = C;
       B = D;
    }
    )
    Or if there are additional else's:
    Code:
    if (  !( (A == 1) && (B == 2) )  )
    {
      if(C == 3 || D == 4))
      {
         A = C;
         B = D;
      } else ...
    )
    Last edited by King Mir; 01-24-2009 at 09:29 PM.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  9. #9
    Registered User
    Join Date
    Aug 2008
    Location
    Delaware
    Posts
    47
    Well, my Compiler doesn't seem to like a Null IF statement, unless I'm using the wrong braces.

    matsp, Your suggestion is not unreadable, but this a very complex nested IF-ELSE. The code that I showed was a simplified example. The execution has to exit the IF-Else at several points where a condition is true, but continue to fall through where conditions are false. I'm concerned that I'll never get it right if I make too many changes to the Intuitive statements.

    I might have to do that, if the Compiler doesn't accept the Null If.

  10. #10
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by danlee58 View Post
    The execution has to exit the IF-Else at several points where a condition is true but continue to fall through where conditions are false..
    Isn't that just the logical flow? Also, in your first example you are not doing anything of the sort. If you want to exit, break. Otherwise the apple hits the ground. The NOP is definitely a no-op...
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by danlee58 View Post
    Well, my Compiler doesn't seem to like a Null IF statement, unless I'm using the wrong braces.

    matsp, Your suggestion is not unreadable, but this a very complex nested IF-ELSE. The code that I showed was a simplified example. The execution has to exit the IF-Else at several points where a condition is true, but continue to fall through where conditions are false. I'm concerned that I'll never get it right if I make too many changes to the Intuitive statements.

    I might have to do that, if the Compiler doesn't accept the Null If.
    If your compiler doesn't accept ; as the true part of an if, it is extremely extremely broken. I would venture that it is highly more likely that you have messed up the braces.

  12. #12
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by danlee58 View Post
    Well, my Compiler doesn't seem to like a Null IF statement, unless I'm using the wrong braces.

    matsp, Your suggestion is not unreadable, but this a very complex nested IF-ELSE. The code that I showed was a simplified example. The execution has to exit the IF-Else at several points where a condition is true, but continue to fall through where conditions are false. I'm concerned that I'll never get it right if I make too many changes to the Intuitive statements.

    I might have to do that, if the Compiler doesn't accept the Null If.
    Prelude's example should work. If you tried that and it didn't, post the smallest sample of code that demonstrates the failure.

    The normal solution is still to negate the do nothing condition, and put the other alternatives in that if block.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  13. #13
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    I can't see what's so hard?
    Instead of:
    Code:
    if ( condition A )
       Do Nothing;
    else if ( condition B )
       Do Something;
    It would make so much more sense to do:
    Code:
    if ( (Not condition A) && (condition B) )
       Do Something;
    i.e. what matsp said.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
    if ( (A != 1 && B != 2) &&
         (C == 3 || D == 4) )
    {
       A = C;
       B = D;
    }
    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.

  15. #15
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    @matsp & Elysia

    You don't seem to be applying Demorgan's correctly:
    !( (A == 1) && (B == 2) )

    is the same as

    A!=1 || B!=2
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. dll to encrypt packet sends
    By splintter in forum C++ Programming
    Replies: 22
    Last Post: 04-20-2008, 09:00 PM
  2. Which one is "quicker" ?
    By AloneInTheDark in forum C# Programming
    Replies: 2
    Last Post: 02-08-2008, 09:23 PM
  3. nop - revisited
    By oioi in forum Tech Board
    Replies: 4
    Last Post: 12-28-2004, 10:18 AM
  4. Is there anything wrong with my LCD code?
    By ooicheesan in forum C++ Programming
    Replies: 1
    Last Post: 10-18-2004, 01:13 AM
  5. nop
    By Eibro in forum Tech Board
    Replies: 3
    Last Post: 09-02-2002, 08:07 PM