首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >int24 - 24位整型数据类型

int24 - 24位整型数据类型
EN

Stack Overflow用户
提问于 2010-04-21 20:16:04
回答 6查看 26.4K关注 0票数 15

C++中是否有24位的原始整型数据类型?

如果没有,是否可以创建一个类int24 (,uint24 )?

它的目的可能是:

*操作24位格式的声音文件

*在没有alphachannel的情况下操纵位图数据

非常感谢你提前

哦哦

EN

回答 6

Stack Overflow用户

回答已采纳

发布于 2010-04-21 20:18:08

我写这篇文章是为了帮助我处理音频。它不是最快的,但对我来说是有效的:)

代码语言:javascript
运行
复制
const int INT24_MAX = 8388607;

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

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

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

    operator int() const
    {
        if ( m_Internal[2] & 0x80 ) // Is this a negative?  Then we need to siingn extend.
        {
            return (0xff << 24) | (m_Internal[2] << 16) | (m_Internal[1] << 8) | (m_Internal[0] << 0);
        }
        else
        {
            return (m_Internal[2] << 16) | (m_Internal[1] << 8) | (m_Internal[0] << 0);
        }
    }

    operator float() const
    {
        return (float)this->operator int();
    }

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

        return *this;
    }

    Int24& operator =( const int input )
    {
        m_Internal[0]   = ((unsigned char*)&input)[0];
        m_Internal[1]   = ((unsigned char*)&input)[1];
        m_Internal[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 int val ) const
    {
        return Int24( (int)*this + val );
    }

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

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

    Int24 operator /( const int val ) const
    {
        return Int24( (int)*this / 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 )
    {
        *this   = *this + val;
        return *this;
    }

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

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

    Int24& operator /=( const int 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 );
    }

    /***********************************************/

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

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

    /***********************************************/
    /***********************************************/

    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;
    }

    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 int val ) const
    {
        return (int)*this == val;
    }

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

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

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

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

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

    /***********************************************/
    /***********************************************/
};
票数 17
EN

Stack Overflow用户

发布于 2010-04-21 20:21:56

根据需要,我会为它使用一个位域。

代码语言:javascript
运行
复制
struct int24{
    unsigned int data : 24;
};

或者,如果分隔更容易,只需使用3个字节(字符)。

顺便说一句,你在问题中提到的两种用例通常都使用32位整数。在音频处理的情况下,通常在加载音频块时转换为32位整数(或浮点数,最好是为了防止出现溢出情况),因为您不会一次将整个文件放入内存中。

对于图像数据,人们倾向于使用32位整数,而忽略所有的alpha 8 alpha位,或者如果您正在处理紧密打包的格式,您可能更好地将它们作为字符指针来操作,因为您将所有通道都分开。无论如何,这将是一个性能/内存的权衡,因为写入一个int通常比单独写入三个字符更快;然而,它将多占用25%的内存。

像这样打包结构是特定于编译器的。但是,在Visual Studio中,您需要执行以下操作才能使结构恰好为24位。

代码语言:javascript
运行
复制
#pragma pack(push, 1)
struct int24{
    unsigned int data : 24;
};
#pragma pack(pop)
票数 21
EN

Stack Overflow用户

发布于 2010-04-21 20:59:02

使用小于整数(32或64位取决于您的体系结构)的任何值都不是理想的。较小数据类型(short等)的所有CPU操作都是使用整数运算完成的。必须完成与CPU之间的转换,这会降低应用程序的运行速度(即使只是一点点)。

我的建议是:将它们存储为32 (或64位)整数,以提高整体速度。当需要执行I/O时,您将不得不自己进行转换。

至于处理音频数据,有许多库可以为你处理I/O -除非你想开始学习PCM等是如何存储的-以及其他DSP函数。我建议使用现有的许多库中的一个。

票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2682725

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档