Fatmumuhomer

10-22-2008, 09:37 PM

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:

locPROFILE tgPrfGNDTRKREQDC ={DCSPKG_BASESVC_GROUND, TRACKCAT_C, PKG_LBL, (locSIGTRK | TYP_SIGREQ | TYP_DC)};

I then have these #defines:

#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:

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!!!

liorean

10-22-2008, 11:45 PM

0x00000200

0x00010000

0x00400000

| 0x00000040

--------------

0x00410240The 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.

Fatmumuhomer

10-23-2008, 02:39 PM

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?

0x00020000

0x00010000

0x00400000

0x00200000

How would the OR work there since some of the bits have overlapping values?

liorean

10-23-2008, 03:06 PM

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,

0x00020000

0x00010000

0x00400000

| 0x00200000

--------------

0x00630000A little more interesting would be to look at numbers that have more than one bit overlapping:

0x09 = 0b00001001

| 0x05 = 0b00000101

--------------

0x0d = 0b00001101Each 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.

Fatmumuhomer

10-23-2008, 03:14 PM

Ok. That makes sense then. Thanks for the help!

ghell

10-24-2008, 12:27 PM

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)

Fatmumuhomer

10-24-2008, 07:10 PM

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. :)