Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

My code doesnt work and when i compile i get lab 3 1 . cpp: 4 0 : 1 2 : warning: comparison between signed

My code doesnt work and when i compile i get lab31.cpp:40:12: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if (bit1!= getBit(HammingChar,11-1)) offendingBit +=1;
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
lab31.cpp:41:12: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if (bit2!= getBit(HammingChar,11-2)) offendingBit +=2;
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
lab31.cpp:42:12: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if (bit4!= getBit(HammingChar,11-4)) offendingBit +=4;
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
lab31.cpp:43:12: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if (bit8!= getBit(HammingChar,11-8)) offendingBit +=8;
this is the code :
#include
#include
using namespace std;
const string ID = "Stellar Carroll - CS 1337- Lab 31";
// 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;
cout << ID << endl;
// 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;
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;
// Debug output for the offending bit position
//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 the character from the corrected HammingChar
ch =0;
ch = setBit(ch,0, getBit(HammingChar,2));
ch = setBit(ch,1, getBit(HammingChar,4));
ch = setBit(ch,2, getBit(HammingChar,5));
ch = setBit(ch,3, getBit(HammingChar,6));
ch = setBit(ch,4, getBit(HammingChar,8));
ch = setBit(ch,5, getBit(HammingChar,9));
ch = setBit(ch,6, getBit(HammingChar,10));
// Output the corrected ASCII character
cout << ch;
}
cout << endl;
return 0;
}

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

1 2 3 Data Base Techniques

Authors: Dick Andersen

1st Edition

0880223464, 978-0880223461

More Books

Students also viewed these Databases questions