代码之家  ›  专栏  ›  技术社区  ›  OlimilOops

int24-24位整型数据类型

c++
  •  11
  • OlimilOops  · 技术社区  · 16 年前

    C++中有24位原始整数数据类型吗?

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

    其目的可能是:
    *操作24位格式的声音文件
    *不使用AlphaChannel操作位图数据

    提前多谢

    哎呀

    4 回复  |  直到 13 年前
        1
  •  14
  •   Goz    16 年前

    我写这篇文章是为了帮助我进行音频操作。这不是最快的,但对我很有效。)

    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;
        }
    
        /***********************************************/
        /***********************************************/
    };
    
        2
  •  18
  •   Jasper Bekkers    13 年前

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

    struct int24{
        unsigned int data : 24;
    };
    

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

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

    对于图像数据,人们往往只使用32位整数,而忽略所有alpha 8 alpha位,或者如果您处理的是压缩格式,那么最好还是将它们作为字符指针来处理,因为所有通道都是分开的。无论如何,这将是一个性能/内存权衡,因为分别写入一个int通常比三个chars快;但是它将占用25%的内存。

    像这样的打包结构是编译器特有的。但是,在Visual Studio中,您可以执行以下操作使结构正好24位。

    #pragma pack(push, 1)
    struct int24{
        unsigned int data : 24;
    };
    #pragma pack(pop)
    
        3
  •  6
  •   Nicolas    16 年前

    使用小于整数(32位或64位,具体取决于您的体系结构)的任何对象都是不理想的。较小数据类型(短等)的所有CPU操作都是使用整数算法完成的。必须完成与CPU之间的转换,从而减慢应用程序的速度(即使只是一点点)。

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

    至于操作音频数据,有许多库可以为您处理I/O—除非您想开始学习如何存储PCM等—以及其他DSP功能。我建议你使用那些图书馆中的一个。

        4
  •  0
  •   Paul R    16 年前

    不-你真正能做的就是:

    typedef int32_t int24_t;
    

    这有助于提高代码/意图的可读性/明显性,但不会对范围或存储空间施加任何限制。