I don't know what it does though...
|
I tried to say that :) Bit twiddling is obnoxious to read.
((Bitplane0_ROW[p] >> N) & 1) | (((Bitplane1_ROW[p] >> N) & 1) << 1)
says
shift bpr0[p] down so that the nth bit is in the 0/1 bit location and test it against 1. Or that with bpr1[p] shifted down and tested against 1, shifted up.
which is still hard to follow, it becomes this:
add bpr0[p]'s nth bit to
two times bpr1[p]'s nth bit.
because shifting up (<<) is a multiplication by a power of 2 and shifting down is division by a power of 2. so if BPR1[p]'s nth bit is 1, you get 1, shifted up one time, is in binary from 01 to 10 which is from 1 to 2 in decimal. See it? Shift again, its 100 which is 4. Multiply by 2 by shift.
likewise, the ors are similar to adding. 10 | 1 is 11 which says that 2 ored with 1 is 3, which it is. Its not exactly adding (don't make that mistake somewhere later) but you know that you are adding either 1 and 0 or 0 and 0, and for THAT it works the same.
doing this with bits is just overly complicated.
now look at it this way:
int pot[] {0,1,2,4,8,16 ...};
result = (BPR0[p] & pot[n])*1 + (PBR1[p] & pot[n])*2;
of course the *1 is useless, but its there so you can SEE what is going on.
that adds (0 or 1)*1 + (0 or 1)*2 to give 0,1,2,3 as possible results, which are in binary
00
01
10
11
... and it does it with minima bit fiddling, which can be further reduced with bitfields (then you don't need the powers of two array (pot)) as its inherent).
See if you can do the next one with these ideas (which way you implement is irrelevant, but make it so you can read it in 5 years when you come back to the code). It is just more of the same, boil down each term to zero or 1 and then set the bit it represents in the integer (1,2,4,8) to get your result.
it isnt useful here, but know that anything to the zero power is 1. this helps with c++ 0 based array indexing when doing bit stuff, you can set it so [0] is the 1 bit, [1] is the 2 bit.. representing 2 to the index. the pot array quietly uses this idea a little.