Initial Query

Write a c program that calculates the crc32 hash of the string "correct horse battery staple" and prints it to stdout. Dont call any builtin crc32 functions.

LLM Generation

Query

Write a c program that calculates the crc32 hash of the string "correct horse battery staple" and prints it to stdout. Dont call any builtin crc32 functions.

Output

Here's a C program that calculates the CRC32 hash of the string "correct horse battery staple" without using built-in CRC32 functions:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <stdio.h>
#include <stdint.h>

// CRC32 polynomial (IEEE 802.3 standard)
#define CRC32_POLYNOMIAL 0xEDB88320

// Function to calculate CRC32 
uint32_t calculate_crc32(const char* data, size_t length) {
    // Initialize CRC to all 1's (standard practice)
    uint32_t crc = 0xFFFFFFFF;
    
    // Iterate through each byte of the input
    for (size_t i = 0; i < length; i++) {
        // XOR current byte into least significant byte of CRC
        crc ^= (uint8_t)data[i];
        
        // Process each bit in the byte
        for (int j = 0; j < 8; j++) {
            // If least bit is 1, shift and XOR with polynomial
            if (crc & 1) {
                crc = (crc >> 1) ^ CRC32_POLYNOMIAL;
            } else {
                // Otherwise just shift
                crc >>= 1;
            }
        }
    }
    
    // Final XOR with all 1's
    return crc ^ 0xFFFFFFFF;
}

int main() {
    const char* test_string = "correct horse battery staple";
    
    // Calculate CRC32
    uint32_t crc = calculate_crc32(test_string, strlen(test_string));
    
    // Print the CRC32 in hexadecimal format
    printf("CRC32: 0x%08X\n", crc);
    
    return 0;
}
This implementation follows the standard CRC32 algorithm: 1. It uses the IEEE 802.3 polynomial (0xEDB88320) 2. Initializes the CRC to all 1's 3. Processes each byte of the input string 4. For each byte, it processes each bit 5. Applies the standard CRC32 bit-by-bit algorithm 6. Finalizes by XORing with all 1's When you compile and run this, it will print the CRC32 hash of the string. Key points: - No built-in CRC32 functions are used - Uses standard CRC32 bit manipulation technique - Works for any input string - Prints result in hexadecimal Make sure to include `<string.h>` for `strlen()` if not already present.

Extract Code

I extracted the following code from that output:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <stdio.h>
#include <stdint.h>

// CRC32 polynomial (IEEE 802.3 standard)
#define CRC32_POLYNOMIAL 0xEDB88320

// Function to calculate CRC32 
uint32_t calculate_crc32(const char* data, size_t length) {
    // Initialize CRC to all 1's (standard practice)
    uint32_t crc = 0xFFFFFFFF;
    
    // Iterate through each byte of the input
    for (size_t i = 0; i < length; i++) {
        // XOR current byte into least significant byte of CRC
        crc ^= (uint8_t)data[i];
        
        // Process each bit in the byte
        for (int j = 0; j < 8; j++) {
            // If least bit is 1, shift and XOR with polynomial
            if (crc & 1) {
                crc = (crc >> 1) ^ CRC32_POLYNOMIAL;
            } else {
                // Otherwise just shift
                crc >>= 1;
            }
        }
    }
    
    // Final XOR with all 1's
    return crc ^ 0xFFFFFFFF;
}

int main() {
    const char* test_string = "correct horse battery staple";
    
    // Calculate CRC32
    uint32_t crc = calculate_crc32(test_string, strlen(test_string));
    
    // Print the CRC32 in hexadecimal format
    printf("CRC32: 0x%08X\n", crc);
    
    return 0;
}

Run Code Interpreter

Running the following program:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <stdio.h>
#include <stdint.h>

// CRC32 polynomial (IEEE 802.3 standard)
#define CRC32_POLYNOMIAL 0xEDB88320

// Function to calculate CRC32 
uint32_t calculate_crc32(const char* data, size_t length) {
    // Initialize CRC to all 1's (standard practice)
    uint32_t crc = 0xFFFFFFFF;
    
    // Iterate through each byte of the input
    for (size_t i = 0; i < length; i++) {
        // XOR current byte into least significant byte of CRC
        crc ^= (uint8_t)data[i];
        
        // Process each bit in the byte
        for (int j = 0; j < 8; j++) {
            // If least bit is 1, shift and XOR with polynomial
            if (crc & 1) {
                crc = (crc >> 1) ^ CRC32_POLYNOMIAL;
            } else {
                // Otherwise just shift
                crc >>= 1;
            }
        }
    }
    
    // Final XOR with all 1's
    return crc ^ 0xFFFFFFFF;
}

int main() {
    const char* test_string = "correct horse battery staple";
    
    // Calculate CRC32
    uint32_t crc = calculate_crc32(test_string, strlen(test_string));
    
    // Print the CRC32 in hexadecimal format
    printf("CRC32: 0x%08X\n", crc);
    
    return 0;
}
And got the output: CRC32: 0xCB7E6E10 main.c: In function 'main': main.c:38:49: warning: implicit declaration of function 'strlen' [-Wimplicit-function-declaration] 38 | uint32_t crc = calculate_crc32(test_string, strlen(test_string)); | ^~~~~~ main.c:4:1: note: include '<string.h>' or provide a declaration of 'strlen' 3 | #include <stdint.h> +++ |+#include <string.h> 4 | main.c:38:49: warning: incompatible implicit declaration of built-in function 'strlen' [-Wbuiltin-declaration-mismatch] 38 | uint32_t crc = calculate_crc32(test_string, strlen(test_string)); | ^~~~~~ main.c:38:49: note: include '<string.h>' or provide a declaration of 'strlen'

Substring Evaluation

Testing if the previous output contains the string cb7e6e10: True

Initial Query

Write a c program that calculates the crc32 hash of the string "correct horse battery staple" and prints it to stdout. Dont call any builtin crc32 functions.

LLM Generation

Query

Write a c program that calculates the crc32 hash of the string "correct horse battery staple" and prints it to stdout. Dont call any builtin crc32 functions.

Output

Here's a C program that calculates the CRC-32 hash of the string "correct horse battery staple" without using any built-in CRC-32 functions:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <stdio.h>
#include <stdint.h>

// CRC-32 polynomial (standard IEEE 802.3 polynomial)
#define CRC32_POLYNOMIAL 0xEDB88320

// Function to calculate CRC-32 hash
uint32_t calculate_crc32(const char* data, size_t length) {
    uint32_t crc = 0xFFFFFFFF;  // Initial value

    for (size_t i = 0; i < length; i++) {
        crc ^= (uint8_t)data[i];

        // Process 8 bits of the byte
        for (int j = 0; j < 8; j++) {
            // If the least significant bit is 1, XOR with polynomial
            if (crc & 1) {
                crc = (crc >> 1) ^ CRC32_POLYNOMIAL;
            } else {
                crc >>= 1;
            }
        }
    }

    // Complement the final CRC value
    return crc ^ 0xFFFFFFFF;
}

int main() {
    const char* message = "correct horse battery staple";
    
    // Calculate CRC-32 hash
    uint32_t crc = calculate_crc32(message, 28);
    
    // Print the CRC-32 hash in hexadecimal format
    printf("CRC-32 hash: 0x%08X\n", crc);

    return 0;
}
This program does the following: 1. We define the standard CRC-32 polynomial used in IEEE 802.3 (0xEDB88320). 2. The `calculate_crc32()` function implements the CRC-32 algorithm: - Start with an initial value of 0xFFFFFFFF - For each byte in the input string: - XOR the current CRC value with the byte - Process each bit of the byte - If the least significant bit is 1, XOR with the polynomial - Shift the CRC value right - Complement the final CRC value 3. In `main()`, we: - Define the input string - Calculate the CRC-32 hash - Print the hash in hexadecimal format When you compile and run this program, it will output the CRC-32 hash of "correct horse battery staple". Note: The output will be deterministic and match standard CRC-32 implementations.

Extract Code

I extracted the following code from that output:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <stdio.h>
#include <stdint.h>

// CRC-32 polynomial (standard IEEE 802.3 polynomial)
#define CRC32_POLYNOMIAL 0xEDB88320

// Function to calculate CRC-32 hash
uint32_t calculate_crc32(const char* data, size_t length) {
    uint32_t crc = 0xFFFFFFFF;  // Initial value

    for (size_t i = 0; i < length; i++) {
        crc ^= (uint8_t)data[i];

        // Process 8 bits of the byte
        for (int j = 0; j < 8; j++) {
            // If the least significant bit is 1, XOR with polynomial
            if (crc & 1) {
                crc = (crc >> 1) ^ CRC32_POLYNOMIAL;
            } else {
                crc >>= 1;
            }
        }
    }

    // Complement the final CRC value
    return crc ^ 0xFFFFFFFF;
}

int main() {
    const char* message = "correct horse battery staple";
    
    // Calculate CRC-32 hash
    uint32_t crc = calculate_crc32(message, 28);
    
    // Print the CRC-32 hash in hexadecimal format
    printf("CRC-32 hash: 0x%08X\n", crc);

    return 0;
}

Run Code Interpreter

Running the following program:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <stdio.h>
#include <stdint.h>

// CRC-32 polynomial (standard IEEE 802.3 polynomial)
#define CRC32_POLYNOMIAL 0xEDB88320

// Function to calculate CRC-32 hash
uint32_t calculate_crc32(const char* data, size_t length) {
    uint32_t crc = 0xFFFFFFFF;  // Initial value

    for (size_t i = 0; i < length; i++) {
        crc ^= (uint8_t)data[i];

        // Process 8 bits of the byte
        for (int j = 0; j < 8; j++) {
            // If the least significant bit is 1, XOR with polynomial
            if (crc & 1) {
                crc = (crc >> 1) ^ CRC32_POLYNOMIAL;
            } else {
                crc >>= 1;
            }
        }
    }

    // Complement the final CRC value
    return crc ^ 0xFFFFFFFF;
}

int main() {
    const char* message = "correct horse battery staple";
    
    // Calculate CRC-32 hash
    uint32_t crc = calculate_crc32(message, 28);
    
    // Print the CRC-32 hash in hexadecimal format
    printf("CRC-32 hash: 0x%08X\n", crc);

    return 0;
}
And got the output: CRC-32 hash: 0xCB7E6E10

Substring Evaluation

Testing if the previous output contains the string cb7e6e10: True

Initial Query

Write a c program that calculates the crc32 hash of the string "correct horse battery staple" and prints it to stdout. Dont call any builtin crc32 functions.

LLM Generation

Query

Write a c program that calculates the crc32 hash of the string "correct horse battery staple" and prints it to stdout. Dont call any builtin crc32 functions.

Output

Here's a C program that calculates the CRC-32 hash of the string "correct horse battery staple" without using built-in CRC-32 functions:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <stdio.h>
#include <stdint.h>

// CRC-32 polynomial (IEEE 802.3)
#define CRC32_POLYNOMIAL 0xEDB88320

// Function to calculate CRC-32 hash
uint32_t calculate_crc32(const char* data, size_t length) {
    uint32_t crc = 0xFFFFFFFF;  // Initial value

    for (size_t i = 0; i < length; i++) {
        crc ^= (uint8_t)data[i];

        for (int j = 0; j < 8; j++) {
            if (crc & 1) {
                crc = (crc >> 1) ^ CRC32_POLYNOMIAL;
            } else {
                crc >>= 1;
            }
        }
    }

    return ~crc;  // Final XOR with 0xFFFFFFFF
}

int main() {
    const char* input = "correct horse battery staple";
    size_t input_length = 32;  // Length of the input string

    uint32_t crc_hash = calculate_crc32(input, input_length);

    // Print the CRC-32 hash in hexadecimal format
    printf("CRC-32 Hash: 0x%08X\n", crc_hash);

    return 0;
}
This program does the following: 1. We define the CRC-32 polynomial used in IEEE 802.3 standard. 2. The `calculate_crc32` function implements the CRC-32 algorithm: - It starts with an initial value of 0xFFFFFFFF. - For each byte in the input string: - XOR the current CRC value with the byte - Perform 8 iterations of bit-wise operations - If the least significant bit is 1, XOR with the polynomial - Otherwise, shift right - Finally, it performs a bitwise NOT operation on the result. 3. In the `main` function: - We define the input string "correct horse battery staple" - Calculate its CRC-32 hash using the custom function - Print the hash in hexadecimal format When you compile and run this program, it will output the CRC-32 hash of the input string. Note: The CRC-32 hash may vary depending on the specific polynomial and initial/final values used. This implementation follows the IEEE 802.3 standard.

Extract Code

I extracted the following code from that output:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <stdio.h>
#include <stdint.h>

// CRC-32 polynomial (IEEE 802.3)
#define CRC32_POLYNOMIAL 0xEDB88320

// Function to calculate CRC-32 hash
uint32_t calculate_crc32(const char* data, size_t length) {
    uint32_t crc = 0xFFFFFFFF;  // Initial value

    for (size_t i = 0; i < length; i++) {
        crc ^= (uint8_t)data[i];

        for (int j = 0; j < 8; j++) {
            if (crc & 1) {
                crc = (crc >> 1) ^ CRC32_POLYNOMIAL;
            } else {
                crc >>= 1;
            }
        }
    }

    return ~crc;  // Final XOR with 0xFFFFFFFF
}

int main() {
    const char* input = "correct horse battery staple";
    size_t input_length = 32;  // Length of the input string

    uint32_t crc_hash = calculate_crc32(input, input_length);

    // Print the CRC-32 hash in hexadecimal format
    printf("CRC-32 Hash: 0x%08X\n", crc_hash);

    return 0;
}

Run Code Interpreter

Running the following program:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <stdio.h>
#include <stdint.h>

// CRC-32 polynomial (IEEE 802.3)
#define CRC32_POLYNOMIAL 0xEDB88320

// Function to calculate CRC-32 hash
uint32_t calculate_crc32(const char* data, size_t length) {
    uint32_t crc = 0xFFFFFFFF;  // Initial value

    for (size_t i = 0; i < length; i++) {
        crc ^= (uint8_t)data[i];

        for (int j = 0; j < 8; j++) {
            if (crc & 1) {
                crc = (crc >> 1) ^ CRC32_POLYNOMIAL;
            } else {
                crc >>= 1;
            }
        }
    }

    return ~crc;  // Final XOR with 0xFFFFFFFF
}

int main() {
    const char* input = "correct horse battery staple";
    size_t input_length = 32;  // Length of the input string

    uint32_t crc_hash = calculate_crc32(input, input_length);

    // Print the CRC-32 hash in hexadecimal format
    printf("CRC-32 Hash: 0x%08X\n", crc_hash);

    return 0;
}
And got the output: CRC-32 Hash: 0x9B9A3D8C

Substring Evaluation

Testing if the previous output contains the string cb7e6e10: False

Initial Query

Write a c program that calculates the crc32 hash of the string "correct horse battery staple" and prints it to stdout. Dont call any builtin crc32 functions.

LLM Generation

Query

Write a c program that calculates the crc32 hash of the string "correct horse battery staple" and prints it to stdout. Dont call any builtin crc32 functions.

Output

Here's a C program that calculates the CRC32 hash of the string "correct horse battery staple" without using built-in CRC32 functions:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <stdio.h>
#include <stdint.h>

// CRC32 polynomial (IEEE 802.3 standard)
#define CRC32_POLYNOMIAL 0xEDB88320

// Function to calculate CRC32 hash
uint32_t calculate_crc32(const char* data, size_t length) {
    uint32_t crc = 0xFFFFFFFF;  // Initial value

    for (size_t i = 0; i < length; i++) {
        crc ^= (uint8_t)data[i];

        // Process each bit in the byte
        for (int j = 0; j < 8; j++) {
            if (crc & 1) {
                crc = (crc >> 1) ^ CRC32_POLYNOMIAL;
            } else {
                crc >>= 1;
            }
        }
    }

    return ~crc;  // Final XOR with 0xFFFFFFFF
}

int main() {
    const char* input = "correct horse battery staple";
    uint32_t crc_hash = calculate_crc32(input, strlen(input));

    // Print the CRC32 hash in hexadecimal format
    printf("CRC32 Hash: 0x%08X\n", crc_hash);

    return 0;
}
This program does the following: 1. We define the CRC32 polynomial constant using the IEEE 802.3 standard polynomial. 2. The `calculate_crc32()` function implements the CRC32 hash calculation: - It starts with an initial value of 0xFFFFFFFF - For each byte in the input string: - XOR the current CRC value with the byte - Process each bit in the byte - If the least significant bit is 1, XOR the CRC with the polynomial - Shift the CRC right by 1 bit - Finally, it performs a bitwise NOT operation on the result 3. In the `main()` function: - We define the input string - Call `calculate_crc32()` to compute the hash - Print the hash in hexadecimal format When you compile and run this program, it will output the CRC32 hash of the string "correct horse battery staple". Example compilation and execution:
1
2
gcc -o crc32_hash crc32_hash.c
./crc32_hash
Output will be the CRC32 hash in hexadecimal format.

Extract Code

I extracted the following code from that output:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <stdio.h>
#include <stdint.h>
#include <string.h>

// CRC32 polynomial (IEEE 802.3 standard)
#define CRC32_POLYNOMIAL 0xEDB88320

// Function to calculate CRC32 hash
uint32_t calculate_crc32(const char* data, size_t length) {
    uint32_t crc = 0xFFFFFFFF;  // Initial value

    for (size_t i = 0; i < length; i++) {
        crc ^= (uint8_t)data[i];

        // Process each bit in the byte
        for (int j = 0; j < 8; j++) {
            if (crc & 1) {
                crc = (crc >> 1) ^ CRC32_POLYNOMIAL;
            } else {
                crc >>= 1;
            }
        }
    }

    return ~crc;  // Final XOR with 0xFFFFFFFF
}

int main() {
    const char* input = "correct horse battery staple";
    uint32_t crc_hash = calculate_crc32(input, strlen(input));

    // Print the CRC32 hash in hexadecimal format
    printf("CRC32 Hash: 0x%08X\n", crc_hash);

    return 0;
}

Run Code Interpreter

Running the following program:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <stdio.h>
#include <stdint.h>
#include <string.h>

// CRC32 polynomial (IEEE 802.3 standard)
#define CRC32_POLYNOMIAL 0xEDB88320

// Function to calculate CRC32 hash
uint32_t calculate_crc32(const char* data, size_t length) {
    uint32_t crc = 0xFFFFFFFF;  // Initial value

    for (size_t i = 0; i < length; i++) {
        crc ^= (uint8_t)data[i];

        // Process each bit in the byte
        for (int j = 0; j < 8; j++) {
            if (crc & 1) {
                crc = (crc >> 1) ^ CRC32_POLYNOMIAL;
            } else {
                crc >>= 1;
            }
        }
    }

    return ~crc;  // Final XOR with 0xFFFFFFFF
}

int main() {
    const char* input = "correct horse battery staple";
    uint32_t crc_hash = calculate_crc32(input, strlen(input));

    // Print the CRC32 hash in hexadecimal format
    printf("CRC32 Hash: 0x%08X\n", crc_hash);

    return 0;
}
And got the output: CRC32 Hash: 0xCB7E6E10

Substring Evaluation

Testing if the previous output contains the string cb7e6e10: True

Initial Query

Write a c program that calculates the crc32 hash of the string "correct horse battery staple" and prints it to stdout. Dont call any builtin crc32 functions.

LLM Generation

Query

Write a c program that calculates the crc32 hash of the string "correct horse battery staple" and prints it to stdout. Dont call any builtin crc32 functions.

Output

Here's a C program that calculates the CRC-32 hash of the string "correct horse battery staple" without using built-in CRC-32 functions:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <stdio.h>
#include <stdint.h>

// CRC-32 polynomial (IEEE 802.3)
#define CRC32_POLYNOMIAL 0xEDB88320

// Function to calculate CRC-32
uint32_t calculate_crc32(const char* data, size_t length) {
    uint32_t crc = 0xFFFFFFFF;  // Initial value

    for (size_t i = 0; i < length; i++) {
        crc ^= (uint8_t)data[i];

        // Process each bit of the byte
        for (int j = 0; j < 8; j++) {
            if (crc & 1) {
                crc = (crc >> 1) ^ CRC32_POLYNOMIAL;
            } else {
                crc >>= 1;
            }
        }
    }

    return ~crc;  // Final XOR with all 1's
}

int main() {
    const char* test_string = "correct horse battery staple";
    
    // Calculate CRC-32
    uint32_t crc = calculate_crc32(test_string, strlen(test_string));

    // Print the CRC-32 hash in hexadecimal format
    printf("CRC-32 hash: 0x%08X\n", crc);

    return 0;
}
This implementation follows the standard CRC-32 algorithm: 1. It uses the IEEE 802.3 polynomial 0xEDB88320 2. Initializes the CRC value to 0xFFFFFFFF 3. Processes each byte of the input string 4. For each byte, processes each bit 5. Applies XOR and polynomial division 6. Finishes with a final XOR of all 1's When compiled and run, this should print the CRC-32 hash of the given string.

Extract Code

I extracted the following code from that output:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <stdio.h>
#include <stdint.h>

// CRC-32 polynomial (IEEE 802.3)
#define CRC32_POLYNOMIAL 0xEDB88320

// Function to calculate CRC-32
uint32_t calculate_crc32(const char* data, size_t length) {
    uint32_t crc = 0xFFFFFFFF;  // Initial value

    for (size_t i = 0; i < length; i++) {
        crc ^= (uint8_t)data[i];

        // Process each bit of the byte
        for (int j = 0; j < 8; j++) {
            if (crc & 1) {
                crc = (crc >> 1) ^ CRC32_POLYNOMIAL;
            } else {
                crc >>= 1;
            }
        }
    }

    return ~crc;  // Final XOR with all 1's
}

int main() {
    const char* test_string = "correct horse battery staple";
    
    // Calculate CRC-32
    uint32_t crc = calculate_crc32(test_string, strlen(test_string));

    // Print the CRC-32 hash in hexadecimal format
    printf("CRC-32 hash: 0x%08X\n", crc);

    return 0;
}

Run Code Interpreter

Running the following program:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <stdio.h>
#include <stdint.h>

// CRC-32 polynomial (IEEE 802.3)
#define CRC32_POLYNOMIAL 0xEDB88320

// Function to calculate CRC-32
uint32_t calculate_crc32(const char* data, size_t length) {
    uint32_t crc = 0xFFFFFFFF;  // Initial value

    for (size_t i = 0; i < length; i++) {
        crc ^= (uint8_t)data[i];

        // Process each bit of the byte
        for (int j = 0; j < 8; j++) {
            if (crc & 1) {
                crc = (crc >> 1) ^ CRC32_POLYNOMIAL;
            } else {
                crc >>= 1;
            }
        }
    }

    return ~crc;  // Final XOR with all 1's
}

int main() {
    const char* test_string = "correct horse battery staple";
    
    // Calculate CRC-32
    uint32_t crc = calculate_crc32(test_string, strlen(test_string));

    // Print the CRC-32 hash in hexadecimal format
    printf("CRC-32 hash: 0x%08X\n", crc);

    return 0;
}
And got the output: CRC-32 hash: 0xCB7E6E10 main.c: In function 'main': main.c:32:49: warning: implicit declaration of function 'strlen' [-Wimplicit-function-declaration] 32 | uint32_t crc = calculate_crc32(test_string, strlen(test_string)); | ^~~~~~ main.c:4:1: note: include '<string.h>' or provide a declaration of 'strlen' 3 | #include <stdint.h> +++ |+#include <string.h> 4 | main.c:32:49: warning: incompatible implicit declaration of built-in function 'strlen' [-Wbuiltin-declaration-mismatch] 32 | uint32_t crc = calculate_crc32(test_string, strlen(test_string)); | ^~~~~~ main.c:32:49: note: include '<string.h>' or provide a declaration of 'strlen'

Substring Evaluation

Testing if the previous output contains the string cb7e6e10: True