Results 1 to 11 of 11

Thread: A question about uint 8 and uint 16 and uint 32 and uint 64

  1. #1
    Join Date
    Jul 2014
    Posts
    95
    Thanks
    67

    Default A question about uint 8 and uint 16 and uint 32 and uint 64

    Hi!
    What are the differences between uint 8 and uint 16 and uint 32 and uint 64???

    thanks

  2. #2
    Join Date
    Apr 2013
    Location
    Prague
    Posts
    258
    Thanks
    3
    Thanked 65 Times in 59 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: A question about uint 8 and uint 16 and uint 32 and uint 64

    The size of the unsigned int (uint):

    uint8 - 8 bits (1 byte) - 0 .. 255 (0xFF) - unsigned char, uchar, byte
    uint16 - 16 bits (2 bytes) - 0 .. 65535 (0xFFFF) - unsigned short, ushort, word
    uint32 - 32 bits (4 bytes) - 0 .. 0xFFFFFFFF - unsigned int, unsigned long, ulong, dword
    uint64 - 64 bits (8 bytes) - 0 .. 0xFFFFFFFFFFFFFFFF - unsigned long long, qword

    Note that there aren't other sizes, like "uint12" and so on. Only 8, 16, 32, 64 and, sometimes, 128 (oword on 64-bit processors, certain compilers).

  3. The following user says thank you to Radek for this useful post:

    rezas1000 (1st August 2014)

  4. #3
    Join Date
    Jul 2014
    Posts
    95
    Thanks
    67

    Default Re: A question about uint 8 and uint 16 and uint 32 and uint 64

    Hello
    My system is 32 bits.but the following code does not work with qint32.why?
    See the following section of code.
    Note:qint64 Converted to qint32.The name of the program is Audio Output Example and Examples of Qt.
    Qt Code:
    1. #include <QAudioDeviceInfo>
    2. #include <QAudioOutput>
    3. #include <QDebug>
    4. #include <QVBoxLayout>
    5. #include <qmath.h>
    6. #include <qendian.h>
    7.  
    8. #include "audiooutput.h"
    9.  
    10. #define PUSH_MODE_LABEL "Enable push mode"
    11. #define PULL_MODE_LABEL "Enable pull mode"
    12. #define SUSPEND_LABEL "Suspend playback"
    13. #define RESUME_LABEL "Resume playback"
    14. #define VOLUME_LABEL "Volume:"
    15.  
    16. const int DurationSeconds = 1;
    17. const int ToneSampleRateHz = 600;
    18. const int DataSampleRateHz = 44100;
    19. const int BufferSize = 32768;
    20.  
    21.  
    22. Generator::Generator(const QAudioFormat &format,
    23. qint32 durationUs,
    24. int sampleRate,
    25. QObject *parent)
    26. : QIODevice(parent)
    27. , m_pos(0)
    28. {
    29. generateData(format, durationUs, sampleRate);
    30. }
    31.  
    32. Generator::~Generator()
    33. {
    34.  
    35. }
    36.  
    37. void Generator::start()
    38. {
    39. open(QIODevice::ReadOnly);
    40. }
    41.  
    42. void Generator::stop()
    43. {
    44. m_pos = 0;
    45. close();
    46. }
    47.  
    48. void Generator::generateData(const QAudioFormat &format, qint32 durationUs, int sampleRate)
    49. {
    50. const int channelBytes = format.sampleSize() / 8;
    51. const int sampleBytes = format.channelCount() * channelBytes;
    52.  
    53. qint32 length = (format.sampleRate() * format.channelCount() * (format.sampleSize() / 8))
    54. * durationUs / 100000;
    55.  
    56. Q_ASSERT(length % sampleBytes == 0);
    57. Q_UNUSED(sampleBytes) // suppress warning in release builds
    58.  
    59. m_buffer.resize(length);
    60. unsigned char *ptr = reinterpret_cast<unsigned char *>(m_buffer.data());
    61. int sampleIndex = 0;
    62.  
    63. while (length) {
    64. const qreal x = qSin(2 * M_PI * sampleRate * qreal(sampleIndex % format.sampleRate()) / format.sampleRate());
    65. for (int i=0; i<format.channelCount(); ++i) {
    66. if (format.sampleSize() == 8 && format.sampleType() == QAudioFormat::UnSignedInt) {
    67. const quint8 value = static_cast<quint8>((1.0 + x) / 2 * 255);
    68. *reinterpret_cast<quint8*>(ptr) = value;
    69. } else if (format.sampleSize() == 8 && format.sampleType() == QAudioFormat::SignedInt) {
    70. const qint8 value = static_cast<qint8>(x * 127);
    71. *reinterpret_cast<quint8*>(ptr) = value;
    72. } else if (format.sampleSize() == 16 && format.sampleType() == QAudioFormat::UnSignedInt) {
    73. quint16 value = static_cast<quint16>((1.0 + x) / 2 * 65535);
    74. if (format.byteOrder() == QAudioFormat::LittleEndian)
    75. qToLittleEndian<quint16>(value, ptr);
    76. else
    77. qToBigEndian<quint16>(value, ptr);
    78. } else if (format.sampleSize() == 16 && format.sampleType() == QAudioFormat::SignedInt) {
    79. qint16 value = static_cast<qint16>(x * 32767);
    80. if (format.byteOrder() == QAudioFormat::LittleEndian)
    81. qToLittleEndian<qint16>(value, ptr);
    82. else
    83. qToBigEndian<qint16>(value, ptr);
    84. }
    85.  
    86. ptr += channelBytes;
    87. length -= channelBytes;
    88. }
    89. ++sampleIndex;
    90. }
    91. }
    92.  
    93. qint32 Generator::readData(char *data, qint32 len)
    94. {
    95. qint32 total = 0;
    96. while (len - total > 0) {
    97. const qint32 chunk = qMin((m_buffer.size() - m_pos), len - total);
    98. memcpy(data + total, m_buffer.constData() + m_pos, chunk);
    99. m_pos = (m_pos + chunk) % m_buffer.size();
    100. total += chunk;
    101. }
    102. return total;
    103. }
    104.  
    105. qint32 Generator::writeData(const char *data, qint32 len)
    106. {
    107. Q_UNUSED(data);
    108. Q_UNUSED(len);
    109.  
    110. return 0;
    111. }
    112.  
    113. qint32 Generator::bytesAvailable() const
    114. {
    115. return m_buffer.size() + QIODevice::bytesAvailable();
    116. }
    117.  
    118. AudioTest::AudioTest()
    119. : m_pullTimer(new QTimer(this))
    120. , m_modeButton(0)
    121. , m_suspendResumeButton(0)
    122. , m_deviceBox(0)
    123. , m_device(QAudioDeviceInfo::defaultOutputDevice())
    124. , m_generator(0)
    125. , m_audioOutput(0)
    126. , m_output(0)
    127. , m_buffer(BufferSize, 0)
    128. {
    129. initializeWindow();
    130. initializeAudio();
    131. }
    132.  
    133. void AudioTest::initializeWindow()
    134. {
    135. QScopedPointer<QWidget> window(new QWidget);
    136. QScopedPointer<QVBoxLayout> layout(new QVBoxLayout);
    137.  
    138. m_deviceBox = new QComboBox(this);
    139. foreach (const QAudioDeviceInfo &deviceInfo, QAudioDeviceInfo::availableDevices(QAudio::AudioOutput))
    140. m_deviceBox->addItem(deviceInfo.deviceName(), qVariantFromValue(deviceInfo));
    141. connect(m_deviceBox,SIGNAL(activated(int)),SLOT(deviceChanged(int)));
    142. layout->addWidget(m_deviceBox);
    143.  
    144. m_modeButton = new QPushButton(this);
    145. m_modeButton->setText(tr(PUSH_MODE_LABEL));
    146. connect(m_modeButton, SIGNAL(clicked()), SLOT(toggleMode()));
    147. layout->addWidget(m_modeButton);
    148.  
    149. m_suspendResumeButton = new QPushButton(this);
    150. m_suspendResumeButton->setText(tr(SUSPEND_LABEL));
    151. connect(m_suspendResumeButton, SIGNAL(clicked()), SLOT(toggleSuspendResume()));
    152. layout->addWidget(m_suspendResumeButton);
    153.  
    154. QHBoxLayout *volumeBox = new QHBoxLayout;
    155. m_volumeLabel = new QLabel;
    156. m_volumeLabel->setText(tr(VOLUME_LABEL));
    157. m_volumeSlider = new QSlider(Qt::Horizontal);
    158. m_volumeSlider->setMinimum(0);
    159. m_volumeSlider->setMaximum(100);
    160. m_volumeSlider->setSingleStep(10);
    161. connect(m_volumeSlider, SIGNAL(valueChanged(int)), this, SLOT(volumeChanged(int)));
    162. volumeBox->addWidget(m_volumeLabel);
    163. volumeBox->addWidget(m_volumeSlider);
    164. layout->addLayout(volumeBox);
    165.  
    166. window->setLayout(layout.data());
    167. layout.take(); // ownership transferred
    168.  
    169. setCentralWidget(window.data());
    170. QWidget *const windowPtr = window.take(); // ownership transferred
    171. windowPtr->show();
    172. }
    173.  
    174. void AudioTest::initializeAudio()
    175. {
    176. connect(m_pullTimer, SIGNAL(timeout()), SLOT(pullTimerExpired()));
    177.  
    178. m_pullMode = true;
    179.  
    180. m_format.setSampleRate(DataSampleRateHz);
    181. m_format.setChannelCount(1);
    182. m_format.setSampleSize(16);
    183. m_format.setCodec("audio/pcm");
    184. m_format.setByteOrder(QAudioFormat::LittleEndian);
    185. m_format.setSampleType(QAudioFormat::SignedInt);
    186.  
    187. QAudioDeviceInfo info(QAudioDeviceInfo::defaultOutputDevice());
    188. if (!info.isFormatSupported(m_format)) {
    189. qWarning() << "Default format not supported - trying to use nearest";
    190. m_format = info.nearestFormat(m_format);
    191. }
    192.  
    193. m_generator = new Generator(m_format, DurationSeconds*1000000, ToneSampleRateHz, this);
    194.  
    195. createAudioOutput();
    196. }
    197.  
    198. void AudioTest::createAudioOutput()
    199. {
    200. delete m_audioOutput;
    201. m_audioOutput = 0;
    202. m_audioOutput = new QAudioOutput(m_device, m_format, this);
    203. connect(m_audioOutput, SIGNAL(notify()), SLOT(notified()));
    204. connect(m_audioOutput, SIGNAL(stateChanged(QAudio::State)), SLOT(handleStateChanged(QAudio::State)));
    205. m_generator->start();
    206. m_audioOutput->start(m_generator);
    207. m_volumeSlider->setValue(int(m_audioOutput->volume()*100.0f));
    208. }
    209.  
    210. AudioTest::~AudioTest()
    211. {
    212.  
    213. }
    214.  
    215. void AudioTest::deviceChanged(int index)
    216. {
    217. m_pullTimer->stop();
    218. m_generator->stop();
    219. m_audioOutput->stop();
    220. m_audioOutput->disconnect(this);
    221. m_device = m_deviceBox->itemData(index).value<QAudioDeviceInfo>();
    222. createAudioOutput();
    223. }
    224.  
    225. void AudioTest::volumeChanged(int value)
    226. {
    227. if (m_audioOutput)
    228. m_audioOutput->setVolume(qreal(value/100.0f));
    229. }
    230.  
    231. void AudioTest::notified()
    232. {
    233. qWarning() << "bytesFree = " << m_audioOutput->bytesFree()
    234. << ", " << "elapsedUSecs = " << m_audioOutput->elapsedUSecs()
    235. << ", " << "processedUSecs = " << m_audioOutput->processedUSecs();
    236. }
    237.  
    238. void AudioTest::pullTimerExpired()
    239. {
    240. if (m_audioOutput && m_audioOutput->state() != QAudio::StoppedState) {
    241. int chunks = m_audioOutput->bytesFree()/m_audioOutput->periodSize();
    242. while (chunks) {
    243. const qint32 len = m_generator->read(m_buffer.data(), m_audioOutput->periodSize());
    244. if (len)
    245. m_output->write(m_buffer.data(), len);
    246. if (len != m_audioOutput->periodSize())
    247. break;
    248. --chunks;
    249. }
    250. }
    251. }
    252.  
    253. void AudioTest::toggleMode()
    254. {
    255. m_pullTimer->stop();
    256. m_audioOutput->stop();
    257.  
    258. if (m_pullMode) {
    259. m_modeButton->setText(tr(PULL_MODE_LABEL));
    260. m_output = m_audioOutput->start();
    261. m_pullMode = false;
    262. m_pullTimer->start(20);
    263. } else {
    264. m_modeButton->setText(tr(PUSH_MODE_LABEL));
    265. m_pullMode = true;
    266. m_audioOutput->start(m_generator);
    267. }
    268.  
    269. m_suspendResumeButton->setText(tr(SUSPEND_LABEL));
    270. }
    271.  
    272. void AudioTest::toggleSuspendResume()
    273. {
    274. if (m_audioOutput->state() == QAudio::SuspendedState) {
    275. qWarning() << "status: Suspended, resume()";
    276. m_audioOutput->resume();
    277. m_suspendResumeButton->setText(tr(SUSPEND_LABEL));
    278. } else if (m_audioOutput->state() == QAudio::ActiveState) {
    279. qWarning() << "status: Active, suspend()";
    280. m_audioOutput->suspend();
    281. m_suspendResumeButton->setText(tr(RESUME_LABEL));
    282. } else if (m_audioOutput->state() == QAudio::StoppedState) {
    283. qWarning() << "status: Stopped, resume()";
    284. m_audioOutput->resume();
    285. m_suspendResumeButton->setText(tr(SUSPEND_LABEL));
    286. } else if (m_audioOutput->state() == QAudio::IdleState) {
    287. qWarning() << "status: IdleState";
    288. }
    289. }
    290.  
    291. void AudioTest::handleStateChanged(QAudio::State state)
    292. {
    293. qWarning() << "state = " << state;
    294. }
    To copy to clipboard, switch view to plain text mode 

  5. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: A question about uint 8 and uint 16 and uint 32 and uint 64

    A qint32 can hold much smaller numbers than a qint64... They may not be directly interchangeable without other changes to the maths. However, we are not gong to disassemble your entire program to determine what you have done to the original example or guess what "does not work" about it. "Does not work" is not a useful description of the problem, a symptom of a problem, the location of a problem, or the desired behaviour.

  6. The following user says thank you to ChrisW67 for this useful post:

    rezas1000 (29th August 2014)

  7. #5
    Join Date
    Apr 2013
    Location
    Prague
    Posts
    258
    Thanks
    3
    Thanked 65 Times in 59 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: A question about uint 8 and uint 16 and uint 32 and uint 64

    (1) If you have replaced qint64 by qint32 then why have you done it? A 32-bit CPU can process 64-bit integers, too. Your (32-bit) Qt will take care for 64-bit arithmetic. No need for replacing qint64 by qint32.
    (2) As to possible errors: Check line 193. I guess "duration" was a 64-bit integer. Because a 32-bit int can hold 4*10e9 at most, the "new Generator" will fail whenever duration > 4000 sec.

  8. The following user says thank you to Radek for this useful post:

    rezas1000 (29th August 2014)

  9. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: A question about uint 8 and uint 16 and uint 32 and uint 64

    Quote Originally Posted by Radek View Post
    Because a 32-bit int can hold 4*10e9 at most, the "new Generator" will fail whenever duration > 4000 sec.
    It is a signed integer so even only half of that.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  10. The following user says thank you to wysota for this useful post:

    rezas1000 (29th August 2014)

  11. #7
    Join Date
    Jul 2014
    Posts
    95
    Thanks
    67

    Default Re: A question about uint 8 and uint 16 and uint 32 and uint 64

    A 32-bit CPU can process 64-bit integers
    Not wrong???

  12. #8
    Join Date
    Apr 2013
    Location
    Prague
    Posts
    258
    Thanks
    3
    Thanked 65 Times in 59 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: A question about uint 8 and uint 16 and uint 32 and uint 64

    Why should it be wrong? It's only a question of a bit of programming (multiple precision arithmetics).

  13. The following user says thank you to Radek for this useful post:

    rezas1000 (29th August 2014)

  14. #9
    Join Date
    Jul 2014
    Posts
    95
    Thanks
    67

    Default Re: A question about uint 8 and uint 16 and uint 32 and uint 64

    My system is 32-bit.
    64-bit applications will not run on my system.

    So in the above code, why should we use qint64?

  15. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: A question about uint 8 and uint 16 and uint 32 and uint 64

    Quote Originally Posted by rezas1000 View Post
    My system is 32-bit.
    64-bit applications will not run on my system.

    So in the above code, why should we use qint64?
    quint64 is just a datatype (mapping to unsigned long long or something similar), it has nothing to do with the width of the address or data bus which is what makes a system "32 bit" or "64 bit". The processor can handle 32 bit as well as 64 bit datatypes and maybe even 128 bit types as well, it is just a matter of efficiency of processing such data.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  16. The following user says thank you to wysota for this useful post:

    rezas1000 (29th August 2014)

  17. #11
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: A question about uint 8 and uint 16 and uint 32 and uint 64

    Exactly!

    A "double" for example is always 64 bit, it is specified that way https://en.wikipedia.org/wiki/Double_precision

    Cheers,
    _

  18. The following user says thank you to anda_skoa for this useful post:

    rezas1000 (29th August 2014)

Similar Threads

  1. converting uint to QString
    By Yayati.Ekbote in forum Newbie
    Replies: 1
    Last Post: 14th April 2010, 13:43
  2. (portion of...) QByteArray to UInt
    By pdoria in forum Qt Programming
    Replies: 3
    Last Post: 25th July 2009, 15:23
  3. Replies: 2
    Last Post: 12th June 2006, 17:47
  4. Invite QDateTime to go 8 secs on future uint
    By patrik08 in forum Newbie
    Replies: 4
    Last Post: 6th June 2006, 23:26
  5. void clearWState( uint n );
    By sunil.thaha in forum Qt Programming
    Replies: 4
    Last Post: 4th February 2006, 16:12

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.