Question arising in a writing-Wav function

May 26, 2012 at 8:09pm
Hi fellows,

Quick question: I've got this function example to write a .wav file. After spending some time, I've managed to understand almost everything, but this:
1
2
3
4
5
6
7
8
9
10
11
12
int writeWav (vector<short int> pData, int pSr, int pChannels) {
//...
char * _data;
int bitsPerSample = 8;
int _subChunk2Size=pData.size()*_bitsPerSample/8;
_data=new char[_subChunk2Size];
int cnt=0;
for(int i=0; i<pData.size(); i++) { 
   _data[cnt++]=pData[i] & 0xff; //WHAT EXACTLY DOES THIS
   _data[cnt++]=(pData[i] >> 8) & 0xff; //AND THIS LINES?
}
}


More precisely, I don't understand why '& 0xff' is for, and what (pData[i] >> 8) means (what exactly the operator '>>' is for).

(being pData a vector of short int containing all audio samples, of course)

Thanks!
Last edited on May 26, 2012 at 8:10pm
May 26, 2012 at 8:25pm
Look up the bitwise operators and it should become clearer.
May 27, 2012 at 10:16am
aha!

Well, what I think I understood is that '& 0xff' makes the assignment only for the last 8 bits. After that, the first 8 bits are moved to the right. Repeating the operation of assigning only the last 8 bits, now we have translated the short int (2 bytes) of 'pData[i]' to an array of 2 chars, the first of them containing the last byte of the short int, and the second one containing the first. (this makes sense since wav files are in little endian...)

Can anyone point me if I'm right? I'm sure it has something to do with this, but perhaps my conclusions are bad.

BTW, thanks firedraco for the tip!
Last edited on May 27, 2012 at 10:17am
Topic archived. No new replies allowed.