c - 如何在 C 中定义 24 位数据类型?

标签 c

我将不得不大量处理 24 位音频数据。这只是 unsigned int 但不是 32 位,而是 24 位。那么在 C 中定义和使用 24 位数据的更简单方法是什么?

最佳答案

根据要求,我会为其使用位域:

struct int24{
    unsigned int data : 24;
};

或者,如果分离更容易,只需使用 3 个字节(unsigned char)。如果您不想填充该结构,您可以强制对其进行打包。

[编辑:我看到 C++ 标签被删除了,但无论如何我都会把它留在这里] 如果您更习惯使用 C++,则可以使用如下内容:

const int INT24_MAX = 8388607;

class Int24
{
protected:
    unsigned char value[3];
public:
    Int24(){}

    Int24( const Int24& val )
    {
        *this   = val;
    }

    operator int() const
    {
        /* Sign extend negative quantities */
        if( value[2] & 0x80 ) {
            return (0xff << 24) | (value[2] << 16)
                                | (value[1] <<  8)
                                |  value[0];
        } else {
            return (value[2] << 16)
                 | (value[1] <<  8)
                 |  value[0];
        }
    }

    Int24& operator= (const Int24& input)
    {
        value[0]   = input.value[0];
        value[1]   = input.value[1];
        value[2]   = input.value[2];

        return *this;
    }

    Int24& operator= (const int input)
    {
        value[0]   = ((unsigned char*)&input)[0];
        value[1]   = ((unsigned char*)&input)[1];
        value[2]   = ((unsigned char*)&input)[2];

        return *this;
    }

    Int24 operator+ (const Int24& val) const
    {
        return Int24( (int)*this + (int)val );
    }

    Int24 operator- (const Int24& val) const
    {
        return Int24( (int)*this - (int)val );
    }

    Int24 operator* (const Int24& val) const
    {
        return Int24( (int)*this * (int)val );
    }

    Int24 operator/ (const Int24& val) const
    {
        return Int24( (int)*this / (int)val );
    }

    Int24& operator+= (const Int24& val)
    {
        *this   = *this + val;
        return *this;
    }

    Int24& operator-= (const Int24& val)
    {
        *this   = *this - val;
        return *this;
    }

    Int24& operator*= (const Int24& val)
    {
        *this   = *this * val;
        return *this;
    }

    Int24& operator/= (const Int24& val)
    {
        *this   = *this / val;
        return *this;
    }

    Int24 operator>> (const int val) const
    {
        return Int24( (int)*this >> val );
    }

    Int24 operator<< (const int val) const
    {
        return Int24( (int)*this << val );
    }

    operator bool() const
    {
        return (int)*this != 0;
    }

    bool operator! () const
    {
        return !((int)*this);
    }

    Int24 operator- ()
    {
        return Int24( -(int)*this );
    }

    bool operator== (const Int24& val) const
    {
        return (int)*this == (int)val;
    }

    bool operator!= (const Int24& val) const
    {
        return (int)*this != (int)val;
    }

    bool operator>= (const Int24& val) const
    {
        return (int)*this >= (int)val;
    }

    bool operator<= (const Int24& val) const
    {
        return (int)*this <= (int)val;
    }

    /* Define all operations you need below.. */

关于c - 如何在 C 中定义 24 位数据类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7416699/

相关文章:

c - 我正在尝试返回第一个持有值(value)项目的节点,如果没有找到则返回 NULL

c - 直接插入排序的错误 C 实现

c - 如何在gdb中获取结构的基本数据类型

c - 用 C 辅助结构封装

c - 使用 rpcgen 的简单计算器

c - 将 scanf 与通过引用传递的结构一起使用

c - 重用缓冲区指针_before_ free

c++ - 将递增/递减运算符放在三元/条件运算符中是否安全?

c - 从 Xcode 在 OS X 本地安装编译代码

c++ - 在 C/C++ 中让计时器每 100 毫秒重新启动一次