简体   繁体   English

迭代char数组,打印每个char的位(在C中)

[英]Iterate through char array, printing bits of each char (in C)

Trying to print out the bits of each char stored in an array. 试图打印出存储在数组中的每个char的位。 I've looked up some code and tried a version to suit my needs. 我查了一些代码并尝试了一个版本以满足我的需求。 Problem is that I only seem to be getting the first char in the array. 问题是我似乎只是获取数组中的第一个char。

//read_buffer is the array I want to iterate through, bytes_to_read is the number of 
//index positions I want to_read. (array is statically allocated and filled using read()
//funct, therefore there are some garbage bits after the char's I want), bytes_to_read
//is what's returned from read() and how many bytes were actually read into array
void PrintBits(char read_buffer[], int bytes_to_read)
{

        int bit = 0;
        int i = 0;
        char char_to_print;

        printf("bytes to read: %d\n", bytes_to_read); //DEBUG

        for (; i < bytes_to_read; i++)
        {
                char_to_print = read_buffer[i];

                for (; bit < 8; bit++)
                {
                        printf("%i", char_to_print & 0X01);
                        char_to_print >> 1;
                }
                printf(" ");
                printf("bytes_to_read: %d -- i: %d", bytes_to_read, i);
        }

        printf("\n");
}

Basically what I'm getting is: 00000000 Not sure why this is. 基本上我得到的是: 00000000不知道为什么会这样。 Through debugging I've found it only to be printing the first bit and nothing else. 通过调试我发现它只是打印第一位而没有别的。 I've also proven that the outer loop is actually iterating through int's 0 - 29... So it should be iterating through the char's in the array. 我还证明了外部循环实际上是通过int的0 - 29迭代...所以它应该遍历数组中的char。 I'm stumped. 我很难过。

Also, can someone tell me what the & 0x01 is doing in the printf statement. 此外,有人可以告诉我& 0x01printf语句中正在做什么。 I found that in someone else's code and I am unsure. 我发现在其他人的代码中,我不确定。

You missed that 你错过了

                   char_to_print >>= 1;

char_to_print was not shifted AND saved char_to_print未被移位并保存

And you should initialize bit each time with an new char_to_print 并且您应该每次使用新的char_to_print初始化位

            for (bit = 0; bit < 8; bit++)

"can someone tell me what the "& 0x01" is doing in the printf statement" “有人能告诉我”&0x01“在printf语句中做了什么”

That's how you get each digit. 这就是你获得每个数字的方式。 The number is shifted down 1, and bitwise ANDed with 1. 1 only has one bit set, the *L*east *S*ignificant one, so AND'ing with that will yield either 1 (if char_to_print also has the LSB set) or zero, if it doesn't. 数字向下移1,与1按位进行比较1. 1仅设置1位,* L * east * S *为1,因此与此相关将产生1(如果char_to_print也设置了LSB)如果不是,则为零。

So, eg, if char_to_print is 4 originally, the first time it's ANDed with 1 yields zero, because the LSB is not set. 因此,例如,如果char_to_print最初为4,则第一次与1的ANDed产生零,因为未设置LSB。 Then it's shifted down one and AND'ed, another zero. 然后它向下移动一个并且与另一个零向下移动。 The third time, the LSB is set, so you get 1. Binary 100 is decimal 4. 第三次,LSB设置,所以你得到1.二进制100是十进制4。

There are two problems: 有两个问题:

  1. char_to_print >> 1; is doing the bit-shift, but throwing away the result. 正在做位移,但扔掉了结果。 Try char_to_print = char_to_print >> 1; 尝试char_to_print = char_to_print >> 1;

  2. You cannot pass a char to printf expecting an integer. 您不能将char传递给期望整数的printf You should (int)(char_to_print & 0x01) . 你应该(int)(char_to_print & 0x01)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM