Tina
Tina

Reputation: 49

Marching cube, bitwise and and OR

In order to understand marching cube algorithm, I followed this page: http://paulbourke.net/geometry/polygonise/

I have some questions: what do bitwise & and | mean?, and how they will work with edge table to find the correct tringles?

Upvotes: 1

Views: 351

Answers (2)

Gijs Den Hollander
Gijs Den Hollander

Reputation: 723

Not going to read that text thats way to long. But here you can find how bit operators work https://en.wikipedia.org/wiki/Bitwise_operations_in_C. `

cubeindex |= 1 --> cubeindex = cubeindex | 1. 

for example cubeindex = 26 (binary 11010) and 1 (binary 00001)

11010  | 00001 = 11011

Here you ad one 26->27.

For the followingedgeTable[cubeindex] & 1:

For example cubeindex = 17 (binary 10001) \

10001 &  00001  = 00001

This becomes 1. Used in the if statement, this just checks if the number edgeTable[cubeindex] contains the bit 00001 and returns true or false accordingly.

hope this helps :)

Cheers

Upvotes: 1

ScottK
ScottK

Reputation: 1556

Bitwise & (and) and | (or) operate on all the bits within an integer value. It operates on each bit independently, and is often used with you have a set of boolean truth values (aka flags) that indicate the state of various objects. It can also be used (as in your example) or test a specific flag while not modifying the others.

The bits in an integer represent powers of two values. If that bit is set to true, then that power of two is included in its value. If that bit is false, then it is no included. The least significant bit b0 represents 2^0, bit 1 represents 2^1, and so on.

For example, the value 5 = 101 in binary because 5 = 2^2 + 2^0 = 4 + 1 = 5

Bitwise OR works by setting the bits in the result to 1 if either of the operands contains a one in that position. Bitwise AND workds by setting each bit to 1 if and only if both operands have a 1 in that position.

For example:

Bitwise OR:     5 | 9 = 0101 | 1001 = 1101
Bitwise AND:    5 & 9 = 0101 & 1001 = 0001

Using these operators, if the set of bit values in cubeindex represent a set of states(true or false values), then you can use the same bitwise operands to test and if a specific state is true, while ignoring the other bits.

In your example:

cubeindex |= 1

sets the least significant bit in cubeindex to true (1) regardless of what it was before. This is because the bitwise OR of any bit value to 0, is the same bit value, while the bitwise OR of any bit value of 1, is always 1 and so is the same as setting that bit to 1 regardless of its previous stae. It is equivalent to:

cubeindex = cubeindex | 000000000000000001 = cubeindex with b0 set to 1

The logical AND is being used to test if the least significant bit of edgeTable[cubeindex] equals 1. The condition is only true if the b0 of this value is 1. Why? Because the other bits do not matter since they are being bitwise AND with zero, which is always zero.

edgeTable[cubeindex] & 1 = value of bit 0

Upvotes: 0

Related Questions