Niels schrieb:
The problem: A function tries to update an existing value, but is only
allowed to write certain bits.
There are 3 variables:
A: the existing value, eg. 10110101
B: what the function wants to write, eg. 01011100
C: which bits the function is allowed to write, eg. 00001111
With these examples, 10111100 should be written.
How do I combine A, B and C to get that result?
1) First, AND the mask with the value the function wants to write in
order to suppress bits:
01011100 (B: value the function wants to write)
& 00001111 (C: mask)
--------
00001100 (D: intermediate result)
$D = $B & $C;
2) Next, OR the above result with the existing value:
10110101 (A: existing value)
| 00001100 (D: B & C)
--------
10111101 (E: final result)
$E = $A | $D;
Or, more succinctly:
$E = $A | ($B & $C);
I love Boolean operations. Used cleverly, they can replace code
branching with simple statements. Back when I was coding in
assembler & PL/M it was a righteously fast way of producing complex results.
Regards,
Paul
[Sorry if this question has long since been answered, but I missed
the rest of the thread.]
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php