# Bitwise efficiency

Hi,

I’m currently trying to figure out how bitwise operators can make code more efficient instead for example if statements.

void count(pnt const* pnts, const int n, unsigned cnt[8]) {
for (int i = 0; i < 8; ++i)
cnt[i] = 0;
for (int i = 0; i < n; ++i) {
if (pnts[i][0] >= 0.0f && pnts[i][1] >= 0.0f && pnts[i][2] >= 0.0f)
++cnt[7];
else if (pnts[i][0] >= 0.0f && pnts[i][1] >= 0.0f && pnts[i][2] < 0.0f)
++cnt[3];
else if (pnts[i][0] >= 0.0f && pnts[i][1] < 0.0f && pnts[i][2] >= 0.0f)
++cnt[5];
else if (pnts[i][0] >= 0.0f && pnts[i][1] < 0.0f && pnts[i][2] < 0.0f)
++cnt[1];
else if (pnts[i][0] < 0.0f && pnts[i][1] >= 0.0f && pnts[i][2] >= 0.0f)
++cnt[6];
else if (pnts[i][0] < 0.0f && pnts[i][1] >= 0.0f && pnts[i][2] < 0.0f)
++cnt[2];
else if (pnts[i][0] < 0.0f && pnts[i][1] < 0.0f && pnts[i][2] >= 0.0f)
++cnt[4];
else
++cnt[0];
}
}

Instead of the code above I tried

void countNew(pnt const* pnts, const int n, unsigned cnt[8])
{
for (int i = 0; i < 8; ++i)
cnt[i] = 0;

``````///|111...|: Octant 1 (-, -, -)
///|110...|: Octant 2 (-, -, +)
///|101...|: Octant 3 (-, +, -)
///|100...|: Octant 4 (-, +, +)
///|011...|: Octant 5 (+, -, -)
///|010...|: Octant 6 (+, -, +)
///|001...|: Octant 7 (+, +, -)
///|000...|: Octant 8 (+, +, +)

for (int i= 0; i < n; ++i)
{
cnt[0] += (((int)pnts[i][0] >> fullShift) & 1) && (((int)pnts[i][1] >> fullShift) & 1) && (((int)pnts[i][2] >> fullShift) & 1); //If all points are negative, cnt[0]++
cnt[1] += (((int)pnts[i][0] >> fullShift) & 1) && (((int)pnts[i][1] >> fullShift) & 1) && (~((int)pnts[i][2] >> fullShift) & 1); //...
cnt[2] += (((int)pnts[i][0] >> fullShift) & 1) && (~((int)pnts[i][1] >> fullShift) & 1) && (((int)pnts[i][2] >> fullShift) & 1);
cnt[3] += (((int)pnts[i][0] >> fullShift) & 1) && (~((int)pnts[i][1] >> fullShift) & 1) && (~((int)pnts[i][2] >> fullShift) & 1);
cnt[4] += (~((int)pnts[i][0] >> fullShift) & 1) && (((int)pnts[i][1] >> fullShift) & 1) && (((int)pnts[i][2] >> fullShift) & 1);
cnt[5] += (~((int)pnts[i][0] >> fullShift) & 1) && (((int)pnts[i][1] >> fullShift) & 1) && (~((int)pnts[i][2] >> fullShift) & 1);
cnt[6] += (~((int)pnts[i][0] >> fullShift) & 1) && (~((int)pnts[i][1] >> fullShift) & 1) && (((int)pnts[i][2] >> fullShift) & 1);
cnt[7] += (~((int)pnts[i][0] >> fullShift) & 1) && (~((int)pnts[i][1] >> fullShift) & 1) && (~((int)pnts[i][2] >> fullShift) & 1);
}
``````

}

(typedef float pnt[3];
#define fullShift sizeof(int) * 8 - 1)

However, the second coce which I thought would be more effiecient is actually slower.

Does anybody have any thought on why that is?

Thank you!