简体   繁体   中英

C/C++ Bitwise OR and how to interpret it

I have absolutely no idea about bitwise OR stuff.

So here is my problem. I am using a library libsndfile. I use it to read an analyze audio files. I need to tell the bitrate of the audio file.

I read a 24 bit wav audio file in. The api provides a field format, which is set to format = 65539.

The documentation tells me this:

The format field in the above SF_INFO structure is made up of the bit-wise OR of a major format type (values between 0x10000 and 0x08000000), a minor format type (with values less than 0x10000) and an optional endian-ness value.

Here are some possible values:

      SF_FORMAT_WAV          = 0x010000,     /* Microsoft WAV format (little endian). */
      SF_FORMAT_AIFF         = 0x020000,     /* Apple/SGI AIFF format (big endian). */
      SF_FORMAT_AU           = 0x030000,     /* Sun/NeXT AU format (big endian). */
      SF_FORMAT_RAW          = 0x040000,     /* RAW PCM data. */
      SF_FORMAT_PAF          = 0x050000,     /* Ensoniq PARIS file format. */
      SF_FORMAT_SVX          = 0x060000,     /* Amiga IFF / SVX8 / SV16 format. */
      SF_FORMAT_NIST         = 0x070000,     /* Sphere NIST format. */
      SF_FORMAT_VOC          = 0x080000,     /* VOC files. */
      SF_FORMAT_IRCAM        = 0x0A0000,     /* Berkeley/IRCAM/CARL */
      SF_FORMAT_W64          = 0x0B0000,     /* Sonic Foundry's 64 bit RIFF/WAV */
      SF_FORMAT_MAT4         = 0x0C0000,     /* Matlab (tm) V4.2 / GNU Octave 2.0 */
      SF_FORMAT_MAT5         = 0x0D0000,     /* Matlab (tm) V5.0 / GNU Octave 2.1 */
      SF_FORMAT_PVF          = 0x0E0000,     /* Portable Voice Format */
      SF_FORMAT_XI           = 0x0F0000,     /* Fasttracker 2 Extended Instrument */
      SF_FORMAT_HTK          = 0x100000,     /* HMM Tool Kit format */
      SF_FORMAT_SDS          = 0x110000,     /* Midi Sample Dump Standard */
      SF_FORMAT_AVR          = 0x120000,     /* Audio Visual Research */
      SF_FORMAT_WAVEX        = 0x130000,     /* MS WAVE with WAVEFORMATEX */
      SF_FORMAT_SD2          = 0x160000,     /* Sound Designer 2 */
      SF_FORMAT_FLAC         = 0x170000,     /* FLAC lossless file format */
      SF_FORMAT_CAF          = 0x180000,     /* Core Audio File format */
      SF_FORMAT_WVE          = 0x190000,     /* Psion WVE format */
      SF_FORMAT_OGG          = 0x200000,     /* Xiph OGG container */
      SF_FORMAT_MPC2K        = 0x210000,     /* Akai MPC 2000 sampler */
      SF_FORMAT_RF64         = 0x220000,     /* RF64 WAV file */

      /* Subtypes from here on. */

      SF_FORMAT_PCM_S8       = 0x0001,       /* Signed 8 bit data */
      SF_FORMAT_PCM_16       = 0x0002,       /* Signed 16 bit data */
      SF_FORMAT_PCM_24       = 0x0003,       /* Signed 24 bit data */
      SF_FORMAT_PCM_32       = 0x0004,       /* Signed 32 bit data */

My big question is: How can I analyze the format = 65539 to tell the major type and subtype ?

thanks so much in advance.

Just plug it into your calculator on your desktop and convert to hex:

65539 == 0x010003

Which is to say:

65539 == (0x010000) | (0x3)

which looks like SF_FORMAT_WAV | SF_FORMAT_PCM_24 SF_FORMAT_WAV | SF_FORMAT_PCM_24 .

You need to determine the bit ranges of the different pieces you're interested in. Looking at the list, there are 2 hex digits devoted to the major type and 4 digits to the subtype. Use AND ( & ) to break them apart.

major = type & 0xff0000;
subtype = type & 0x00ffff;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM