Http://my.oschina.net/goal/blog/195749?fromerr=pryT221q
<?php$hex = Unpack (' h* ', ' as '); Var_dump ($hex); Var_dump (Hexdec ($hex [1])); Var_dump (Base_convert ($hex [1], 16, 2)); $bin = Pack ("A *", "Da"), echo "Output:". $bin. "\ n"; echo "output:0x". Bin2Hex ($bin). "\ n";? >
Takes the decimal number $num the $bit bit to the $mask bit, the return value is the decimal number
Principle, move the $num right to the $bit bit, and then with $mask phase, get the mask mask several binary numbers, for example (that is, 0000 1010), take 9 of the 2nd to 4th bit (101, which is the decimal number 5), the first right to move 1 bits, and then 7 (that is, 0000 0111) and then get 5 (101) This number, because the phase of the phase, only the two sides are 1 is equal to 1, only one side 0 is equal to 0, so the mask must be a coherent 1 binary number, for example 0000 1111,0000 0111
function Getbit ($num, $bit, $mask) {return $num >> $bit & $mask;} Var_dump (Getbit (41, 3, 7));
[PHP] Pack and unpack of [bit conversion accumulation]