Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Jun 2006
    Posts
    30
    Thanks
    2
    Thanked 1 Time in 1 Post

    C++ - Hexadecimal - OR

    I'm working on some stuff at work and have a question about how hex values and the OR operator work.

    I have this line of code:

    Code:
    locPROFILE tgPrfGNDTRKREQDC           ={DCSPKG_BASESVC_GROUND,         TRACKCAT_C,     PKG_LBL,        (locSIGTRK | TYP_SIGREQ | TYP_DC)};
    I then have these #defines:

    Code:
    #define locSIGTRK      (TYP_PUPTRK | TYP_DELTRK)
    #define TYP_DELTRK         (long) 0x00000200
    #define TYP_PUPTRK         (long) 0x00010000
    #define TYP_SIGREQ         (long) 0x00400000
    #define TYP_DC             (long) 0x00000040
    Also, here is the locPROFILE struct:
    Code:
    typedef struct
    {
        short  baseServiceCode;     // Base Service category
        short  sTrackCat;           // Tracking category
        long   lLabelUsage;         // Label Usage
        long   lFeatures;           // Features Category
    } locPROFILE;
    What would the value be for lFeatures from the first line and why? I assume it has to do with bit arithmetic...is that correct?

    Thanks!!!

  • #2
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    Code:
        0x00000200
        0x00010000
        0x00400000
      | 0x00000040
    --------------
        0x00410240
    The bitwise OR operator takes each bit in the two numbers, and if either of the numbers involved has a one in that bit, the resulting number has a one in that bit. Otherwise the resulting number has a zero in that bit position.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #3
    New Coder
    Join Date
    Jun 2006
    Posts
    30
    Thanks
    2
    Thanked 1 Time in 1 Post
    Ok. I think I knew that, but I didn't believe myself. Thanks for the clarification! That helps a lot.

    So what if it was something like this?
    Code:
        0x00020000
        0x00010000
        0x00400000
        0x00200000
    How would the OR work there since some of the bits have overlapping values?

  • #4
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    Remember that these are hexadecimal numbers, no binary. There's actually no bit overlap in them, because 0x08 = 0b00001000, 0x04 = 0b00000100, 0x02 = 0b00000010, 0x01 = 0b00000001. So to take your examples,
    Code:
        0x00020000
        0x00010000
        0x00400000
      | 0x00200000
    --------------
        0x00630000
    A little more interesting would be to look at numbers that have more than one bit overlapping:
    Code:
        0x09 = 0b00001001
      | 0x05 = 0b00000101
    --------------
        0x0d = 0b00001101
    Each bit is OR:ed separately, so the result contains a one in each bit position that has a one in either of the operands, only having a zero in that bit position if both operands had a zero in that position.

    The rule for AND are similar, if you just swap one and zero. In other words: Each bit is AND:ed separately, so the result contains a zero in each bit position that has a zero in either of the operands, only having a one in that bit position if both operands had a one in that position.

    Both of these can be put in the long form with any number of operands.

    There's another binary bitwise operator however: XOR. It works as follows: Each bit is XOR:ed separately, so the result contains a one in each bit position that has a one in one operand and zero in the other operand, and a zero in each bit position where either both operands had a one or both operands had a zero.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • Users who have thanked liorean for this post:

    Fatmumuhomer (10-24-2008)

  • #5
    New Coder
    Join Date
    Jun 2006
    Posts
    30
    Thanks
    2
    Thanked 1 Time in 1 Post
    Ok. That makes sense then. Thanks for the help!

  • #6
    Senior Coder
    Join Date
    Apr 2003
    Location
    England
    Posts
    1,192
    Thanks
    5
    Thanked 13 Times in 13 Posts
    Just convert them into binary and it becomes obvious (hex is just a different representation of the number in binary anyway)

    for example
    0xA = 1010
    0x6 = 0110

    0x0A | 0x06
    = 1010 | 0110
    = 1110
    = 0x0E


    For an example of combining bitwise flags, let's use some arbitrary style information
    Assume
    1000 = 0x08 = visible
    0100 = 0x04 = dashed
    0010 = 0x02 = red text
    0001 = 0x01 = blue background

    0x0A = 1010 = 1000 | 0010 = visible, red text
    0x06 = 0110 = 0100 | 0010 = dashed, red text
    0x0A | 0x06 = visible, dashed, red text (the options are just combined)
    Last edited by ghell; 10-24-2008 at 12:34 PM.

  • Users who have thanked ghell for this post:

    Fatmumuhomer (10-24-2008)

  • #7
    New Coder
    Join Date
    Jun 2006
    Posts
    30
    Thanks
    2
    Thanked 1 Time in 1 Post
    Makes sense. Thanks, ghell!

    I ended up using this stuff to figure out that the bug was in the data I was given and not the code. Gotta love that.


  •  

    Posting Permissions

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