Cyclic Redundancy Cycle (In C)
CODE:
#include<stdio.h>
#include<string.h>
//length of the generator polynom
#define N strlen(gen_poly)
char data[28];
char check_value[28];
char gen_poly[10];
int l,i,j;
void XOR(){
for(j=1;j<N;j++){
check_value[j]=((check_value[j]== gen_poly[j])?'0':'1');
void crc();
//Function to check the errors on the receiver side
void receiver(){
printf("Enter the received data: ");
scanf("%s", data);
printf("\n");
printf("Data Received: %s",data);
crc();
//check the remainder is zero to find the error
for(i=0;(i<N-1) &&(check_value[i]!='1');i++);
if(i<N-1)
printf("\nError detected");
else
printf("\nNo error detected");
void crc(){
for(i=0;i<N;i++)
check_value[i]=data[i];
do{
if(check_value[0]=='1')
XOR();
for(j=0;j<N-1;j++)
check_value[j]=check_value[j+1];
check_value[j]=data[i++];
}while(i<=l+N-1);
int main(){
printf("Enter the data to be transmitted\n");
scanf("%s",data);
printf("Enter the polynomial\n");
scanf("%s",gen_poly);
l=strlen(data);
for(i=l;i<l+N-1;i++)
data[i]='0';
printf("Padded data: %s",data);
printf("\n");
crc();
printf("CRC or check value is : %s",check_value);
for(i=l;i<l+N-1;i++)
data[i]=check_value[i-l];
printf("\n");
printf("Final data to be sent: %s",data);
printf("\n");
receiver();
return 0;
}
Output: Enter the data to be transmitted
1010101010
Enter the polynomial
11001
Padded data: 10101010100000
CRC or check value is : 0010
Final data to be sent: 10101010100010
Enter the received data: 101010100000
Data Received: 101010100000
Error detected
Hamming Code (1): In C++
CODE:
#include<bits/stdc++.h>
using namespace std;
int main(){
int i,n,a[20],b[20];
printf("Enter the length: ");
scanf("%d",&n);
printf("Enter the first array: ");
for(i=0;i<n;i++){
scanf("%d",&a[i]);
printf("Enter the second array: ");
for(i=0;i<n;i++){
scanf("%d",&b[i]);
}
int count=0;
for(i=0;i<n;i++){
if(a[i]!=b[i]){
count++;
printf("Hamming Distance: %d", count);
return 0;
HAMMING CODE (2) – (This is in c)
CODE:
#include <stdio.h>
#include <string.h>
int main() {
char mainString[100], strings[4][100];
int i, minDistance = -1, minIndex = -1;
printf("Enter main string: ");
scanf("%s", mainString);
for (i = 0; i < 4; i++) {
printf("Enter string %d: ", i + 1);
scanf("%s", strings[i]);
int hammingDistance(char str1[], char str2[]) {
int i, count = 0;
for (i = 0; str1[i] && str2[i]; i++)
if (str1[i] != str2[i])
count++;
return count;
for (i = 0; i < 4; i++) {
int distance = hammingDistance(mainString, strings[i]);
if (minDistance == -1 || distance < minDistance) {
minDistance = distance;
minIndex = i;
printf("String %d has the minimum Hamming distance of %d.\n", minIndex + 1, minDistance);
return 0;
PARITY CHECK (In C++)
CODE:
#include <iostream>
#include <vector>
using namespace std;
// Function to compute the row and column parity for a given 2D matrix
void computeParity(const vector<vector<int>>& matrix) {
int rows = matrix.size();
int cols = matrix[0].size();
// Compute row parity
cout << "Row Parity:" << endl;
for (int i = 0; i < rows; ++i) {
int row_parity = 0;
for (int j = 0; j < cols; ++j) {
row_parity ^= matrix[i][j];
cout << "Row " << i << ": " << row_parity << endl;
}
// Compute column parity
cout << "Column Parity:" << endl;
for (int j = 0; j < cols; ++j) {
int col_parity = 0;
for (int i = 0; i < rows; ++i) {
col_parity ^= matrix[i][j];
cout << "Column " << j << ": " << col_parity << endl;
int main() {
vector<vector<int>> matrix = {
{1, 1, 0, 1, 1, 0, 0, 1},
{1, 0, 0, 1, 1, 1, 1, 0},
{1, 0, 1, 1, 0, 0, 1, 1}
};
computeParity(matrix);
return 0;
CLASSFULL ADDRESSING (With decimal format NOT converting to binary):
CODE:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char identifyClass(char ip[]) {
int first_octet = atoi(strtok(ip, "."));
if (first_octet >= 1 && first_octet <= 126)
return 'A';
else if (first_octet >= 128 && first_octet <= 191)
return 'B';
else if (first_octet >= 192 && first_octet <= 223)
return 'C';
else if (first_octet >= 224 && first_octet <= 239)
return 'D';
else
return 'E';
int main() {
char ip[16];
printf("Enter the IP address (in dotted decimal form): ");
scanf("%s", ip);
printf("Input: %s\n", ip);
printf("Output: Class: %c\n", identifyClass(ip));
return 0;
FIRST AND LAST IP ADDRESSES (IN C++):
CODE:
#include <bits/stdc++.h>
using namespace std;
vector <int> bin (int n){
vector <int> s1;
while (n!=1){
int rem=n%2;
n/=2;
s1. push_back(rem);
if (n==1) s1.push_back(n);
reverse (s1.begin(), s1.end());
return s1;
int main(){
string ip = "205.16.37.39/28";
printf("The input is 205.1.37.39/28\n");
string submask_str = ip.substr(13,2);
int submask = stoi(submask_str);
int bits = 32 - submask;
printf("The subnet is: %d\n", bits);
//vector<int> ans = bin(bits);
//for (auto it: ans) cout << it <<" ";
vector<int> ans1 = bin (205);
vector<int> ans2 = bin (16);
vector<int> ans3 = bin (37);
vector<int> ans4 = bin (39);
cout << "\nFirst IP Address: ";
for (auto it: ans1) cout <<it<<" ";
cout <<".";
for (auto it: ans2) cout <<it<<" ";
cout <<".";
for (auto it: ans3) cout <<it<<" ";
cout <<".";
int k1=0;
int s1 = ans4.size()-1;
while (k1!= bits){
ans4[s1--]=0;
k1++;
for (auto it: ans4) cout <<it<<" ";
cout <<endl;
cout << "Last IP Address: ";
for (auto it: ans1) cout << it << " ";
cout << ".";
for (auto it: ans2) cout << it << " ";
cout << ".";
for (auto it: ans3) cout << it << " ";
cout << ".";
int k = 0;
int s = ans4.size() - 1;
while (k != bits) {
ans4[s--] = 1;
k++;
for (auto it: ans4) cout << it << " ";
return 0;
ALL IP ADDRESSES:
CODE:
#include <bits/stdc++.h>
using namespace std;
vector<int> bin(int n){
vector<int> s1;
while (n != 1){
int rem = n % 2;
n /= 2;
s1.push_back(rem);
if (n == 1) s1.push_back(n);
reverse(s1.begin(), s1.end());
return s1;
int main(){
string ip = "205.16.37.39/28";
printf("The input is 205.16.37.39/28\n");
string submask_str = ip.substr(13,2);
int submask = stoi(submask_str);
int bits = 32 - submask;
printf("The subnet is: %d\n", bits);
vector<int> ans1 = bin(205);
vector<int> ans2 = bin(16);
vector<int> ans3 = bin(37);
vector<int> ans4 = bin(39);
cout << "\nFirst IP Address: ";
for (auto it: ans1) cout << it <<" ";
cout <<".";
for (auto it: ans2) cout << it <<" ";
cout <<".";
for (auto it: ans3) cout << it <<" ";
cout <<".";
int k1=0;
int s1 = ans4.size()-1;
while (k1 != bits){
ans4[s1--]=0;
k1++;
for (auto it: ans4) cout << it <<" ";
cout <<endl;
cout << "Last IP Address: ";
for (auto it: ans1) cout << it <<" ";
cout << ".";
for (auto it: ans2) cout << it <<" ";
cout << ".";
for (auto it: ans3) cout << it <<" ";
cout << ".";
int k = 0;
int s = ans4.size() - 1;
while (k != bits) {
ans4[s--] = 1;
k++;
for (auto it: ans4) cout << it <<" ";
for (int i = 128; i <= 143; i++) {
vector<int> binary = bin(i);
cout <<"11001101.10000.100101.";
for (int j = 0; j < 8; j++) {
cout << binary[j];
cout << endl;
return 0;
ISSUES (they are not printed as 8-bit octets)
IP CLASS WITH CONVERSION OF DECIMAL TO BINARY AND NETWORK ID
(IN C++):
CODE:
#include <stdio.h>
#include <string.h>
void decimalToBinary(int decimal, int binary[8]) {
for (int i = 7; i >= 0; i--) {
binary[i] = decimal % 2;
decimal /= 2;
void separate(char str[], char ipClass) {
// Initializing network and host array to NULL
char network[12], host[12];
for (int k = 0; k < 12; k++)
network[k] = host[k] = '\0';
if (ipClass == 'A') {
int i = 0, j = 0;
while (str[j] != '.')
network[i++] = str[j++];
i = 0;
j++;
while (str[j] != '\0')
host[i++] = str[j++];
printf("Network ID is %s.0.0.0\n", network);
//printf("Host ID is %s\n", host);
else if (ipClass == 'B') {
int i = 0, j = 0, dotCount = 0;
while (dotCount < 2) {
network[i++] = str[j++];
if (str[j] == '.')
dotCount++;
}
i = 0;
j++;
while (str[j] != '\0')
host[i++] = str[j++];
printf("Network ID is %s.0.0\n", network);
//printf("Host ID is %s\n", host);
else if (ipClass == 'C') {
int i = 0, j = 0, dotCount = 0;
while (dotCount < 3) {
network[i++] = str[j++];
if (str[j] == '.')
dotCount++;
i = 0;
j++;
while (str[j] != '\0')
host[i++] = str[j++];
printf("Network ID is %s.0\n", network);
//printf("Host ID is %s\n", host);
// Class D and E are not divided in Network
// and Host ID
else
printf("In this Class, IP address is not"
" divided into Network and Host ID\n");
int main() {
char ip_address[16];
printf("Enter the IP address in dotted decimal format (e.g., 192.168.1.1): ");
scanf("%s", ip_address);
int octet = 0;
int binary[32];
int index = 0;
for (int i = 0; i < strlen(ip_address); i++) {
if (ip_address[i] == '.') {
int tempBinary[8];
decimalToBinary(octet, tempBinary);
memcpy(binary + index, tempBinary, 8 * sizeof(int));
index += 8;
octet = 0;
} else {
octet = octet * 10 + (ip_address[i] - '0');
// Convert the last octet to binary
int tempBinary[8];
decimalToBinary(octet, tempBinary);
memcpy(binary + index, tempBinary, 8 * sizeof(int));
char ip_class;
if (binary[0] == 0) {
ip_class = 'A';
} else if (binary[0] == 1 && binary[1] == 0) {
ip_class = 'B';
} else if (binary[0] == 1 && binary[1] == 1 && binary[2] == 0) {
ip_class = 'C';
} else if (binary[0] == 1 && binary[1] == 1 && binary[2] == 1 && binary[3] == 0) {
ip_class = 'D';
} else {
ip_class = 'E';
printf("IP Address Class: %c\n", ip_class);
separate(ip_address, ip_class);
return 0;
IP CLASS WITH CONVERSION (IN C++):
CODE:
#include <stdio.h>
#include <string.h>
void decimalToBinary(int decimal, int binary[8]) {
for (int i = 7; i >= 0; i--) {
binary[i] = decimal % 2;
decimal /= 2;
}
}
int main() {
char ip_address[16];
printf("Enter the IP address in dotted decimal format (e.g., 192.168.1.1): ");
scanf("%s", ip_address);
int octet = 0;
int binary[32];
int index = 0;
for (int i = 0; i < strlen(ip_address); i++) {
if (ip_address[i] == '.') {
int tempBinary[8];
decimalToBinary(octet, tempBinary);
memcpy(binary + index, tempBinary, 8 * sizeof(int));
index += 8;
octet = 0;
} else {
octet = octet * 10 + (ip_address[i] - '0');
// Convert the last octet to binary
int tempBinary[8];
decimalToBinary(octet, tempBinary);
memcpy(binary + index, tempBinary, 8 * sizeof(int));
char ip_class;
if (binary[0] == 0) {
ip_class = 'A';
} else if (binary[0] == 1 && binary[1] == 0) {
ip_class = 'B';
} else if (binary[0] == 1 && binary[1] == 1 && binary[2] == 0) {
ip_class = 'C';
} else if (binary[0] == 1 && binary[1] == 1 && binary[2] == 1 && binary[3] == 0) {
ip_class = 'D';
} else {
ip_class = 'E';
printf("IP Address Class: %c\n", ip_class);
return 0;
Conversion of decimal to binary:
#include <bits/stdc++.h>
using namespace std;
string decimalToBinary(int n) {
string binary = "";
if (n == 0) return "0";
while (n > 0) {
binary = to_string(n % 2) + binary;
n = n / 2;
return binary;
int main() {
cout << "Binary representation is:" << endl;
int numbers[] = {205, 16, 37, 39};
for (int number : numbers) {
string binaryNumber = decimalToBinary(number);
cout <<binaryNumber<<".";
return 0;
CHECKSUM (1): Not taking user input
CODE:
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
string addBinary(const string &a, const string &b) {
string result = "";
int carry = 0;
int i = a.size() - 1, j = b.size() - 1;
while (i >= 0 || j >= 0 || carry == 1) {
carry += (i >= 0) ? a[i--] - '0' : 0;
carry += (j >= 0) ? b[j--] - '0' : 0;
result = char(carry % 2 + '0') + result;
carry /= 2;
return result;
string stripLeadingZeros(const string &s) {
size_t pos = s.find_first_not_of('0');
if (pos != string::npos) {
return s.substr(pos);
return "0";
string handleComplement(string binarySum) {
string complement = "10";
return addBinary(binarySum, complement);
void checkAllOnes(const string &binarySum) {
if (binarySum.find('0') == string::npos) {
cout << "No Error" << endl;
} else {
cout << "Error" << endl;
int main() {
vector<string> binaries = {"101", "111", "011", "110"};
string sum = "0";
for (const auto &binary : binaries) {
sum = addBinary(sum, binary);
cout << "Sum before adding complement: " << sum << endl;
if (sum.size() > 4) {
sum = sum.substr(1);
sum = handleComplement(sum);
sum = stripLeadingZeros(sum);
cout << "Final Sum after adding complement and removing leading zeros: " << sum << endl;
checkAllOnes(sum);
return 0;
WITH USER INPUT AND FRAME SIZE (If both sender and receiver given):
#include <bits/stdc++.h>
using namespace std;
string Ones_complement(string data)
for (int i = 0; i < data.length(); i++) {
if (data[i] == '0')
data[i] = '1';
else
data[i] = '0';
return data;
string checkSum(string data, int block_size)
int n = data.length();
if (n % block_size != 0) {
int pad_size = block_size - (n % block_size);
for (int i = 0; i < pad_size; i++) {
data = '0' + data;
string result = "";
for (int i = 0; i < block_size; i++) {
result += data[i];
for (int i = block_size; i < n; i += block_size) {
string next_block = "";
for (int j = i; j < i + block_size; j++) {
next_block += data[j];
string additions = "";
int sum = 0, carry = 0;
for (int k = block_size - 1; k >= 0; k--) {
sum += (next_block[k] - '0')
+ (result[k] - '0');
carry = sum / 2;
if (sum == 0) {
additions = '0' + additions;
sum = carry;
else if (sum == 1) {
additions = '1' + additions;
sum = carry;
else if (sum == 2) {
additions = '0' + additions;
sum = carry;
else {
additions = '1' + additions;
sum = carry;
string final = "";
if (carry == 1) {
for (int l = additions.length() - 1; l >= 0;
l--) {
if (carry == 0) {
final = additions[l] + final;
else if (((additions[l] - '0') + carry) % 2
== 0) {
final = "0" + final;
carry = 1;
else {
final = "1" + final;
carry = 0;
result = final;
else {
result = additions;
return Ones_complement(result);
}
bool checker(string sent_message,
string rec_message,
int block_size)
string sender_checksum
= checkSum(sent_message, block_size);
string receiver_checksum = checkSum(
rec_message + sender_checksum, block_size);
if (count(receiver_checksum.begin(),
receiver_checksum.end(), '0')
== block_size) {
return true;
else {
return false;
int main()
string sent_message
= "101111011110";
string recv_message
= "101111011110";
int block_size = 3;
if (checker(sent_message,
recv_message,
block_size)) {
cout << "No Error";
}
else {
cout << "Error";
return 0;
PRINT CLASS:
#include <bits/stdc++.h>
using namespace std;
string decimalToBinary(int n) {
string binary = "";
if (n == 0) return "0";
while (n > 0) {
binary = to_string(n % 2) + binary;
n = n / 2;
return binary;
char determinePrefix(string binaryNumber) {
if (binaryNumber.compare(0, 1, "0") == 0) return 'A';
else if (binaryNumber.compare(0, 2, "10") == 0) return 'B';
else if (binaryNumber.compare(0, 3, "110") == 0) return 'C';
else if (binaryNumber.compare(0, 4, "1110") == 0) return 'D';
else return 'E';
int main() {
cout << "Binary representation is:" << endl;
int numbers[] = {205, 16, 37, 39};
bool isFirstNumber = true;
char firstNumberPrefix;
for (int number : numbers) {
string binaryNumber = decimalToBinary(number);
cout << binaryNumber << ".";
if (isFirstNumber) {
firstNumberPrefix = determinePrefix(binaryNumber);
isFirstNumber = false;
cout << "\n"<<firstNumberPrefix << endl;
return 0;