Re: Some techniques in C++
but did you ever read something about bitwise operations?
At least, something from wiki?
Or from MSDN?
Re: Some techniques in C++
Quote:
whats the meaning of this lines:
The best way to understand code is to trace through each statement with the debugger and watch the contents of the variables used. That way you can see the effect of each statement.
Re: Some techniques in C++
Quote:
Originally Posted by
Kmilano
in this piece of code bit masking technique and XOR operation are used, could you please explain me about these techniques and what's the alternative ways instead of these methods?
Using bitwise operations are usually the best way to perform operations on bits so alternatives are seldom necessary :). But bitwise coding can be quite obscure and clever so it's never wrong to put in a comment or two as a courtesy to the reader.
The programmer in this case is using the pow function to produce numbers that are powers of two. This is not a bitwise operation though. The bitwise equivalent to pow(2,p) is (1<<p) which means that a rightmost bit is shifted p positions to the left.
The equality((i&j) == i) is true when all set bits in i are also set in j. If the bits of i and j are used to indicate set membership this would be to say that i is a subset of j (but that mustn't necessarily be how they're used here).
The assignment Final[i] ^= Final[j] performs an xor operation of course but why is not clear from the code you posted. Very common uses for xor in applications are
- scrambling (a kind of simple cryptography),
- checksum calculations (for data integrity purposes),
- Nim-sum calculations (to find a winning strategy in certain games).
But ever so often xor is used simply because that's the wanted logical operation.