PRACTICAL SET 1>>
AIM-->. TO STUDY DIFFERENT TYPES OF TOPOLOGIES AND IMPLEMENT IN CISCO PACKET TRACER:
ANS--->
1>> BUS TOPOLOGY
2>> STAR TOPOLOGY
3>> RING TOPOLOGY
4>> MESH TOPOLOGY
5>> TREE TOPOLOGY
6>> HYBRID TOPOLOGY
PRACTICAL SET 2 >>. NO NEED TO WRITE
PRACTICAL SET 3 >>. NO NEED TO WRITE
PRACTICAL SET 4 >>. NO NEED TO WRITE
PRACTICAL SET 5>>
AIM--> WRITE A PROGRAM TO IMPLEMENT HAMMING CODE
C- PRACTICAL CODE :
#include <stdio.h>
// Encoding Function for (7,4) Hamming Code
void hamming_encode(int data, int *codeword) {
// Ensure the input data is a 4-bit value (0-15)
if (data < 0 || data > 15) {
printf("Input data must be a 4-bit value between 0 and 15.\n");
return;
}
// Initialize the parity bits
int p1 = (data & 1) ^ ((data >> 1) & 1) ^ ((data >> 3) & 1);
int p2 = (data & 1) ^ ((data >> 2) & 1) ^ ((data >> 3) & 1);
int p4 = ((data >> 1) & 1) ^ ((data >> 2) & 1) ^ ((data >> 3) & 1);
// Create the 7-bit Hamming codeword
*codeword = (p1 << 6) | (p2 << 5) | (data << 1) | p4;
}
// Decoding Function for (7,4) Hamming Code
int hamming_decode(int codeword) {
// Calculate syndrome bits
int s1 = ((codeword >> 6) & 1) ^ ((codeword >> 4) & 1) ^ ((codeword >> 2) & 1) ^ (codeword & 1);
int s2 = ((codeword >> 5) & 1) ^ ((codeword >> 4) & 1) ^ ((codeword >> 1) & 1) ^ (codeword & 1);
int s4 = ((codeword >> 3) & 1) ^ ((codeword >> 2) & 1) ^ ((codeword >> 1) & 1) ^ (codeword & 1);
// Calculate the error position
int error_position = s1 + 2 * s2 + 4 * s4;
// Correct the error if one is detected
if (error_position != 0) {
printf("Error detected at position %d. Correcting...\n", error_position);
codeword ^= (1 << (7 - error_position)); // Flip the bit at the error position
}
// Remove the parity bits to obtain the original data
int decoded_data = ((codeword >> 1) & 1) | ((codeword >> 2) & 2) | ((codeword >> 3) & 4) | ((codeword >> 5) & 8);
return decoded_data;
}
int main() {
int data = 5; // Four-bit data word (e.g., 0101 in binary)
int codeword;
printf("Original Data: %d (Binary: %04b)\n", data, data);
// Encoding
hamming_encode(data, &codeword);
printf("Encoded Codeword: %d (Binary: %07b)\n", codeword, codeword);
// Simulate errors in the received codeword (for testing)
int received_codeword = codeword ^ (1 << 2); // Flip a bit for demonstration
// Decoding
int decoded_data = hamming_decode(received_codeword);
printf("Decoded Data: %d (Binary: %04b)\n", decoded_data, decoded_data);
return 0;
}
OUTPUT :
Original Data: 5 (Binary: 0101)
Encoded Codeword: 75 (Binary: 1001011)
Error detected at position 1. Correcting...
Decoded Data: 3 (Binary: 0011)
PRACTICAL SET 6 >>
AIM--> WRITE A PROGRAM TO IMPLEMENT BIT STUFFING
PRACTICAL CODE :
#include <stdio.h>
// Function to perform bit stuffing
void bit_stuffing(char* input, char* stuffed_output) {
char flag_sequence[] = "01111110"; // Flag or delimiter sequence
int flag_len = 8; // Length of the flag sequence
int i, j = 0;
int consecutive_ones = 0;
for (i = 0; input[i] != '\0'; i++) {
stuffed_output[j++] = input[i];
if (input[i] == '1') {
consecutive_ones++;
} else {
consecutive_ones = 0;
}
if (consecutive_ones == 5) {
// If five consecutive ones are encountered, insert a '0'
stuffed_output[j++] = '0';
consecutive_ones = 0; // Reset counter
}
}
// Append the flag sequence to the stuffed output
for (i = 0; i < flag_len; i++) {
stuffed_output[j++] = flag_sequence[i];
}
stuffed_output[j] = '\0'; // Null-terminate the stuffed output
}
int main() {
char input[] = "011111010110110";
char stuffed_output[100];
printf("Original Data: %s\n", input);
bit_stuffing(input, stuffed_output);
printf("Stuffed Data: %s\n", stuffed_output);
return 0;
}
OUTPUT:
Original Data: 011111010110110
Stuffed Data: 011111001011011001111110
PRACTICAL SET 7 >>
AIM--> WRITE A PROGRAM TO IMPLEMENT CRC
PRACTICAL CODE:
#include <stdio.h>
#include <string.h>
// CRC-32 Polynomial (used in Ethernet and many other protocols)
#define CRC32_POLYNOMIAL 0xEDB88320L
// Function to generate a CRC checksum
unsigned int generate_crc32(const char* message) {
unsigned int crc = 0xFFFFFFFF; // Initial CRC value
int i, j;
for (i = 0; message[i] != '\0'; i++) {
crc ^= (unsigned char)message[i];
for (j = 0; j < 8; j++) {
if (crc & 1) {
crc = (crc >> 1) ^ CRC32_POLYNOMIAL;
} else {
crc >>= 1;
}
}
}
return ~crc; // Invert the bits
}
// Function to verify the CRC checksum
int verify_crc32(const char* message, unsigned int checksum) {
return generate_crc32(message) == checksum;
}
int main() {
const char* message = "Hello, CRC!";
unsigned int crc_checksum;
// Generate CRC checksum
crc_checksum = generate_crc32(message);
printf("CRC-32 Checksum: 0x%X\n", crc_checksum);
// Verify the CRC checksum
int is_valid = verify_crc32(message, crc_checksum);
if (is_valid) {
printf("CRC-32 Checksum is valid.\n");
} else {
printf("CRC-32 Checksum is NOT valid.\n");
}
return 0;
}
CRC-32 Checksum: 0xB02496E6
CRC-32 Checksum is valid
PRACTICAL SET 8>>. NO NEED TO WRITE
PRACTICAL SET 9 >>. NO NEED TO WRITE
PRACTICAL SET 10 >>. NO NEED TO WRITE
Comments
Post a Comment