【发布时间】:2023-03-11 02:41:01
【问题描述】:
我正在尝试创建一个包含 100 个 1 和 0 的位图。
下面是我到目前为止出来的内容。我在打印位图时遇到问题,或者我不知道如何打印位图。
我想显示由我设置的所有 1 和 0 组成的位图。对于索引 0 到 99
int main()
{
unsigned int bit_position, setOrUnsetBit, ch;
unsigned char bit_Map_array_index, shift_index;
unsigned char bit_map[100] = { 0 };
do
{
printf("Enter the Bit position (bit starts from 1 and Ends at 100) \n");
scanf("%d", &bit_position);
printf(" Do you want to set/unset the Bit (1 or 0) \n");
scanf("%d", &setOrUnsetBit);
bit_Map_array_index = (bit_position - 1) / 8;
shift_index = (bit_position - 1) % 8;
printf("The bit_position : %d shift Index : %d\n", bit_position, shift_index);
if (setOrUnsetBit)
{
bit_map[bit_Map_array_index] |= 1 << shift_index; //set 1
}
else
{
bit_map[bit_Map_array_index] &= ~(1 << shift_index); //set 0
}
printf(" Do You want to Continue then Enter any Number"
"and for Exit then enter 100\n");
scanf("%d", &ch);
} while (ch != 100);
//I wan to print bitmap here after exiting
system("pause");
return 0;
}
我在 C 编程方面的经验很少......所以无论我错在哪里,请纠正我。
提前致谢。
【问题讨论】:
-
我想知道你代码中的数字
50来自哪里... -
修正为8~8bit
-
试试这个可能有帮助:stackoverflow.com/questions/2525310/…
-
请发minimal reproducible example,以便我们重现问题。如果没有实际输入、期望输出、实际输出的示例以及缺少关键部分的代码(例如
#include语句),我们只能猜测实际情况。 -
编译时,始终启用警告,然后修复这些警告。 (对于
gcc,至少使用:-Wall -Wextra -Wconversion -pedantic -std=gnu11)
标签: c bitmap bitmapdata