[PHP] Pack and unpack of [bit conversion accumulation]

Source: Internet
Author: User
Tags unpack

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]

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.