在PHP中位操作? [英] Bitwise operations in PHP?

查看:81
本文介绍了在PHP中位操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我了解,位运算所必需的很多低级编程,如编写设备驱动程序,低级别的图形,通信协议的分组组装和解码。我一直在做PHP几年来,我已经在PHP项目很少​​见到位运算。

I understand that bitwise operations are necessary for much low-level programming, such as writing device drivers, low-level graphics, communications protocol packet assembly and decoding. I have been doing PHP for several years now, and I have seen bitwise operations very rarely in PHP projects.

您能给我使用的例子?

推荐答案

您可以使用它来位掩码EN事情code组合。基本上,它的工作原理是让每个比特的含义,所以如果你有 00000000 ,每一位再presents东西,除了是一个十进制数为好。比方说,我有我想要存储用户的一些preferences,但我的数据库在存储方面非常有限。我可以简单地存储十进制数,从这个派生,其中preferences选择,例如 9 2 ^ 3 + 2 ^ 0 00001001 ,所以用户preference 1和preference 4。

You could use it for bitmasks to encode combinations of things. Basically, it works by giving each bit a meaning, so if you have 00000000, each bit represents something, in addition to being a single decimal number as well. Let's say I have some preferences for users I want to store, but my database is very limited in terms of storage. I could simply store the decimal number and derive from this, which preferences are selected, e.g. 9 is 2^3 + 2^0 is 00001001, so the user has preference 1 and preference 4.

 00000000 Meaning       Bin Dec    | Examples
 │││││││└ Preference 1  2^0   1    | Pref 1+2   is Dec   3 is 00000011
 ││││││└─ Preference 2  2^1   2    | Pref 1+8   is Dec 129 is 10000001
 │││││└── Preference 3  2^2   4    | Pref 3,4+6 is Dec  44 is 00101100
 ││││└─── Preference 4  2^3   8    | all Prefs  is Dec 255 is 11111111
 │││└──── Preference 5  2^4  16    |
 ││└───── Preference 6  2^5  32    | etc ...
 │└────── Preference 7  2^6  64    |
 └─────── Preference 8  2^7 128    |

进一步阅读

  • http://www.weberdev.com/get_example-3809.html
  • http://stu.mp/2004/06/a-quick-bitmask-howto-for-programmers.html
  • Why should I use bitwise/bitmask in PHP?
  • http://en.wikipedia.org/wiki/Mask_%28computing%29

这篇关于在PHP中位操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆