duanhuang4841 2014-06-16 20:57
浏览 1005
已采纳

通过整数值确定标志

Many PHP functions use constants with integer values for flags. Hence, an integer value of:

48 = 32, 16

51 = 32, 16, 2, 1

I was curious if there is a php function / class or a more elegant way to reverse the process to break down an integer as I did above, and even better determine the flags associated with the function.

I have thought of setting an associative array like so: [ 'flag name' => int ] and then looping through the array to add up the values of every possible combination, but this seems pretty inefficient. Especially when dealing with as many as 12 flags, then you have millions of combinations.

  • 写回答

1条回答 默认 最新

  • doujuegai8830 2014-06-16 21:05
    关注

    You can use bitwise AND to see if a value is set:

    $a = 23;
    $sixteen = 16;
    $eight = 8;
    if($a & $sixteen) echo "the 16 bit is set";
    if($a & $eight) echo "the 8 bit is set";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器