Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

#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

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

Essentials of Database Management

Authors: Jeffrey A. Hoffer, Heikki Topi, Ramesh Venkataraman

1st edition

133405680, 9780133547702 , 978-0133405682

More Books

Students also viewed these Databases questions