从集合中随机给定整数 0、1 和 2 时获取整数 0、1 和 2 的快速方法
fast way to get integers 0, 1, and 2 when given a random one from the set
所以基本上
int num = rand(2); //random number from 0-2
int otherNum, otherOtherNum;
otherNum = implement this
otherOtherNum = implement this
例如,如果 num 为 2,则 otherNum 和 otherOtherNum 必须设置为 0 和 1(或 1 和 0)。
您将如何实现这一点?假设您不能使用分支表或查找表。是的,我想要一个操纵解决方案。是的,我希望该解决方案比使用模运算符的解决方案更快(因为这本质上是一个除法)。
我认为查找可能是最快的,但不确定,但我不喜欢这种解决方案。
您也可以使用 XOR 和位掩码来执行此操作。
#include <stdio.h>
void
f(unsigned val, unsigned ary[3])
{
ary[0] = val;
ary[1] = (ary[0] ^ 1) & 1;
ary[2] = (ary[0] ^ 2) & 2;
}
int
main()
{
unsigned ary[3] = {0};
f(0, ary);
printf("f(0) = %d %d %dn", ary[0], ary[1], ary[2]);
f(1, ary);
printf("f(1) = %d %d %dn", ary[0], ary[1], ary[2]);
f(2, ary);
printf("f(2) = %d %d %dn", ary[0], ary[1], ary[2]);
return 0;
}
这将打印:
f(0) = 0 1 2
f(1) = 1 0 2
f(2) = 2 1 0
otherNum = (num + 1) % 3
otherOtherNum = (num + 2) % 3
如果对查找表的限制意味着避免内存访问,则可以使用寄存器内查找表。寄存器内查找表只是一个编译时常量。
const int tab = ((1 << 0) | (2 << 4) |
(0 << 8) | (2 << 12) |
(0 << 16) | (1 << 20));
int num = rand(2); //random number from 0-2
int otherNum, otherOtherNum;
otherNum = (tab >> num*8) & 0xf;
otherOtherNum = (tab >> (num*8+4)) & 0xf;
我的 2 次小便。
int main()
{
std::srand(std::time(0));
int num = std::rand() % 3; //random number from 0-2
int otherNum = (0b001001 >> (num << 1)) & 0b11;
int otherOtherNum = (0b010010 >> (num << 1)) & 0b11;
std::cout << num << 'n';
std::cout << otherNum << 'n';
std::cout << otherOtherNum << 'n';
}
笔记:
0b001001 = 9
0b010010 = 18
0b11 = 3
此方法基本上使用存储在整数位中的表,并将相关位移动到所需的变量中。
另一种选择是使用数组进行查找,这避免了任何加法和模数,尽管我不相信它更快:
int lookup[3] = {1, 2, 0};
int num = rand(2); //random number from 0-2
int otherNum, otherOtherNum;
otherNum = lookup[num];
otherOtherNum = lookup[otherNum]
相关文章:
- 如何获取矢量循环的整数位置
- 获取我的基类以递增派生类对象整数
- 从两个 4x64 位整数数组中获取取模
- 如何获取进程的整数PID?
- 从 stdio 获取一行空格分隔的整数,而不知道它们是多少 (C++)
- 给枚举一个整数,并在 C++ 中将其相关值作为字符串获取
- 如何在 c++ 中正确从正整数中获取偶数和奇数?
- 快速扫描标准以获取整数不起作用
- 获取由空格分隔的未知大小的单行输入,并将其存储到整数数组中
- 只要用户输入,如何从用户那里获取整数?
- 如何交替从文件中获取一行数据和一个整数?
- 获取两个无符号整数 C++ 乘积的高 32 位的有效方法
- 如何从另一个类动态获取整数?
- 如何使用字符串和整数获取汽车类的整数结果
- 控制台在获取整数后暂停
- C++一次从文本文件中获取每 2 个整数作为对
- 在某些范围更新后获取整数数组的最终状态的有效算法是什么?
- 如何从从 MPU 陀螺仪发送的字节数组中获取整数
- 获取所有方式以两个整数的产品表示数字
- 如何在C 中获取整数数组的元素