[PHP] [bit conversion accumulation] and operations intercept the value of the binary stream

Source: Internet
Author: User
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.