Results 1 to 5 of 5

Thread: and, or - 'single' characters?

  1. #1
    Join Date
    Jun 2008
    Posts
    589
    Thanks
    13
    Thanked 54 Times in 54 Posts
    Blog Entries
    1

    Default and, or - 'single' characters?

    In some programs, I have seen code in which the programmer does this:

    Code:
    if (i&1) {
    do_somthing();
    }
    Why is there only one ampersand and not two? If anyone could please explain how the 'single' character of operators work, including '|' and '&' or any others not listed please?

    -magicyte

  2. #2
    Join Date
    Mar 2007
    Location
    Currently: New York/Philadelphia
    Posts
    2,735
    Thanks
    3
    Thanked 519 Times in 507 Posts

    Default

    Perhaps it would help if you told us where you've seen such syntax. As far as I'm aware, that's not a valid logical conditional.

  3. #3
    Join Date
    Jun 2008
    Posts
    589
    Thanks
    13
    Thanked 54 Times in 54 Posts
    Blog Entries
    1

    Default

    I've seen it in C++ tutorial books. Sorry- I really shouldn't have posted this here, huh.

    ^ I've also seen this done in C++ code and bits of JavaScript. ^

    Here is one link:

    http://www.cprogramming.com/tutorial...operators.html <- Don't understand this

    if you understand what I mean, is it possible in JavaScript or PHP (client-side / server-side)?

    Again, I shouldn't have posted this thread here. Mod/admin, please move/relocate thread... Thank you!

    -magicyte
    Last edited by magicyte; 11-19-2008 at 05:27 AM.

  4. #4
    Join Date
    Mar 2008
    Posts
    122
    Thanks
    17
    Thanked 5 Times in 5 Posts

    Default

    Ive seen this in php! and i was puzzled as to what it was. (One ampersand).

  5. #5
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Those are not logical operators but bitwise operators. In weakly-typed languages where 1 is coerced to true and 0 to false they are mostly equivalent, but lack short-circuit behaviour. As such, they will perform worse in some situations and should not be used where a logical operator is intended.

    In languages where the logical operators yield the terminating value, they will also result in a different return value in most cases:
    Code:
    // Javascript
    true && false // false
    true & false // 0
    ({}) || undefined // ({}) (only the first operand matters because the result would be the same no matter the second; the object is the terminating value, and is returned)
    ({}) | undefined // 0 (the object is coerced to 1, and undefined to 0)
    
    function foo() {
      return true;
    }
    
    function bar() {
      return false;
    }
    
    bar() && foo() // false; only bar is called
    bar() & foo() // 0; both bar and foo are called
    In strongly-typed languages, using a bitwise operator on boolean values will yield an error:
    Code:
    -- Haskell
    import Data.Bits
    
    1 .&. 2 :: Int -- 0
    
    True .&. False :: Int
    -- Couldn't match expected type `Int' against inferred type `Bool'
    -- In the expression: True .&. False :: Int
    Last edited by Twey; 12-11-2008 at 03:55 PM.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

Bookmarks

Posting Permissions

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