c++ int 24 - 24位整数数据类型

z9ju0rcb  于 2022-11-19  发布在  其他
关注(0)|答案(6)|浏览(459)

C++中有24位基本整型数据类型吗?
如果没有,是否可以创建一个类int24(,uint24)?
其目的可能是:

  • 操作24位格式的声音文件
  • 无α通道操作位图数据
ncgqoxb0

ncgqoxb01#

根据需要,我将使用位域。

  1. struct int24{
  2. unsigned int data : 24;
  3. };

或者,如果分离更容易,则仅使用3个字节(字符)。
顺便说一句,你在问题中提到的两个用例通常都使用32位整数。在音频处理的情况下,你通常会转换为32位整数(或浮点数,最好是,以防止溢出的情况下,你会得到与定点或整数数学),当加载块的音频,因为你不会有整个文件在内存中一次。
对于图像数据,人们倾向于使用32位整数,而忽略alpha 8位,或者如果你要处理一个紧密压缩的格式,你可能更好地把它们作为字符指针来处理,因为你会把所有的通道分开。这将是一个性能/内存的权衡,因为写一个int通常比分别写三个字符快;但是它将占用多25%内存。
像这样打包结构体是编译器特定的。但是,在Visual Studio中,您可以执行以下操作使结构体正好为24位。

  1. #pragma pack(push, 1)
  2. struct int24{
  3. unsigned int data : 24;
  4. };
  5. #pragma pack(pop)
6ie5vjzr

6ie5vjzr2#

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

  1. const int INT24_MAX = 8388607;
  2. class Int24
  3. {
  4. protected:
  5. unsigned char m_Internal[3];
  6. public:
  7. Int24()
  8. {
  9. }
  10. Int24( const int val )
  11. {
  12. *this = val;
  13. }
  14. Int24( const Int24& val )
  15. {
  16. *this = val;
  17. }
  18. operator int() const
  19. {
  20. if ( m_Internal[2] & 0x80 ) // Is this a negative? Then we need to siingn extend.
  21. {
  22. return (0xff << 24) | (m_Internal[2] << 16) | (m_Internal[1] << 8) | (m_Internal[0] << 0);
  23. }
  24. else
  25. {
  26. return (m_Internal[2] << 16) | (m_Internal[1] << 8) | (m_Internal[0] << 0);
  27. }
  28. }
  29. operator float() const
  30. {
  31. return (float)this->operator int();
  32. }
  33. Int24& operator =( const Int24& input )
  34. {
  35. m_Internal[0] = input.m_Internal[0];
  36. m_Internal[1] = input.m_Internal[1];
  37. m_Internal[2] = input.m_Internal[2];
  38. return *this;
  39. }
  40. Int24& operator =( const int input )
  41. {
  42. m_Internal[0] = ((unsigned char*)&input)[0];
  43. m_Internal[1] = ((unsigned char*)&input)[1];
  44. m_Internal[2] = ((unsigned char*)&input)[2];
  45. return *this;
  46. }
  47. /***********************************************/
  48. Int24 operator +( const Int24& val ) const
  49. {
  50. return Int24( (int)*this + (int)val );
  51. }
  52. Int24 operator -( const Int24& val ) const
  53. {
  54. return Int24( (int)*this - (int)val );
  55. }
  56. Int24 operator *( const Int24& val ) const
  57. {
  58. return Int24( (int)*this * (int)val );
  59. }
  60. Int24 operator /( const Int24& val ) const
  61. {
  62. return Int24( (int)*this / (int)val );
  63. }
  64. /***********************************************/
  65. Int24 operator +( const int val ) const
  66. {
  67. return Int24( (int)*this + val );
  68. }
  69. Int24 operator -( const int val ) const
  70. {
  71. return Int24( (int)*this - val );
  72. }
  73. Int24 operator *( const int val ) const
  74. {
  75. return Int24( (int)*this * val );
  76. }
  77. Int24 operator /( const int val ) const
  78. {
  79. return Int24( (int)*this / val );
  80. }
  81. /***********************************************/
  82. /***********************************************/
  83. Int24& operator +=( const Int24& val )
  84. {
  85. *this = *this + val;
  86. return *this;
  87. }
  88. Int24& operator -=( const Int24& val )
  89. {
  90. *this = *this - val;
  91. return *this;
  92. }
  93. Int24& operator *=( const Int24& val )
  94. {
  95. *this = *this * val;
  96. return *this;
  97. }
  98. Int24& operator /=( const Int24& val )
  99. {
  100. *this = *this / val;
  101. return *this;
  102. }
  103. /***********************************************/
  104. Int24& operator +=( const int val )
  105. {
  106. *this = *this + val;
  107. return *this;
  108. }
  109. Int24& operator -=( const int val )
  110. {
  111. *this = *this - val;
  112. return *this;
  113. }
  114. Int24& operator *=( const int val )
  115. {
  116. *this = *this * val;
  117. return *this;
  118. }
  119. Int24& operator /=( const int val )
  120. {
  121. *this = *this / val;
  122. return *this;
  123. }
  124. /***********************************************/
  125. /***********************************************/
  126. Int24 operator >>( const int val ) const
  127. {
  128. return Int24( (int)*this >> val );
  129. }
  130. Int24 operator <<( const int val ) const
  131. {
  132. return Int24( (int)*this << val );
  133. }
  134. /***********************************************/
  135. Int24& operator >>=( const int val )
  136. {
  137. *this = *this >> val;
  138. return *this;
  139. }
  140. Int24& operator <<=( const int val )
  141. {
  142. *this = *this << val;
  143. return *this;
  144. }
  145. /***********************************************/
  146. /***********************************************/
  147. operator bool() const
  148. {
  149. return (int)*this != 0;
  150. }
  151. bool operator !() const
  152. {
  153. return !((int)*this);
  154. }
  155. Int24 operator -()
  156. {
  157. return Int24( -(int)*this );
  158. }
  159. /***********************************************/
  160. /***********************************************/
  161. bool operator ==( const Int24& val ) const
  162. {
  163. return (int)*this == (int)val;
  164. }
  165. bool operator !=( const Int24& val ) const
  166. {
  167. return (int)*this != (int)val;
  168. }
  169. bool operator >=( const Int24& val ) const
  170. {
  171. return (int)*this >= (int)val;
  172. }
  173. bool operator <=( const Int24& val ) const
  174. {
  175. return (int)*this <= (int)val;
  176. }
  177. bool operator >( const Int24& val ) const
  178. {
  179. return (int)*this > (int)val;
  180. }
  181. bool operator <( const Int24& val ) const
  182. {
  183. return (int)*this < (int)val;
  184. }
  185. /***********************************************/
  186. bool operator ==( const int val ) const
  187. {
  188. return (int)*this == val;
  189. }
  190. bool operator !=( const int val ) const
  191. {
  192. return (int)*this != val;
  193. }
  194. bool operator >=( const int val ) const
  195. {
  196. return (int)*this >= val;
  197. }
  198. bool operator <=( const int val ) const
  199. {
  200. return (int)*this <= val;
  201. }
  202. bool operator >( const int val ) const
  203. {
  204. return ((int)*this) > val;
  205. }
  206. bool operator <( const int val ) const
  207. {
  208. return (int)*this < val;
  209. }
  210. /***********************************************/
  211. /***********************************************/
  212. };
展开查看全部
acruukt9

acruukt93#

处理任何小于整数(32位或64位,取决于您的体系结构)的数据都是不理想的。所有较小数据类型(短整型等)的CPU操作都是使用整数运算完成的。必须完成与CPU的转换,这会降低应用程序的速度(即使只是一点点)。
我的建议是:将它们存储为32位(或64位)整数以提高整体速度。当需要进行I/O时,您必须自己进行转换。
至于操作音频数据,有许多库可以为您处理I/O-除非您想开始学习PCM等是如何存储的-以及其他DSP函数。我建议使用其中的一个库。

a2mppw5e

a2mppw5e4#

我知道我晚了十年,但你觉得比特集解决方案怎么样?

  1. class i24
  2. {
  3. std::bitset<24> m_value;
  4. public:
  5. constexpr i24(int value) noexcept: m_value {static_cast<unsigned long long>(value)} {}
  6. operator int() const
  7. {
  8. constexpr std::uint32_t negative_mask = (0xff << 24);
  9. return (m_value[23] ? negative_mask : 0) | m_value.to_ulong();
  10. }
  11. };
lyr7nygr

lyr7nygr5#

不-您真正能做的是:

  1. typedef int32_t int24_t;

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

z4bn682m

z4bn682m6#

最好的方法是创建一个Int24类,并像原始类型一样使用它。
Int24.h

  1. #ifndef INT24_H
  2. #define INT24_H
  3. class Int24
  4. {
  5. public:
  6. Int24();
  7. Int24(unsigned long);
  8. Int24 operator+ (Int24 value);
  9. Int24 operator* (int value);
  10. Int24 operator/ (int value);
  11. void operator= (unsigned long value);
  12. void operator= (Int24 value);
  13. operator int() const;
  14. // Declare prefix and postfix increment operators.
  15. Int24 &operator++(); // Prefix increment operator.
  16. Int24 operator++(int); // Postfix increment operator.
  17. // Declare prefix and postfix decrement operators.
  18. Int24 &operator--(); // Prefix decrement operator.
  19. Int24 operator--(int); // Postfix decrement operator.
  20. unsigned long value() const;
  21. private:
  22. unsigned char mBytes[3] ;
  23. };
  24. #endif // INT24_H

Int24.cpp

  1. #include "Int24.h"
  2. Int24::Int24()
  3. {
  4. mBytes[0] = 0;
  5. mBytes[1] = 0;
  6. mBytes[2] = 0;
  7. }
  8. Int24::Int24(unsigned long value)
  9. {
  10. mBytes[0] = ( value & 0xff);
  11. mBytes[1] = ((value >> 8) & 0xff);
  12. mBytes[2] = ((value >> 16 ) & 0xff);
  13. }
  14. Int24 Int24::operator+(Int24 value)
  15. {
  16. Int24 retVal;
  17. unsigned long myValue;
  18. unsigned long addValue;
  19. myValue = this->mBytes[2];
  20. myValue <<= 8;
  21. myValue |= this->mBytes[1];
  22. myValue <<= 8;
  23. myValue |= this->mBytes[0];
  24. addValue = value.mBytes[2];
  25. addValue <<= 8;
  26. addValue |= value.mBytes[1];
  27. addValue <<= 8;
  28. addValue |= value.mBytes[0];
  29. myValue += addValue;
  30. retVal = myValue;
  31. return retVal;
  32. }
  33. Int24 Int24::operator*(int value)
  34. {
  35. (*this) = (*this).value() * value;
  36. return (*this);
  37. }
  38. Int24 Int24::operator/(int value)
  39. {
  40. (*this) = (*this).value() / value;
  41. return (*this);
  42. }
  43. void Int24::operator=(unsigned long value)
  44. {
  45. mBytes[0] = ( value & 0xff);
  46. mBytes[1] = ((value >> 8) & 0xff);
  47. mBytes[2] = ((value >> 16 ) & 0xff);
  48. }
  49. void Int24::operator=(Int24 value)
  50. {
  51. mBytes[0] = value.mBytes[0];
  52. mBytes[1] = value.mBytes[1];
  53. mBytes[2] = value.mBytes[2];
  54. }
  55. Int24 &Int24::operator++()
  56. {
  57. (*this) = (*this).value() + 1;
  58. return *this;
  59. }
  60. Int24 Int24::operator++(int)
  61. {
  62. Int24 temp = (*this);
  63. ++(*this);
  64. return temp;
  65. }
  66. Int24 &Int24::operator--()
  67. {
  68. (*this) = (*this).value() - 1;
  69. return *this;
  70. }
  71. Int24 Int24::operator--(int)
  72. {
  73. Int24 temp = (*this);
  74. --(*this);
  75. return temp;
  76. }
  77. Int24::operator int() const
  78. {
  79. return value();
  80. }
  81. unsigned long Int24::value() const
  82. {
  83. unsigned long retVal;
  84. retVal = this->mBytes[2];
  85. retVal <<= 8;
  86. retVal |= this->mBytes[1];
  87. retVal <<= 8;
  88. retVal |= this->mBytes[0];
  89. return retVal;
  90. }

你可以从my GitHub link下载Int24和Int48类,这里还有一个例子告诉你如何使用它。

展开查看全部

相关问题