Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Can you please correct and finish the following code? It isnt outputting the corrected hamming decoded message. The instructions are in the images attached. All

Can you please correct and finish the following code? It isnt outputting the corrected hamming decoded message. The instructions are in the images attached. All without veering to far from the base code I have and without including any other libraries. Thank you so much in advance.
#include
#include
using namespace std;
// Function to get the value of a specific bit in a short integer
int getBit(short num, int pos){
return (num >> pos) & 1;
}
// Function to set a specific bit in a char variable
char setBit(char ch, int pos, int val){
if (val)
return ch |(1 pos);
else
return ch & ~(1 pos);
}
int main(){
short HammingChar;
uint bit1, bit2, bit4, bit8, offendingBit;
char ch;
// Read an unknown number of short ints, each representing a Hamming character
while (cin >> HammingChar){
offendingBit =0;
// Use the formula to calculate parity bit 1
bit1=(getBit(HammingChar,11-3)+ getBit(HammingChar,11-5)+
getBit(HammingChar,11-7)+ getBit(HammingChar,11-9)+
getBit(HammingChar,11-11))%2;
// Use the formulas to calculate parity bits 2,4, and 8
bit2=(getBit(HammingChar,11-3)+ getBit(HammingChar,11-6)+
getBit(HammingChar,11-7)+ getBit(HammingChar,11-10)+
getBit(HammingChar,11-11))%2;
bit4=(getBit(HammingChar,11-5)+ getBit(HammingChar,11-6)+
getBit(HammingChar,11-7))%2;
bit8=(getBit(HammingChar,11-9)+ getBit(HammingChar,11-10)+
getBit(HammingChar,11-11))%2;
// Check if any calculated parity bit disagrees with the value from HammingChar
if (bit1!= getBit(HammingChar,11-1)) offendingBit +=1;
if (bit2!= getBit(HammingChar,11-2)) offendingBit +=2;
if (bit4!= getBit(HammingChar,11-4)) offendingBit +=4;
if (bit8!= getBit(HammingChar,11-8)) offendingBit +=8;
cout "Debug: offendingBit =" offendingBit endl;
// If offendingBit is not 0, correct the bit and build the character
if (offendingBit !=0){
int correctedBitPos =11- offendingBit;
HammingChar = HammingChar ^(1(correctedBitPos -1));
}
// Build ch from HammingChar
ch = HammingChar >>4; // Extract the ASCII character from the Hamming code
cout ch;
}
return 0;
}
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database And Expert Systems Applications 31st International Conference Dexa 2020 Bratislava Slovakia September 14 17 2020 Proceedings Part 1 Lncs 12391

Authors: Sven Hartmann ,Josef Kung ,Gabriele Kotsis ,A Min Tjoa ,Ismail Khalil

1st Edition

303059002X, 978-3030590024

More Books

Students also viewed these Databases questions

Question

Guidelines for Informative Speeches?

Answered: 1 week ago