Question: #Must be in C + + ( Data Structures / Algorithms ) and have no errors, I've got it partially done in Part 1 but

#Must be in C++(Data Structures/Algorithms) and have no errors, I've got it partially done in Part 1 but I'm stumped, here's my code:
// beginning of main
#include
#include "BitsByte.h"
using namespace std;
int main(){
Byte bite;
// Test setValue function
bite.setValue(99);
// Test at function
for (int i =0; i <8; i++){
cout << bite.at(i)<< endl;
}
// Test toInt and toString functions
cout << "Int: "<< bite.toInt()<< endl;
cout << "String: "<< bite.toString()<< endl;
return 0;
}
// main ends
// BitsByte.cpp begins
#include "BitsByte.h"
// Convert bits to integer
int Byte::bitsToInt(){
int value =0;
for (int i =0; i <8; ++i){
value |=(bits[i]<<(7- i));
}
return value;
}
// Sets the value of bits
void Byte::setValue(int value){
for (int i =7; i >=0; --i){
bits[i]=(value & 1);
value >>=1;
}
}
// Function to get the bit at specific indexs
int Byte::at(int index){
return bits[index];
}
// Converts bits to strings
std::string Byte::toString(){
std::string str;
for (int i =0; i <8; ++i){// Loop from index 0 to 7
str += std::to_string(bits[i]);
}
return str;
}
// Converts bits to integer
int Byte::toInt(){
return bitsToInt();
}
// BitsByte.cpp ends
// BitsByte.h begins
#ifndef BYTE_H
#define BYTE_H
#include
class Byte {
private:
int bits[8];
int bitsToInt();
public:
void setValue(int value);
int at(int index);
std::string toString();
int toInt();
// Constructors
};
#endif
// BitsByte.h ends

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!