Can anyone explain this line of code to me?

Discussion in 'Tech Talk' started by devildog2067, Aug 15, 2007.

  1. devildog2067

    devildog2067

    Messages:
    16,710
    Likes Received:
    2,092
    Joined:
    Apr 20, 2005
    Location:
    Chicago
    I'm a physicist, not a computer scientist, so this is absolute Greek to me.

    HcalDetId::HcalDetId(HcalSubdetector subdet, int tower_ieta, int tower_iphi, int depth) : DetId(Hcal,subdet) {
    // (no checking at this point!)
    id_ |= ((depth&0x7)<<14) |
    ((tower_ieta>0)?(0x2000|(tower_ieta<<7)):((-tower_ieta)<<7)) |
    (tower_iphi&0x7F);
    }

    id_ is of type uint_32 declared in the header for DetId.

    Not only do I not really understand what the bitwise shift operator << or the bitwise or/assign operator |= do, I also don't understand the operator precedence.

    I know there's some computer scientists here, any help would be appreciated! Thanks.
     
  2. Duncan223

    Duncan223 Guest

    Messages:
    237
    Likes Received:
    1
    Joined:
    Nov 13, 2006

  3. Brown Hawk

    Brown Hawk CLM

    Messages:
    1,695
    Likes Received:
    0
    Joined:
    Mar 19, 2005
    Location:
    Ohio
    Off hand, it looks like a C++ object oriented procedure. (There are other possibilities.)

    A bitwise shift is moving the bits to the right (>>) or left (<<) that many positions. Depending on the machine and the compilier, the bits are either left as zeros or the moved bits are moved around to the last position. (Probably the former, which is way more common, but I have seen the latter.)

    Example of 1: 100111 <<2 is 011100
    Example of 2: 100111 <<2 is 011110

    Again offhand, looks like they might be moving some 8 or 16 bit words into 32 bit words and doing some other calcs in one step instead of multiple steps.

    Hawk
     
  4. devildog2067

    devildog2067

    Messages:
    16,710
    Likes Received:
    2,092
    Joined:
    Apr 20, 2005
    Location:
    Chicago
    It is, I suppose I should have been a bit more specific in terms of my understanding of the procedure. I do quite a lot of programming but I'm not a programmer; all of my experience is on-the-fly rather than the result of any sort of curriculum. Also, my recent experience has been in FORTRAN so object-oriented isn't all that natural to me.

    OK, so with the more specific questions:

    depth is an int. Adding the & means address, right? Then 0x7 converts it into a hex value.

    Then, there's an if:then:else statement in that weird contracted C++ syntax, ? : . I understand the statement tower_ieta > 0, but what's 0x2000|(tower_ieta<<7) ? Isn't | OR? I don't understand the overloading here. The statement has to return a true/false value, but what's it comparing? tower_ieta is an int, you shift it bitwise 7 bits, but then what? I don't understand what is OR comparing between the two things here.

    Finally, the sequence of the operators is |= | ? : | . Do they get evaluated from left to right or what?
     
  5. Brown Hawk

    Brown Hawk CLM

    Messages:
    1,695
    Likes Received:
    0
    Joined:
    Mar 19, 2005
    Location:
    Ohio
    C++ is not one of the four languages I'm using every day, so if someone more current wants to chime in, it won't hurt my feelings.

    "0x" means a hexidecimal number. 0x2000 = 2000 Hex or 8192 decimal.

    "|=" combines bitsets using OR and stores them in id_

    ":" is an initializer where a : b, a is initialized to value b
    ETA forget this one, it combines with the ? and is described below. Hawk

    "|" ia a bitwise inclusive or

    "||" is logical OR

    "&" is a bitwise AND operator

    "&&" is a logical AND operator

    Operational order is left to right. But watch out for the ().

    So it looks like a procedure to do straight bit manipulation. It doesn't return a true/false, it returns a number.

    Hopefully, that allows you to make more sense of it.

    Hawk

    ETA Sorry, the ?: mandates that the leftmost operand is ALWAYS evaluated first of three, and if zero the rightmost is evaluated and the middle is ignored, if the leftmost is non-zero, the middle is evaluated and the third is ignored.

    The syntax is: leftmost ? middle : right
    Translation: If leftmost then middle else right

    Hawk
     
  6. Brown Hawk

    Brown Hawk CLM

    Messages:
    1,695
    Likes Received:
    0
    Joined:
    Mar 19, 2005
    Location:
    Ohio
    So as described above this would be:


    Take the "depth" bit manipulation:
    which is then Bitwise ORed with the "tower if:then:else" value
    and that combo:
    is then bitwise ORed with the "tower_iphi" bitwise manipulation.

    The result is then bitwise ORed with id_ and the final result is stored in id_.

    Simple really.




























































    NOT!

    Hawk
     
  7. zoyter2

    zoyter2 Yeah, so what?

    Messages:
    4,697
    Likes Received:
    9
    Joined:
    Jun 25, 2002
    Location:
    Anniston, Alabama
    It means the assets are all in place, the plan is online, the operation has a green light, and to wait for the signal to advance.

    WTF did you do with the decoder ring. I told you NEVER to take it off!:supergrin:
     
  8. ThetaPi

    ThetaPi

    Messages:
    27
    Likes Received:
    1
    Joined:
    Dec 3, 2005
    Location:
    Northwest Arkansas
    // Cleaned up version, does the same thing and easier to understand.

    //--------------------

    HcalDetId::HcalDetId(HcalSubdetector subdet, int tower_ieta, int tower_iphi, int depth) : DetId(Hcal,subdet) {
    // (no checking at this point!)
    FIRST = (depth & 0x0007) << 14;
    if(tower_ieta > 0) {
    SECOND = 0x2000 | (tower_ieta << 7);
    }
    else
    SECOND = (-tower_ieta) << 7;
    }
    THIRD = tower_iphi & 0x007F;
    id_ |= FIRST | SECOND | THRID;
    }

    //--------------------

    Here is a table of operator precedence: http://www.phim.unibe.ch/comp_doc/c_manual/C/CONCEPT/precedence.html.

    There are a few things wrong with this code. Number one, whoever wrote it, did not document it. Number two, it may

    not be portable. Number 3, they do not check the values put into the function.

    Number 1 is just someone being lazy.
    Number 2 is just someone not being careful.
    Number 3 is just someone being lazy. If your program crashes here, it might have a bad value sent to it. They assume

    values will be good, but we all know the saying about assuming.

    First of, the code mostly takes values and does bitwise operations on them. I split the operation up a bit to make it a bit more easy to understand. You can make it a one-liner (someone did), but it is hard maintaining the code.

    FIRST is the value of depth bitwise and'd with 0x0007, then shifted left 14 places. This code is not portable because it makes the assumption of 16 bit integers. Some systems have 32 bit integers as default. Depends mostly on the hardware and what compiler is used.

    SECOND depends on the sign of tower_ieta. It either shifts the value of tower_eita 7 places to the left and bitwise or's it with 0x2000, or it makes it positive and shifts the value of tower_eita 7 places to the left. It has to make it positive, because the operation would not operate as expected on a negative number.

    THIRD is the value of tower_iphi bitwise and'd with 0x007F.

    The result, id_ is id_, FIRST, SECOND, and THIRD bitwise or'd together and assigned to id_.

    If you give me some values sent to the function, I can work a few out to show you what it does.

    Jeremy.
    LAT III
    CHEMISTRY/BIOCHEMISTRY LIBRARY
     
  9. Douglas in CT

    Douglas in CT Millennium Member

    Messages:
    1,417
    Likes Received:
    0
    Joined:
    Feb 8, 1999
    Location:
    What does your CT Permit say?
  10. woodasptim

    woodasptim

    Messages:
    2,296
    Likes Received:
    67
    Joined:
    Feb 7, 2007
    Location:
    NE Arkansas
    Will this get us on some sort of watch list?














    j/k - it's not one of the languages that I use regularly, sorry.