function Getbit ($num, $bit, $mask) { return $num >> $bit & $mask;} Var_dump (Getbit (3, 7)); Var_dump (Getbit (0x29, 0x0, 0xf)); Hexadecimal notation also applies
This function is suitable for binary streams with big endian
Takes the decimal number $num the $bit bit to the $mask bit, the return value is the decimal number
Principle, the $num left to the $bit bit, and then with the $mask phase, get a mask of a few binary numbers, for example (that is, 0000 1010), take 9 of the 2nd to 4th bit (101, that 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 bit, only the two sides are 1 is equal to 1, only one side 0 is equal to 0, so the mask must be a left coherent 1 binary number, for example 0000 1111,0000 0111
[PHP] [bit conversion accumulation] and operations intercept the value of the binary stream