【发布时间】:2017-12-15 05:32:40
【问题描述】:
对于某些背景,我正在尝试编写一个系统来传递整数数据包,以便使用布尔切换来确定两个节点之间是否应该有墙来构建迷宫,目前我的迷宫可以处理 480 个墙,因此我不想发送一个包含单个项目的数据包,而是将其拆分为一个整数数组(长度为 8),从而给我 480/8 个要发送的对象。
const int wallRows = mazeSize / 8;
int temp = NULL;
int temp2 = NULL;
int current = NULL;
int concatCount = 0;
int* walls = new int[wallRows];
int wallIndex = 0;
for (int i = 0; i < mazeSize; i++) {
current = temp2;
//ensure my ints have only 8 bytes
if (concatCount >= 7) {
//allocate a full int to the array
walls[wallIndex] = temp;
//clear the int
temp = NULL;
//move to the next array pos
wallIndex++;
//restart the int count
concatCount = 0;
}
if (maze->allEdges[i]._iswall) {
//append a 1 to the int
temp = 0b1;
}
else {
//append a 0 to the int
temp = 0b0;
}
//increment the int count
current = (temp2 << 1) | temp;
concatCount++;
}
这是我目前构建的,我的想法是从一个 int 开始,根据 bool "_isWall" 的返回将 int 传递给它,并将结果移位到 int 的末尾。 当 int 达到容量时,迭代到数组中的下一个 int 并重新开始,直到迷宫的墙壁填充了数组。
编辑:我的问题不清楚。 我的按位运算似乎实际上并未将多个位分配给同一个整数,我哪里出错了?
【问题讨论】:
-
你有什么问题?
-
并非所有整数都是 8 字节长。
uint64_t可能是 8 个字节长,但前提是CHAR_BIT==8即您的字节长为 8 位。大多数情况下,字节长为 8 位,但是如果您为 32 位 pc、raspberri pi 或 arduino 编译,那么您的 int 将不是 64 位。
标签: c++ arrays bit-manipulation enet