Thanks for Your Donations
Latest Releases

Resident Evil Revelations

Metro Last Light

Iron Sky Invasion

Deadly Premonition The Director's Cut

Dragons Dogma Dark Arisen

Star Trek

Dead Island Riptide

Injustice Gods Among Us
Results 1 to 2 of 2

Thread: Booleans Without Short-Circuiting

  1. #1
    ireggae's Avatar
    ireggae is offline Universal Moderator Hall of Famer
    Join Date
    Jan 2011
    Location
    Xbox Live
    Posts
    5,200
    Rep Power
    26

    Default Booleans Without Short-Circuiting

    Booleans Without Short-Circuiting

    C# implements the boolean AND and OR operations like C/C++ with the operators && and ||.
    The 'single character' versions of these (& and |) are used in both languages for bitwise AND and OR operations.
    But in C# the single character versions can be used for boolean logic too, but with a twist.
    The standard boolean operators (&& and ||) try to make your code as efficient as possible, by employing short-circuiting, to skip evaluation where possible.
    If we have a boolean AND expression with two arguments, we know that the expression as a whole is false if one (or both) of the arguments is false. Thus when the first argument is false, the end result is known, and the second argument is never evaluated.
    With OR it's the other way around: if one argument is true, the whole expression is true. Thus if the first argument evaluates to true, the second argument isn't evaluated.
    Consider this sample class:

    class Program
    {
    static bool BooleanExpr1()
    {
    Console.WriteLine("Executing Boolean Expression 1");
    return true;
    }

    static bool BooleanExpr2()
    {
    Console.WriteLine("Executing Boolean Expression 2");
    return true;
    }

    static void Main(string[] args)
    {
    bool b = BooleanExpr1() || BooleanExpr2();
    }
    }

    If we run this program, only "Executing Boolean Expression 1" is printed to the console. Since BooleanExpr1 evaluates to true, the runtime immediately knows that b must be true, so it never evaluates BooleanExpr2().
    This is a nice thing, since it speeds up our program, because expressions whose values have no effect on the result are skipped.
    However, there are times when you do want all expressions to be evaluated for some reason.
    Now you would need to write additional lines of code and introduce temporary variables to make sure all expressions are executed:

    bool b1 = BooleanExpr1();
    bool b2 = BooleanExpr2();
    bool b = b1 || b2;

    But C# gives us the single-character boolean operators. What they do is equal to the two-character boolean operators, but without the short-circuiting. All parts of the boolean expressions are evaluated, no matter if that is neccessary or not.
    Thus, the three lines of code above can be reduced to

    bool b1 = BooleanExpr1() | BooleanExpr2();

    And we see "Executing Boolean Expression 1 Executing Boolean Expression 2" in the console.
    Check Out the Pc Zone! From Movies, Apps, and Mobile to Music and Anime!
    http://www.ps3iso.com/pc-zone/

  2. #2
    romaan's Avatar
    romaan is offline Universal Moderator Athanatos
    Join Date
    Jan 2011
    Location
    Sweet Home
    Posts
    4,188
    Blog Entries
    10
    Rep Power
    29

    Default

    Nice Info
    Would You like to Say Thanks Or +Rep Please Click Icon at Bottom Left of My Post
    Safe Steps For Install Rogero CEX-4.21 CFW
    Run Backup 3.6+ Games on DEX, Noob Friendly With Pictures and Videos
    Get Your User ID For ReActPSN | Tutorial Multiman NTFS Support | Tutorial SlideShow Dynamic PS3 Theme | Tutorial PS3usercheat PKG Guide - For Non-Dongle Users
    Uploading Rules & Clarification
    Credits are not a requirement for posting content. None of the 99% of uploaders on this site post credits. It is not a requirement. Listing credits is by choice if you want to post them or not.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •