• 15
• 15
• 11
• 9
• 10

[C#] Bits and OR operator problem

This topic is 2936 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

I am currently trying to figure out a number (index) based on the following:
Quote:
 The results of evaluating the table condition (a bit) is copied to one of the bits which form the index of the first sub-node crossed. When a condition is true, the bit associated is set to 1 otherwise it is set to 0.  The whole process can be implemented using the "|" (OR) operator to combine the necessary bits.
The table below shows what bits I have to evaluate and produce the index from: This is what I have written based off that table:
            int index = 0;
float max = Math_Util.Max3(t0x, t0y, t0z);

if (t0x == max)
{
// YZ entry plane
if (tMy < t0x)
{
// 1
}
if (tMz < t0x)
{
// 2
}

}
if (t0y == max)
{
// XZ entry plane
if (tMx < t0y)
{
// 0
}
if (tMz < t0y)
{
// 2
}
}
if (t0z == max)
{
// XY entry plane
if (tMx < t0z)
{
// 0
}
if (tMy < t0z)
{
// 1
}
}


Unfortunately I am confused as to how I should combine the bit affected with if statement correctly. How can I use the OR operator in order to produce the correct index? Thank you.

Share on other sites
int val = 0;
val |= bit;

Is that what you are looking for? Turn a particular bit on?

val &= ~bit; //Will turn a bit off.

Share on other sites

            int index = 0;            float max = Math_Util.Max3(t0x, t0y, t0z);            // |= bit  (Turns bit on)            // &= ~bit (Turns bit off)            if (t0x == max)            {                // YZ entry plane                 index = tMy < t0x ? index |= 1 : index &= ~1;   // Bit affected = 1                index = tMz < t0x ? index |= 2 : index &= ~2;   // Bit affected = 2            }            if (t0y == max)            {                // XZ entry plane                index = tMx < t0y ? index |= 0 : index &= ~0;                index = tMz < t0y ? index |= 2 : index &= ~2;            }            if (t0z == max)            {                // XY entry plane                index = tMx < t0z ? index |= 0 : index &= ~0;                index = tMy < t0z ? index |= 1 : index &= ~1;            }

I'm not entirely sure what the original source is wanting me to do though, as the way it is explained only serves to confuse me.

If anyone could explain it to me then I would be grateful.

It is on page 4, under section 3.1 of this paper.

Share on other sites
Quote:
 Original post by Spa8nkyFrom your advice I have changed the code to the following:*** Source Snippet Removed ***I'm not entirely sure what the original source is wanting me to do though, as the way it is explained only serves to confuse me.
The code snippet you're looking for would be
            if (t0x == max)            {                // YZ entry plane                 if( tMy < t0x ) index |= 2; // Bit 1 affected                if( tMz < t0x ) index |= 4; // Bit 2 affected            }            if (t0y == max)            {                // XZ entry plane                if( tMx < t0y ) index |= 1; // Bit 0 affected                if( tMz < t0y ) index |= 4; // Bit 2 affected            }            if (t0z == max)            {                // XY entry plane                if( tMx < t0z ) index |= 1; // Bit 0 affected                if( tMy < t0z ) index |= 2; // Bit 1 affected            }
because when you say "bit no 1" you specify a bit index, addressing a single bit in a binary number. The | operator expects binary numbers, so you have to convert "bit index" to "binary number" (see below). Moreover, you don't need to clear individual bits, because you've cleared them all at initialization (int index = 0) and you have never a condition that must undo a previous set.

If you look at the following examples
010 = 000002
110 = 000012
210 = 000102
410 = 001002
810 = 010002
you can see that bit in a binary number (subscribed with 2) stands for a power of 2 (btw, that's the nature of the binary number base).

If you have several bits set, then you add their decimal values to yield in the final decimal value, e.g.
00112 = 210 + 110 = 310

So the possible results of setting bits 0 to 2 are in the interval [010, 710], in principle. However, your code never sets all 3 bits, so it yield in values from the interval [010, 610] only. (The article text mentions that, too.)

EDIT: If you want to let the compiler do the bit index to number conversion, then you can use the left shift operator:
int number = 1 << bitIndex;
would do the trick. It starts with bit 0 set (i.e. number == 0012) and shifts that digit bitIndex many places to the left, filling the right places with 0's. So, if bitIndex == 0, the result is still 0012; if bitIndex == 1 the result will be 0102; and so forth.

Share on other sites
Thanks very much haegarr. The power of 2 makes sense, now all my bits are now(2^Bit affected).

The nature of combining the affected bit with 0 and 1 (if statement result) confused the heck out of me. I guess then the bit remains unaffected if the if statement is false.

Share on other sites
Um, of course. When you don't write an 'else' to match an 'if', there is no code that gets executed specifically when the condition is false. If no code is executed, no bits can be affected. :)