SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    Apr 2009
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question what does this construct do {flag = flag | a;}?

    I'm trying to reuse some old code I wrote and forget where I found the syntax(I'm no expert - probably fit better in the 'dabbler' category).

    Code:
    if(pmIn != ""){flag = flag | a;}
    Is this a short cut for the ternary operator? As in
    Code:
    flag = (pmIn != "") ? flag : a
    or
    Code:
    if(pmIn != ""){flag = flag;} else {flag = a;}
    Thanks

  2. #2
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,812
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    No. It would be if it had || between the two values though instead of the single |.

    One | by itself is a bitwqise or operator and so the value returned from flag | a will be a value with bits set on where ever either of those two values had the bit set on.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  3. #3
    SitePoint Member
    Join Date
    Apr 2009
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, now I remember what I was doing. Here's the whole snippet

    Code:
    //get combinations of times
      //
      var flag = 0;
      var a = 1;
      var b = 2;
      var c = 4;
    	var d = 8;
    
      if(amIn != ""){flag = flag | a;}
      if(amOut != ""){flag = flag | b;}
      if(pmIn != ""){flag = flag | c;}
      if(pmOut != ""){flag = flag | d;}
    
      switch(flag)
      {
        case 3:
          bb = cTFD_03(amIn, amOut);	//, hrs);
          break;
        case 9:
          cTFD_09(amIn, pmOut);	//, hrs);
          break;
        case 12:
          cTFD_12(pmIn, pmOut);	//, hrs);
          break;
        case 15:
          cTFD_15(amIn, amOut, pmIn, pmOut);	//, hrs);
          break;
    		default:
          alert(tvError);
          break;
      }
    So when different combinations of the if's are true it produces different values for flag, then I use that value in the switch statement.
    Just proves what a waste of time comments are.

  4. #4
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,702
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by pnguine View Post
    So when different combinations of the if's are true it produces different values for flag, then I use that value in the switch statement.
    Just proves what a waste of time comments are.
    The added complexity doesn't provide much of any benefit either.
    Have a look at the following, which performs the same job in an easier to understand manner:

    Code javascript:
    if (amIn && amOut && pmIn && pmOut) {
        cTFD_15(amIn, amOut, pmIn, pmOut);
    } else if (amIn && amOut) {
        cTFD_03(amIn, amOut);
    } else if (amIn && pmOut) {
        cTFD_09(amIn, pmOut);
    } else if (pmIn && pmOut) {
        cTFD_12(pmIn, pmOut);
    }

    Then it's just a matter of giving the functions suitably descriptive names.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  5. #5
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,812
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Also bitwise operators in JavaScript are extremely inefficient - unlike most other languages where they are amongst the fastest operations, in JavaScript they are the slowest by a long way.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  6. #6
    SitePoint Member
    Join Date
    Apr 2009
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the speedy help and tips everyone. I'll be sure to try here again next time I forget what I've done (or need help doing something I've never done.)


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
  •