Simulating C code

This is a discussion on Simulating C code within the C Programming forums, part of the General Programming Boards category; I am trying to simulate the following code in excel, with cells as bits. It would be nice to just ...

  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    6

    Simulating C code

    I am trying to simulate the following code in excel, with cells as bits. It would be nice to just line up different rows (Data, FrameCheckSequence, GeneratorPolynomial, etc) and then do the bit operations on the cells (columnwise), shifting until one arrives at the final result. When I do this, however, I don't seem to obtain the desired answer. Trying to figure out why not.

    CRC11GenerateFrameCheckSequence
    /*
    Inputs: ByteArrayPtr is the address of a 13 bit array holding 102 bits of data. The two most significant bits must be 0.

    Outputs: 11 bit frame check sequence.
    */
    Code:
    Extern unsigned short CRC11GenerateFrameCheckSequence( unsigned char * ByteArrayPtr )
    { 
       unsigned short     GeneratorPolynomial = 0x0F35;
       unsigned short     FrameCheckSequence = 0x07FF;
       unsigned short     Data;
       int                        ByteIndex, Bit;
    
       /* Do most significant byte skipping the 2 most significant bits */
       Data =  *ByteArrayPtr << 5;
       ByteArrayPtr++
    
       for ( Bit = 2;  Bit < 8;  Bit++ )
      {
          if  ( (FrameCheckSequence ^ Data) & 0x400 )  
             FrameCheckSequence = (FrameCheckSequence << 1) ^ GeneratorPolynomial;
          else
              FrameCheckSequence = (FrameCheckSequence << 1);
          FrameCheckSequence &=  0x7FF;
          Data <<=1;
      }
    
       /* Do rest of the bytes */
       for ( ByteIndex = 1; ByteIndex < 13; ByteIndex++ )
       {
          Data = *ByteArrayPtr << 3;
          ByteArrayPtr++;
          for ( Bit = 0; Bit < 8; Bit++ )
          {
             if (  (FrameCheckSequence ^ Data) & 0x0400 )  
                FrameCheckSequence = (FrameCheckSequence << 1) ^ GeneratorPolynomial;
             else
                FrameCheckSequence = (FrameCheckSequence << 1);
             FrameCheckSequence &=  0x7FF;
             Data <<=1;
          }    
        }
        return FrameCheckSequence;

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Haven't gotten very far in reading your code, but this can't be right:
    Code:
     /* Do most significant byte skipping the 2 most significant bits */
       Data =  *ByteArrayPtr << 5;
    Edit: Oh, wait, maybe it can if the comment doesn't have anything to do with the next bit of code. Well, in that case I would double-check your algorithm.
    Last edited by tabstop; 07-05-2009 at 07:31 PM.

  3. #3
    Registered User
    Join Date
    Jul 2009
    Posts
    6
    Quote Originally Posted by tabstop View Post
    Haven't gotten very far in reading your code, but this can't be right:
    Code:
     /* Do most significant byte skipping the 2 most significant bits */
       Data =  *ByteArrayPtr << 5;
    Edit: Oh, wait, maybe it can if the comment doesn't have anything to do with the next bit of code. Well, in that case I would double-check your algorithm.
    The C code is not mine, so I have to interpret it also. If the C code itself has problems (or even typos), that would be a start.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Hang on. What version (if any) is the "working, correct" version and what version (if any) is the version you need to fix.

  5. #5
    Registered User
    Join Date
    Jul 2009
    Posts
    6
    Quote Originally Posted by tabstop View Post
    Hang on. What version (if any) is the "working, correct" version and what version (if any) is the version you need to fix.
    The C code (not mine) is supposed to be a working version of a Cyclical Redundancy Check. I will need to move this code to a different environment, but I was initially trying to simulate its action on a spreadsheet.

    So I thought I would represent bits as cells on the spreadsheet (0s and 1s), spread the data across as rows (for example, 102 columns in the first row to represent the data), and then interpret the code as bitwise operations (for each column), and deleting a leading cell to shift left ( filling in a trailing cell if necessary).

    When I did this, apparently my logic (I wrote a macro to do it) differs from the logic of the c code, because my results do not match the examples I have. Or perhaps the logic is okay but my initial lineup of variables is different. It seems like it should be fairly straightforward, but the danged answers come out different. I am trying to figure out just what I am doing wrong.
    Last edited by penrose; 07-06-2009 at 08:06 AM.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Well, I don't know what you've done. I'm guessing/hoping you know what all the symbols mean? If you're doing things "by hand" you don't really have to worry (much) about the << 3 and the 0x400, as I think that's there just to get things away from the sign bit and to line things up so that you get 11 bits of answer (so you can just interpret it bit by bit as you're looking at it in the cells as long as you realize your answer is always 11 bits, I think).

  7. #7
    Registered User
    Join Date
    Jul 2009
    Posts
    6
    Quote Originally Posted by tabstop View Post
    Well, I don't know what you've done. I'm guessing/hoping you know what all the symbols mean? If you're doing things "by hand" you don't really have to worry (much) about the << 3 and the 0x400, as I think that's there just to get things away from the sign bit and to line things up so that you get 11 bits of answer (so you can just interpret it bit by bit as you're looking at it in the cells as long as you realize your answer is always 11 bits, I think).
    Here is an example of what I did: FCS is Frame Check sequence.

    Data: . . . . . . . . 0 0 0 0 0 0 0 0 1 0 1 1 0 0 0 0 1 0 1 0 1 1 0 0 0 1 1 . . . . . . . . . . . . .etc. (102 bits long)
    FCS: . . . . . . . . 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 (16 bits long, initial value = 0x07FF)

    So when it says FCS ^ Data, I do an XOR col by col for 16 cols. Not sure if this is what the c code says, or if I need the four leading zeros on the FCS to begin with. After that, it says & 0x400. That will single out one col, but is 0x400 = 010000000000 (12 bits long)?

    I start out by hand just to get the logic, then write a macro to actually evaluate the final result.

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Doesn't matter whether you think of it as 12 or 16 bits, I don't think, as the first four should always be zero either way.

    ^ is xor, right, and & is and. So you don't need to do column by column, you just need to check that one digit that you and with. If it's a 1, shift over and xor with the polynomial thing; if it's a zero, just shift over.

  9. #9
    Registered User
    Join Date
    Jul 2009
    Posts
    6
    Quote Originally Posted by tabstop View Post
    Doesn't matter whether you think of it as 12 or 16 bits, I don't think, as the first four should always be zero either way.

    ^ is xor, right, and & is and. So you don't need to do column by column, you just need to check that one digit that you and with. If it's a 1, shift over and xor with the polynomial thing; if it's a zero, just shift over.
    Agreed. So you think it can look like this:

    Data: . . . . . . . . 0 0 0 0 0 0 0 0 1 0 1 1 0 0 0 0 1 0 1 0 1 1 0 0 0 1 1 . . . . . . . . . . . . .etc. (102 bits long)
    FCS: . . . . . . . . 0 1 1 1 1 1 1 1 1 1 1 1 (12 bits long, initial value)
    0x400 . . . . . . . . 0 1 0 0 0 0 0 0 0 0 0 0 (12 bits long)

    and the one col I check is the 2nd col? And when I shift and xor with the generator, I should do a 12 col xor?

    Generator = 1 1 1 1 0 0 1 1 0 1 0 1 = F35?

  10. #10
    Registered User
    Join Date
    Jul 2009
    Posts
    6
    Quote Originally Posted by penrose View Post
    Agreed. So you think it can look like this:

    Data: . . . . . . . . 0 0 0 0 0 0 0 0 1 0 1 1 0 0 0 0 1 0 1 0 1 1 0 0 0 1 1 . . . . . . . . . . . . .etc. (102 bits long)
    FCS: . . . . . . . . 0 1 1 1 1 1 1 1 1 1 1 1 (12 bits long, initial value)
    0x400 . . . . . . . . 0 1 0 0 0 0 0 0 0 0 0 0 (12 bits long)

    and the one col I check is the 2nd col? And when I shift and xor with the generator, I should do a 12 col xor?

    Generator = 1 1 1 1 0 0 1 1 0 1 0 1 = F35?
    Still can't make my answers agree with the "official" answer. Somehow my variables are not correctly aligned or not the right length or something else is going on.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extended ASCII Characters in an RTF Control
    By JustMax in forum C Programming
    Replies: 18
    Last Post: 04-03-2009, 08:20 PM
  2. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  3. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 03:17 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 05:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21