整数到布尔数组
integer to bool array
我想将像 83 这样的整数编码为二进制代码,1100101最快的方法是什么? 现在我正在使用以下代码:
ToBinary(int size, int value) {
size--;
this->code = new bool[size];
int in = size;
while (in >= 0) {
if(pow(2, in) <= value) {
this->code[size-in] = pow(2, in);
value -= pow(2, in);
} else
this->code[size-in] = 0;
in--;
}
}
你可以利用使用>>
进行换档,让事情变得容易得多:
ToBinary(int size, int value) {
int i = size;
this->code = new bool[size];
while(i--) {
this->code[i] = (value >> i) & 1;
}
}
(或者,要按相反的顺序排列,this->code[size - i]
。
如果你在编译时知道大小,std::bitset<size> bits(value);
将在其构造函数中做你想要的。
相关文章:
- 当我尝试通过 arduino 上的引用传递布尔数组时出现问题
- 以十为基数的数字到布尔数组,该数组要求二进制/基数为 2 的数字
- 存储另一个函数返回的布尔数组时遇到问题
- 在 c++ 中分配布尔值的动态二维数组时超出内存限制
- 尝试设置唯一指针布尔数组的值时BAD_ACCESS错误
- C++ CopyMemory 和布尔数组
- 如何正确识别具有布尔值的动态数组
- 将uint64_t位掩码转换为 std::布尔数组
- 使用 true 初始化布尔数组时出现问题
- CRC32 C++使用布尔数组和手动逐位异或实现
- 带有布尔数组成员的 C++ 结构.如何在主代码中使用
- 使用动态布尔数组时出现问题
- C/如何将布尔值的数组写入文本文件
- 以C++为单位旋转布尔数组
- 将 __m128i?布尔数组中每个布尔字节的低位提取到打包的位图
- 我无法将'2D array whit bool'转换为"空 2D 数组布尔值"(用于生命游戏)
- 使用数组和布尔值在输出未知时从输出中删除逗号
- 如何在布尔值 true 参数上使用函数更改数组的值
- 给定一个大小的布尔数组,这都是真实的,我将如何仅使用递归将所有值设置为false
- 在动态2D布尔数组中更改一个值似乎会更改不同数组中不同位置的值