Note that there are some explanatory texts on larger screens.

plurals
  1. POAny significant performance improvement by using bitwise operators instead of plain int sums in C#?
    primarykey
    data
    text
    <p>I started working with C# a few weeks ago and I'm now in a situation where I need to build up a "bit set" flag to handle different cases in an algorithm. I have thus two options:</p> <pre><code> enum RelativePositioning { LEFT = 0, RIGHT = 1, BOTTOM = 2, TOP = 3, FRONT = 4, BACK = 5 } pos = ((eye.X &lt; minCorner.X ? 1 : 0) &lt;&lt; (int) RelativePositioning.LEFT) + ((eye.X &gt; maxCorner.X ? 1 : 0) &lt;&lt; (int) RelativePositioning.RIGHT) + ((eye.Y &lt; minCorner.Y ? 1 : 0) &lt;&lt; (int) RelativePositioning.BOTTOM) + ((eye.Y &gt; maxCorner.Y ? 1 : 0) &lt;&lt; (int) RelativePositioning.TOP) + ((eye.Z &lt; minCorner.Z ? 1 : 0) &lt;&lt; (int) RelativePositioning.FRONT) + ((eye.Z &gt; maxCorner.Z ? 1 : 0) &lt;&lt; (int) RelativePositioning.BACK); </code></pre> <p>Or:</p> <pre><code> enum RelativePositioning { LEFT = 1, RIGHT = 2, BOTTOM = 4, TOP = 8, FRONT = 16, BACK = 32 } if (eye.X &lt; minCorner.X) { pos += (int) RelativePositioning.LEFT; } if (eye.X &gt; maxCorner.X) { pos += (int) RelativePositioning.RIGHT; } if (eye.Y &lt; minCorner.Y) { pos += (int) RelativePositioning.BOTTOM; } if (eye.Y &gt; maxCorner.Y) { pos += (int) RelativePositioning.TOP; } if (eye.Z &gt; maxCorner.Z) { pos += (int) RelativePositioning.FRONT; } if (eye.Z &lt; minCorner.Z) { pos += (int) RelativePositioning.BACK; } </code></pre> <p>I could have used something as <code>((eye.X &gt; maxCorner.X) &lt;&lt; 1)</code> but C# does not allow implicit casting from bool to int and the ternary operator was similar enough. My question now is: is there any performance improvement in using the first version over the second?</p> <p>Thank you<br /> Tommaso</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload