Results 1 to 4 of 4

Thread: Function Help

  1. #1

    Thread Starter
    Frenzied Member vbgladiator's Avatar
    Join Date
    May 2001
    Posts
    1,950

    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,
    Don't anthropomorphize computers -- they hate it

  2. #2
    PowerPoster sunburnt's Avatar
    Join Date
    Feb 2001
    Location
    Boulder, Colorado
    Posts
    1,403
    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;
    Every passing hour brings the Solar System forty-three thousand miles closer to Globular Cluster M13 in Hercules -- and still there are some misfits who insist that there is no such thing as progress.

  3. #3

    Thread Starter
    Frenzied Member vbgladiator's Avatar
    Join Date
    May 2001
    Posts
    1,950
    [00010000] = 0x0F in binary

    Isn't it 1111 in binary?
    Don't anthropomorphize computers -- they hate it

  4. #4
    PowerPoster sunburnt's Avatar
    Join Date
    Feb 2001
    Location
    Boulder, Colorado
    Posts
    1,403
    You're right, it's 15, not 16. That was a stupid mistake.
    Every passing hour brings the Solar System forty-three thousand miles closer to Globular Cluster M13 in Hercules -- and still there are some misfits who insist that there is no such thing as progress.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width