我需要从右到左查找二进制数中的第一个置位;我想出了这个解决方案:
int cnt=0;
while (number& 1 ==0)
{
cnt++;
number>>=1;
}
Run Code Online (Sandbox Code Playgroud)
有更好的方法吗?一些聪明的位操纵技术?
枚举整数并返回打开的每个位的指数的最快方法是什么?看过一个使用<<和另一个使用Math.Pow的例子.想知道是否有其他事情真的很快.
谢谢.
我想找到获得long long最低位的索引的最快方法.即:
00101001001000 -> 3
Run Code Online (Sandbox Code Playgroud)
涉及循环和移位的解决方案太慢了.即:
int i;
if(bits == 0ULL) {
i = 64;
} else {
for(i = 0;!(bits & 1ULL);i++)
bits >>= 1;
}
Run Code Online (Sandbox Code Playgroud)
编辑:使用信息
使用ffsll的函数不能真正减少它的用法,但在这里(当然简化).它只是遍历索引并对它们做了一些事情.这个函数可能是我整个应用程序中使用最广泛的函数,尽管有很多缓存它的价值.它是我的alpha-beta搜索引擎中的合法移动生成器.
while(bits){
index = ffsll(bits);
doSomething(index);
index &= index-1;
}
Run Code Online (Sandbox Code Playgroud) 我正在将用C++编写的程序翻译成C#,并且我遇到了一个我无法解决的内在函数.在C++中,这被称为:
unsigned char _BitScanForward(unsigned long * Index, unsigned long Mask);
Run Code Online (Sandbox Code Playgroud)
如果我只知道内部函数所在的DLL(如果有的话),我可以使用P/Invoke.由于我不知道,我在.NET框架中寻找替代方案,但我空手而归.
有谁知道如何在_BitScanForward上使用P/Invoke,或者做同样的事情的.NET方法?
感谢任何帮助,谢谢.
假设我有以下位掩码:
1 << 1, // 2
1 << 2, // 4
1 << 3, // 8
1 << 4, // 16
1 << 5, // 32
1 << 6 // 64
Run Code Online (Sandbox Code Playgroud)
我想得到“逆”。
这完成了工作:
void foo(int n) {
int c = 1;
while (n/2 >= 2) {
n /= 2;
c++;;
}
println(c);
}
Run Code Online (Sandbox Code Playgroud)
例如,1 << 4结果是 16。如果我运行foo(16)它会打印 4。但是,我觉得它可以做得更简单,但我不知道如何做。
是否可以?
我有一个100个随机整数的列表.每个随机整数的值都是0到99.允许重复,因此列表可能是这样的
56, 1, 1, 1, 1, 0, 2, 6, 99...
Run Code Online (Sandbox Code Playgroud)
我需要找到列表中未包含的最小整数(> = 0).
我最初的解决方案是:
vector<int> integerList(100); //list of random integers
...
vector<bool> listedIntegers(101, false);
for (int theInt : integerList)
{
listedIntegers[theInt] = true;
}
int smallestInt;
for (int j = 0; j < 101; j++)
{
if (!listedIntegers[j])
{
smallestInt = j;
break;
}
}
Run Code Online (Sandbox Code Playgroud)
但这需要一个二级数组用于簿记和第二个(可能是完整的)列表迭代.我需要执行这个任务数百万次(实际的应用程序是一个贪婪的图形着色算法,我需要找到一个顶点邻接列表中最小的未使用的颜色值),所以我想知道是否有一个聪明的方法来获取没有那么多开销的相同结果?
我正在尝试使用实现安德鲁·格兰特建议的LSB查找方法来回答这个问题:设置的最低有效位的位置
但是,它导致了分段错误.这是一个展示问题的小程序:
#include <iostream>
typedef unsigned char Byte;
int main()
{
int value = 300;
Byte* byteArray = (Byte*)value;
if (byteArray[0] > 0)
{
std::cout<< "This line is never reached. Trying to access the array index results in a seg-fault." << std::endl;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
我已经读过在C++中使用'C-Style'强制转换是不好的做法.我应该用reinterpret_cast<Byte*>(value)吗?但是,这仍会导致分段错误.
我必须将 2 个 DWORD、IP 地址和网络掩码转换为 CDIR 格式...所以我有 2 个 DWORD 对应于 1.1.1.1 和 255.255.255.255,我想提出字符串 1.1.1.1/32
对此有何想法?
谢谢
有没有办法提取集合中的最高(或最低)值?例如,如果我有以下"字节集":
[0, 1, 2, 4, 5, 28, 199]
Run Code Online (Sandbox Code Playgroud)
我可以运行任何功能并返回199作为结果?
编辑:有一个明显的暴力解决方案涉及for..in循环.如果可能的话,我想找到一个比这更好的方法.
输入是存储在连续存储器中的比特阵列,每1比特存储器具有1比特的比特阵列.
输出是比特阵列的设定位索引的数组.
例:
bitarray: 0000 1111 0101 1010
setA: {4,5,6,7,9,11,12,14}
setB: {2,4,5,7,9,10,11,12}
Run Code Online (Sandbox Code Playgroud)
获得A组或B组都可以.该集存储为uint32_t数组,因此该集的每个元素都是数组中的无符号32位整数.
如何在单个cpu核心上快5倍左右?
当前代码:
#include <iostream>
#include <vector>
#include <time.h>
using namespace std;
template <typename T>
uint32_t bitarray2set(T& v, uint32_t * ptr_set){
uint32_t i;
uint32_t base = 0;
uint32_t * ptr_set_new = ptr_set;
uint32_t size = v.capacity();
for(i = 0; i < size; i++){
find_set_bit(v[i], ptr_set_new, base);
base += 8*sizeof(uint32_t);
}
return (ptr_set_new - ptr_set);
}
inline void find_set_bit(uint32_t n, uint32_t*& ptr_set, uint32_t base){
// Find the set bits …Run Code Online (Sandbox Code Playgroud) 我正在尝试编写 BSF 的矢量化实现作为练习,但我陷入困境,它不起作用。
算法:
short bitScanForward(int16_t bb)
{
constexpr uint16_t two = static_cast<uint16_t>(2);
constexpr uint16_t zero = static_cast<uint16_t>(0);
uint16_t lsb;
bb &= -bb;
lsb = (unsigned short)bb
| (unsigned short)(bb >> short(8));
return static_cast<short>(((((((unsigned short)(bb >>
short(8)) != zero) * two)
+ ((lsb & unsigned short(0xf0f0)) != zero)) * two)
+ ((lsb & unsigned short(0xcccc)) != zero)) * two)
+ ((lsb & unsigned short(0xaaaa)) != zero);
}
Run Code Online (Sandbox Code Playgroud)
我的矢量代码:
[[nodiscard]] inline __m128i _mm_cmpneq_epi16(const __m128i& a, const __m128i& …Run Code Online (Sandbox Code Playgroud)