Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用C语言打印位图_C_Bitmap_Bitmapdata - Fatal编程技术网

用C语言打印位图

用C语言打印位图,c,bitmap,bitmapdata,C,Bitmap,Bitmapdata,我正在尝试创建一个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

我正在尝试创建一个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;
}
intmain()
{
无符号整数位的位置,setOrUnsetBit,ch;
无符号字符位映射数组索引,移位索引;
无符号字符位映射[100]={0};
做
{
printf(“输入位位置(位从1开始到100结束)\n”);
扫描频率(“%d”位和位u位置);
printf(“是否要设置/取消设置位(1或0)\n”);
scanf(“%d”和setorunset位);
位映射数组索引=(位位置-1)/8;
移位索引=(位位置-1)%8;
printf(“位位置:%d移位索引:%d\n”,位位置,移位索引);
如果(设置或取消设置位)
{

bit_map[bit_map_array_index]|=1您使用的是字节,而不是位。您有100个字节,请将每个字节设置为0或1。无需移动值:

unsigned char bytes[100];
for(int i = 0; i < sizeof(bytes); i++)
    bytes[i] = rand() % 2;

for(int y = 0; y < 10; y++)
{
    for(int x = 0; x < 10; x++)
    {
        int i = y * 10 + x;
        printf("%d ", bytes[i]);
    }
    printf("\n");
}
因为
13
13*8
位或
104
位。您只需要
100
位。如何设置和获取位取决于您选择的格式。例如,位图文件会被填充,因此每行是4字节的倍数。通常,您可以将值设置为:

if(bytes[i])
    data[byteindex] |= (1 << shift);
else
    data[byteindex] &= ~(1 << shift);

我想知道你的代码中的数字
50
是从哪里来的…更正为8~8比特。这可能会有帮助:请发布一个,这样我们就可以重现问题。没有实际输入、所需输出、实际输出的示例,并且代码缺少关键部分,例如
\include
语句,我们只能猜测实际情况是。编译时,始终启用警告,然后修复这些警告。(对于
gcc
,至少使用:
-Wall-Wextra-Wconversion-pedantic-std=gnu11
),那么您打算使用位还是字节呢?另请参阅关于1位位图文件格式的回答
if(bytes[i])
    data[byteindex] |= (1 << shift);
else
    data[byteindex] &= ~(1 << shift);
int value = (data[byte] & shift) > 0;