查找位数组中设置的最高有效位(最左侧) [英] Find most significant bit (left-most) that is set in a bit array

查看:24
本文介绍了查找位数组中设置的最高有效位(最左侧)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个位数组实现,其中第 0 个索引是数组中第一个字节的 MSB,第 8 个索引是第二个字节的 MSB,等等......

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 有 __builtin_clz 在 x86/x64 上转换为 BSR,在 ARM 上转换为 CLZ 等,并在硬件未实现的情况下模拟指令.
Visual C++ 2005 及更高版本具有 _BitScanReverse.

GCC has __builtin_clz that translates to BSR on x86/x64, CLZ on ARM, etc. and emulates the instruction if the hardware does not implement it.
Visual C++ 2005 and up has _BitScanReverse.

这篇关于查找位数组中设置的最高有效位(最左侧)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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