找到最显著位(最左边的)是,在一个位阵列设置 [英] Find most significant bit (left-most) that is set in a bit array

查看:208
本文介绍了找到最显著位(最左边的)是,在一个位阵列设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个位阵列实现,其中0号索引是在数组中的第一个字节的最高位,第八指数是第二个字节的最高位,等等...

I have a bit array implementation where the 0th index is the MSB of the first byte in an array, the 8th index is the MSB of the second byte, etc...

什么是发现,是该位数组中设置的第一位的快捷方式?所有相关的解决方案我已经抬头找到的第一个最显著位,但我需要首先最显著之一。因此,考虑到0x00A1,我想8(因为它是从左侧第9位)。

What's a fast way to find the first bit that is set in this bit array? All the related solutions I have looked up find the first least significant bit, but I need the first most significant one. So, given 0x00A1, I want 8 (since it's the 9th bit from the left).

推荐答案

GCC有<一个href=\"http://gcc.gnu.org/onlinedocs/gcc-4.3.4/gcc/Other-Builtins.html\"><$c$c>__builtin_clz可以转换为BSR在x86 / x64操作系统,CLZ在ARM等,并模拟了指令如果硬件没有实现它。结果
的Visual C ++ 2005和起来了<一个href=\"http://msdn.microsoft.com/en-us/library/fbxyd7zd%28v=VS.80%29.aspx\"><$c$c>_BitScanReverse.

这篇关于找到最显著位(最左边的)是,在一个位阵列设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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