简体   繁体   English

C / C ++按位“或”及其解释

[英]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. 我正在使用库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. 我读取了一个24位的wav音频文件。该api提供了一种字段格式,该格式设置为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. 上面的SF_INFO结构中的format字段由主要格式类型(值在0x10000到0x08000000之间),次要格式类型(值小于0x10000)和可选的字节序值组成。

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 ? 我的大问题是:如何分析格式= 65539以区分主要类型和子类型?

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 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. 您需要确定感兴趣的不同部分的位范围。在列表中,有2个十六进制数字专用于主要类型,而4个数字专用于子类型。 Use AND ( & ) to break them apart. 使用AND( & )将它们分开。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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