用 C 处理位流

发布于 2024-11-16 08:43:08 字数 396 浏览 2 评论 0原文

我正在用 C 语言编写一些具有通信通道的信号处理代码。在输出端,当它们到达时我得到了一堆位。

for (n=0; n<BUFFER_LENGTH; n++) {
    /* do some processing that calculates x */
    output[n] = x > 0;
}

这是我的问题:

  1. 有没有一个好的类型可以代表 输出数组?起初我以为 uint1_t 是理想的选择,但我听说 这并不一定代表 内存中的一点点。
  2. 一旦我找到一个 我知道的数据中的同步模式 接下来的位的格式,我怎样才能 将一堆 1 和 0 转换为 将数组转换为整数、浮点数、 双打、角色等?我听说过使用 一个工会,但我认为这不会与 位数组。

I am writing some signal processing code in C that has a communications channel. At the output I get a bunch of bits as they arrive.

for (n=0; n<BUFFER_LENGTH; n++) {
    /* do some processing that calculates x */
    output[n] = x > 0;
}

Here are my questions:

  1. Is there a good type to represent
    the output array? At first I thought
    uint1_t would be ideal but I hear
    that doesn't necessarily represent
    one bit in memory.
  2. Once I find a
    sync pattern in the data I know the
    format of the next bits, how can I
    convert a bunch of 1's and 0's in
    the array into integers, floats,
    doubles, characters, etc.? I've heard of using
    a union but I don't think that will work with an
    array of bits.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

秋千易 2024-11-23 08:43:09

只需将数据存储在足够大的字节块中,然后使用移位和掩码迭代这些位,以顺序提取各个位。

例如,将缓冲区的内容打印为单独的位:

#include <stdio.h>
#include <stdint.h> // uint8_t et al
#include <limits.h> // CHAR_BIT

uint8_t buffer[256];
int b, bit;

for (b = 0; b < 256; ++b)
{
    for (bit = CHAR_BIT - 1; bit >= 0; --bit)
    {
        uint8_t mask = 1 << bit;
        printf("%2d", (buffer[b] & mask) != 0);
    }
    printf("\n");
}

Just store the data in a sufficiently large block of bytes and then iterate through the bits using shifting and masking to extract individual bits sequentially.

e.g. to print out the contents of a buffer as individual bits:

#include <stdio.h>
#include <stdint.h> // uint8_t et al
#include <limits.h> // CHAR_BIT

uint8_t buffer[256];
int b, bit;

for (b = 0; b < 256; ++b)
{
    for (bit = CHAR_BIT - 1; bit >= 0; --bit)
    {
        uint8_t mask = 1 << bit;
        printf("%2d", (buffer[b] & mask) != 0);
    }
    printf("\n");
}
孤独陪着我 2024-11-23 08:43:09

您必须以 8 位的倍数来处理它们,并自由地使用掩码(& 运算符)和移位(>> 运算符)。
因此,以 MP3 比特流为例(来自 http://www.mp3-tech .org/programmer/frame_header.html):

AAAAAAAA AAABBCCD EEEEFFGH IIJJKLMM

它可以保存在一个 32 位变量或四个 8 位变量中,如下所示:

MMLKJJII HGFFEEEE DCCBBAAA AAAAAAAA

这里你有:

int i = 0;
while (!syncFound)
{
    uint16 sync = data[i] | ((data[i + 1] & 0x07) << 8);
    if (sync == 0x07ff)
    {
        // The first 11 bits represent a sync.
        uint8 version = (data[i + 1] & 0x18) >> 3;
        uint6 layer = (data[i + 1] & 0x60) >> 5);
        // etc...

        syncFound = TRUE;
    } else {
        // current byte is not the start of a frame. check if the next byte is.
        i++;
    }

}

数组边界检查可能会变得乏味,因此也可能会变得乏味交易具有可变长度标头。

You'll have to deal with them in multiples of 8 bits and use masking (& operator) and shifting (>> operator) liberally.
So, to take the MP3 bitstream as an example (from http://www.mp3-tech.org/programmer/frame_header.html):

AAAAAAAA AAABBCCD EEEEFFGH IIJJKLMM

which can be held in either one 32-bit variable or four 8-bit ones as:

MMLKJJII HGFFEEEE DCCBBAAA AAAAAAAA

Here you'd have:

int i = 0;
while (!syncFound)
{
    uint16 sync = data[i] | ((data[i + 1] & 0x07) << 8);
    if (sync == 0x07ff)
    {
        // The first 11 bits represent a sync.
        uint8 version = (data[i + 1] & 0x18) >> 3;
        uint6 layer = (data[i + 1] & 0x60) >> 5);
        // etc...

        syncFound = TRUE;
    } else {
        // current byte is not the start of a frame. check if the next byte is.
        i++;
    }

}

Array bounds checking can get tedious and so too can dealing with variable length headers.

旧时模样 2024-11-23 08:43:09

假设您的解析距离中有大量位,那么您必须进行位打包。
考虑使用循环缓冲区来实现连续操作。
低误码率前提和高熵数据(即一个字节可以采用所有值而不仅仅是几个符号)那么基于联合的解码应该更好。要么在联合之前准备带有移位的位,要么定义八个联合,间隔一个移位并根据需要调用它们。

Assuming a large number of bits in your parsing distance then you have to bit pack.
Think circular buffers to enable continuous operation.
Low bit error rate premise and high entropy data (ie a byte may take all values not just a few symbols) then a union based decoding should be better. Either prep bits with shifts before the union or define eight unions one shift apart and call them as required.

打小就很酷 2024-11-23 08:43:09

将它们存储在一个大的 Char 缓冲区中,在获取数据后,您可以使用转换 API(如 atoi() atof() 等)将它们转换为整数、浮点数。
这在 C 套接字编程中通常也是这样完成的。

Store them in a large Char buffer and after you get your data you can convert to ints, floats using conversion API's like atoi() atof() etc.
This how its generally done in C socket programming too.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文