dongzha3058 2011-02-09 14:05
浏览 76
已采纳

找到最高设置位

I have 5 different values which are saved as bits like 10010. I get the value as an Int from the Database (cannot change that) so like 24 means 11000 I know that i can get the biggest bit here by using

if ((decbin($d) & 16) == 16)

but if the first is 0 i would have to check the next bit, and if that is 0 i would have to ...

So after all i would have a block of ifs and if there were more bits the block was bigger. Is there a simple way to just get the "id" (or value, would not matter) of the highest bit with a 1?

  • 写回答

4条回答 默认 最新

  • duan7007 2011-02-09 14:11
    关注

    Yes. Compute the base 2 logarithm of the number and floor it:

    $highbit = floor(log($d, 2));
    

    If $highbit, for instance, is 5, it means that the 5th bit is the highest bit set to 1.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 51单片机中C语言怎么做到下面类似的功能的函数(相关搜索:c语言)
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起