Calculate XOR of array of bytes

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tuananh87vn
    New Member
    • Sep 2007
    • 63

    #1

    Calculate XOR of array of bytes

    Hi a,

    I'm writing a C function to calculate the XOR of all members in an array of bytes. Each member should be a 512-byte block.

    Here is what i'm figuring how to write this function.

    the parameter includes the input array of 512-byte blocks
    the return result should be an string (char* or char[512])

    i've started the code as below:

    Code:
    char* calXOR(char* arr[512]) {
        char* xr;
    
        if (sizeof(arr) < 2) { // if there is only one segment payload
            xr = arr[0];
        }
        else {
            if (sizeof(arr) == 2) { // if there are payload from two segments
                xr = arr[0] ^ arr[1];
            }
            else {
                if (sizeof(arr) > 2) { // if there are payload from more than 2 segments
                    xr = arr[0] ^ arr[1];
                    int i;
                    for (i = 2; i < sizeof(arr); i ++) {
                        xr = xr ^ arr[i];
                    }
                }
            }
        }
    
        return xr;
    
    }
    Hmm, the code is not working as I write it down but i'm trying to figure it out because i am not so familiar with handling data type and size in C.

    Would be really grateful if somebody can fix these.

    Thank you very much!
  • donbock
    Recognized Expert Top Contributor
    • Mar 2008
    • 2427

    #2
    You define parameter arr as an array of 512 char pointers. However, C doesn't work that way. The compiler silently throws away the dimension (512). In truth, parameter arr is a pointer to a char pointer. Thus, sizeof(arr) is the size of a pointer; and bears no relation to the number of elements in the array.

    The typical approach is to pass two parameters: one that points to the beginning of the array; and another that specifies the number of elements in the array. The caller may be able to use sizeof to compute the value of the second parameter.

    Comment

    • donbock
      Recognized Expert Top Contributor
      • Mar 2008
      • 2427

      #3
      Lines 5, 9, 13, and 16 exclusive-or the array elements together. The array elements are pointers. Did you want to exclusive-or pointers or the chars that are being pointed at?

      Comment

      • Corsari
        New Member
        • Nov 2014
        • 2

        #4
        I was seeking for the same question,
        unfortunately I can't read a real answer.
        Is it possible for you to share the fastest and efficient way to check the XOR result of all the bytes in an array?
        The array would be filled with ASCII HEX values.
        Thank you
        Cor

        Comment

        • donbock
          Recognized Expert Top Contributor
          • Mar 2008
          • 2427

          #5
          Do you want to XOR the hex-ASCII characters or the binary values represented by those characters?

          For example, consider array 'A', '1', 'b', '0':
          XOR of the characters is 0x41 XOR 0x31 XOR 0x62 XOR 0x30 = 0x22.
          XOR of nibble values represented by characters = 0xA XOR 0x1 XOR 0xB XOR 0x0 = 0x0.

          What are you trying to do?

          Comment

          • Corsari
            New Member
            • Nov 2014
            • 2

            #6
            Hello Don! Thank you for the interest

            I'm going to process a data flow which will be get from an array buffer.

            The first exercise is to count how many chunks/buffers/arrays are "blank" so which the content are all x00 bytes.

            So I was wondering:
            instead than running a "for" or a "while" cycle to compare every single byte in that array, it could be faster to instant XOR the buffer content and check if the result is 0.

            Or the question could turn to:
            Which could it be the faster and less cycles consuming process to verify that an binary array content is all x00?

            Thank you

            Cor

            Comment

            • donbock
              Recognized Expert Top Contributor
              • Mar 2008
              • 2427

              #7
              The most reliable way is to compare each byte to 0. The XOR approach you suggest is not reliable -- there are many cases where it will give the wrong answer.

              Many processors perform implicit comparison to zero very quickly; a good compiler will take advantage of that.

              Comment

              Working...