-
Function Help
Hi,
I have a C++ Dll that reads from a string.
i_ch = szRecord[ z_ct++ ];
pd.Demo= i_ch & 0x0F;
pd.Demo1 = (i_ch>>4) & 0x0F;
i_ch = szRecord[ z_ct++ ];
pd.Demo2 = i_ch & 0x1F;
pd.Demo3 = (i_ch>>5) & 0x03;
pd.Demo4 = (i_ch>>7) & 0x01;
i_ch = szRecord[ z_ct++ ];
pd.Demo5 = i_ch & 0x07;
pd.Demo6 = (i_ch>>4) & 0x0F;
szRecord is the string containing the data.
pd is a type.
I don't understand the following:
i_ch = szRecord[z_ct++] - I think it takes byte at a time, not sure though
pd.Demo = i_ch & 0x00F - no clue
pd.Demo3 = (i_ch>>5) & 0x03 - what does the i_ch>>5 mean and what does the & 0x003 in all the lines mean?
Thanks,
-
Code:
// this line:
i_ch = szRecord[ z_ct++ ];
// is the same as:
// i_ch = szRecord[ z_ct ];
// z_ct = z_ct + 1;
//
// & is the bitwise AND operator
pd.Demo= i_ch & 0x0F;
// [01011000] = some random i_ch
// [00010000] = 0x0F in binary
// i_ch is now either 0 or 1, since we are ANDing it with one bit.
// >> is the right shift operator.
// [00010000] = 16 right shifted by 1 (>> 1) == [00001000] = 8.
pd.Demo1 = (i_ch>>4) & 0x0F;
i_ch = szRecord[ z_ct++ ];
pd.Demo2 = i_ch & 0x1F;
pd.Demo3 = (i_ch>>5) & 0x03;
pd.Demo4 = (i_ch>>7) & 0x01;
i_ch = szRecord[ z_ct++ ];
pd.Demo5 = i_ch & 0x07;
pd.Demo6 = (i_ch>>4) & 0x0F;
-
[00010000] = 0x0F in binary
Isn't it 1111 in binary?
-
You're right, it's 15, not 16. That was a stupid mistake. :)