【发布时间】:2018-11-08 19:31:52
【问题描述】:
假设我想创建特定长度的二进制文件,使用前 4 位定义类型(应该允许 16 种不同的类型)和后 60 位定义内容。
如何继续在 C 中构建它?我很难找到在 C 中执行此操作的任何示例(正确解释它)(我以前没有使用过这么低级别的 C,我正试图弄湿我的脚......)
我可以创建一个char[8] 并手动设置每个位,例如
/** Set bit in any sized bit block.
*
* @return none
*
* @param bit - Bit number.
* @param bitmap - Pointer to bitmap.
*
* @note Please note that this function does not know the size of the
* bitmap and it cannot range check the specified bit number.
*/
void SetBit(int bit, unsigned char *bitmap)
{
int n, x;
x = bit / 8; // Index to byte.
n = bit % 8; // Specific bit in byte.
bitmap[x] |= (1 << n); // Set bit.
}
【问题讨论】:
-
char name[16];有 16 字节 * 8 位/字节 = 128 位。char name[8];应该足够了。 -
16 个字符的数组将是 16 x 8 = 128 位
-
数学让我失望了!我会编辑上面的帖子来修复它!
-
是什么类型的?您要存储的数字范围是多少?
-
4 位类型可以是任何可以用 4 位空间表示的东西......猜猜这是 0-15 之间的任何东西
标签: c