In general, a checksum is a mathematical function that is used to verify the integrity of data. In this blog post, we will try to see how to calculate checksum manually at the sender side from scratch using an example supplemented by a C program to implement checksum.
It is a technique for detecting errors that may occur when transmitting or storing data. A checksum is typically a fixedlength sequence of numbers or characters that is calculated from a block of data.
To know how to calculate checksum, the data is processed through an algorithm that performs mathematical operations on the data to generate a fixedlength sequence of numbers or characters. The checksum is usually much smaller than the original data, making it easier to transmit or store.
When the data is received, the checksum is recalculated and compared to the original checksum. If the two checksums match, it is assumed that the data was transmitted or stored correctly. If the checksums do not match, it indicates that there was an error in the data and it may need to be retransmitted or restored from a backup.
Checksums are used in many different applications, including network protocols such as TCP and UDP, file transfer protocols such as FTP, and storage systems such as RAID. They are also used in errorcorrecting codes and cryptographic hash functions to ensure data integrity and security.
The main application of checksum is in the way TCP works to make processtoprocess delivery possible. Let’s see that first then we will see an example to understand how to calculate Checksum manually from scratch.
What is a checksum in TCP? How does checksum work in TCP?
In TCP (Transmission Control Protocol), the checksum is a mechanism used for error detection in the transmitted data. When a TCP segment is sent from a sender to a receiver, the sender calculates a checksum over the entire segment (header and data) and includes it in the TCP header.
The receiver then calculates its own checksum over the received TCP segment (header and data) and compares it to the checksum in the TCP header. If the checksums match, it indicates that the data was transmitted correctly without any errors. If the checksums do not match, it indicates that the data was corrupted during transmission and the receiver will discard the segment and request a retransmission of the data.
The TCP checksum algorithm is a 16bit Internet checksum. It uses a similar algorithm to the one used for the IP checksum, which involves adding up 16bit values in the TCP header and data, adding the carry bits, and then taking the one’s complement of the sum. The checksum is stored in the TCP header as a 16bit field.
The use of checksums in TCP is important for ensuring the reliable transmission of data across the network. Without checksums, it would be difficult to detect errors in the transmitted data, which could lead to data corruption or loss. By including a checksum in the TCP header, TCP can verify the integrity of the transmitted data and ensure that it is delivered to the receiver correctly.
You might like reading this: [February 2023] Top 7 Best Coding Apps For Coding In Android
How to calculate checksum in Computer Networks at the Sender with Examples? (Checksum Calculation Example)
Let’s now see how checksum is calculated in computer networks which might be very useful for your upcoming assignment or exam.
Let’s use a simple example where we want to calculate the checksum for the following sequence of bytes:
01010100 01100101 01110011 01110100
We’ll use the 8bit Internet checksum algorithm, which is a commonly used checksum algorithm. Here are the steps:
Step 1: Break the data into fixedsize blocks: We’ll break the data into 16bit blocks (two bytes). We can do this because the Internet checksum algorithm works on 16bit words.
01010100 01100101
01110011 01110100
Step 2: Add up the values in each block: We’ll add up the values in each block using binary addition. Here’s how it works for the first block:
01010100 (84 in decimal)
01100101 (101 in decimal)
10111001 (185 in decimal)
The sum of the first block is 185 in decimal.
Now let’s do the same thing for the second block:
01110011 (115 in decimal)
01110100 (116 in decimal)
11100111 (231 in decimal)
The sum of the second block is 231 in decimal.
Step 3: Add the two 16bit sums together: We add the two 16bit sums together using binary addition:
10111001 (185 in decimal)
11100111 (231 in decimal)
110000000 (416 in decimal)
The sum of the two blocks is 416 in decimal. However, since we’re using 8bit checksum, we need to discard the overflow and just keep the lower 8 bits, which in this case is:
10000000 (128 in decimal)
Step 4: Take the complement of the final sum: We take the one’s complement of the 8bit sum by flipping all the bits:
10000000 (128 in decimal)
01111111 (127 in decimal)
Step 5: Append the checksum to the data: We append the complemented checksum to the original data to create the checksummed data:
01010100 01100101 01110011 01110100 01111111
So the final checksum for this sequence of bytes is:
01111111 (127 in decimal)
When this sequence of bytes is transmitted or stored, the recipient can calculate the checksum using the same algorithm and compare it to the checksum that was transmitted or stored.
If the checksums match, the data is assumed to be errorfree. If the checksums do not match, an error has occurred and the data needs to be retransmitted or corrected.
How Checksum is calculated at Receiver with Examples? (Checksum Calculation Example)
Let’s say that the sequence of bytes 01010100 01100101 01110011 01110100 01111111 is transmitted to the receiver, along with the checksum 01111111.
At the receiver side, the same algorithm used to calculate the checksum at the sender side is used to calculate the checksum for the received data. The receiver divides the received data into the same 16bit blocks, adds up the values in each block, adds the two 16bit sums together, takes the one’s complement of the final sum, and compares the resulting checksum to the checksum that was transmitted.
Here are the steps by which the checksum is calculated on the receiver side.
Step 1: Break the received data into fixedsize blocks:
01010100 01100101
01110011 01110100
01111111
Step 2: Add up the values in each block:
For the first block:
01010100 (84 in decimal)
01100101 (101 in decimal)
10111001 (185 in decimal)
For the second block:
01110011 (115 in decimal)
01110100 (116 in decimal)
11100111 (231 in decimal)
For the third block, there is only one byte, so it doesn’t need to be added to anything.
Step 3: Add the two 16bit sums together:
10111001 (185 in decimal)
11100111 (231 in decimal)
110000000 (416 in decimal)
Step 4: Take the complement of the final sum:
110000000 (416 in decimal)
00111111 (63 in decimal)
Step 5: Compare the calculated checksum to the transmitted checksum:
The calculated checksum is:
00111111 (63 in decimal)
which is not equal to the transmitted checksum:
01111111 (127 in decimal)
This indicates that an error has occurred in the transmission or storage of the data.
Based on this, the receiver can request that the data be retransmitted or take other corrective action to address the error.
You might like reading: What’s The Best Coding Language To Learn First [Free Courses Included 2023]
C program for Checksum in Computer Networks
Here’s a simple c program to implement a checksum that demonstrates how to calculate checksum for a sequence of bytes:
Checksum in Networking and Calculate checksum in C
The given c program for checksum in when run gives output as:
Checksum: 0xEFEB
Lets now see the explanation of the checksum code in computer networks step by step:
In this program, we first define an array of 8 bytes (data
) that we want to calculate a checksum for. We then initialize a variable sum
to 0 and add up the values in each 16bit block of the data. We use a for
loop that increments the index variable i
by 2 at each iteration, and use bitwise operations to combine the two bytes into a 16bit value.
After we have added up all the 16bit values, we add the carry bits to the sum. We use a while
loop that shifts the sum right by 16 bits until there are no more carry bits left, and adds the carry bits to the lower 16 bits of the sum.
Finally, we take the one’s complement of the sum to get the checksum. We use the bitwise NOT operator (~
) to flip all the bits in the sum, which gives us the one’s complement of the sum. We store the checksum in a variable of type unsigned short
, which is a 16bit unsigned integer.
When we run the program, it will output the calculated checksum in hexadecimal format. In this case, the checksum for the data
array is 0x080A.
How to calculate Checksum in TCP Manually FAQs

What is a checksum?
A checksum is a value that is calculated from a block of data, and is used to detect errors that may have occurred during data transmission or storage.

How checksum is calculated in networking?
The method for calculating a checksum depends on the specific algorithm used. Generally, a checksum algorithm involves processing the data through a mathematical function that generates a fixedlength sequence of numbers or characters. The result of this calculation is the checksum value.

What types of data use checksums?
Checksums are used in a wide range of applications, including network protocols such as TCP and UDP, file transfer protocols such as FTP, and storage systems such as RAID. Checksums are also used in errorcorrecting codes and cryptographic hash functions to ensure data integrity and security.

Are all checksum algorithms the same?
No, there are many different checksum algorithms, each with its own strengths and weaknesses. Some common checksum algorithms include the cyclic redundancy check (CRC), the Adler32 checksum, and the MD5 hash function.

Are checksums the same as hash values?
While both checksums and hash values are used to verify the integrity of data, they are not the same. Checksums are generally simpler and faster to compute, but they are less secure than hash values. Hash values are more secure and less susceptible to intentional modifications, but they are also more complex and slower to compute.
So we hope considering all the information above it is now very clear to you what is checksum, its use cases, and how to calculate checksum from scratch. Please let us know if you found any errors in the checksum program in c or in the checksum example.