Php uses binary numbers to access switch data

Source: Internet
Author: User
Using binary to save switch data can save a lot of space, especially when storing a large amount of data. In general, three operations need to be implemented: [code snippets]

Using binary to save switch data can save a lot of space, especially when storing a large amount of data. In general, three operations need to be implemented: [code snippets]

Using binary to save switch data can save a lot of space, especially when storing a large amount of data. In general, there are three operations to be implemented:

$ Original = 345; // 101011001 function open ($ original, $ position) {$ data = 1 <$ position; return ($ original | $ data );} function close ($ original, $ position) {$ data = 1 <$ position; return ($ original &~ $ Data);} function get ($ original, $ position) {$ data = 1 <$ position; return ($ original & $ data)> 0? 1:0;} echo decbin ($ original), PHP_EOL; echo decbin (open ($ original, 2), '| open |', 2, PHP_EOL; echo decbin (close ($ original, 3), '| close |', 3, PHP_EOL; echo decbin (get ($ original, 4), '| get | ', 4, PHP_EOL; vagrant @ precise64:/var/www/test/curl $ php-f t. php 101011001101011101 | open | 2101010001 | close | 31 | get | 4,

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.