Skip to content

Instantly share code, notes, and snippets.

@Steffo99
Last active December 1, 2016 11:40
Show Gist options
  • Select an option

  • Save Steffo99/63f0d5c2c971c73d3e43952f5c4d4fcb to your computer and use it in GitHub Desktop.

Select an option

Save Steffo99/63f0d5c2c971c73d3e43952f5c4d4fcb to your computer and use it in GitHub Desktop.
Homework. Again.
#ifndef BYTE_CPP
#define BYTE_CPP
#include <math.h>
#include "byte.h"
Byte::Byte()
{
for(int i = 0; i < 8; i++)
{
bit[i] = 0;
}
}
Byte::Byte(short int value)
{
for(int i = 0; i < 8; i++)
{
bit[i] = (bool) ((value & (short int) pow(2, i)) >> i);
}
}
void Byte::Set(short int value)
{
for(int i = 0; i < 8; i++)
{
bit[i] = (bool) ((value & (short int) pow(2, i)) >> i);
}
}
void Byte::SetBit(unsigned int place, bool value)
{
/*place deve essere minore di 8, non faccio il controllo dentro questa funzione*/
bit[place] = value;
}
short int Byte::GetTen()
{
short int result = 0;
for(int i = 0; i < 8; i++)
{
result += (short int) this->bit[i] * pow(2, i);
}
return result;
}
Byte Byte::operator+(Byte other)
{
Byte result;
short int carry = 0;
for(int i = 0; i < 8; i++)
{
short int partial = (short int) this->bit[i] + (short int) other.bit[i] + carry;
carry = (partial & 2) >> 1;
result.bit[i] = partial & 1;
}
return result;
}
Byte Byte::operator~()
{
Byte result;
for(int i = 0; i < 8; i++)
{
result.bit[i] = !this->bit[i];
}
return result;
}
Byte Byte::operator<<(unsigned int shift)
{
Byte result;
for(int i = 7; i >= shift; i--)
{
result.bit[i] = this->bit[i-shift];
}
for(int i = 0; i < shift; i++)
{
result.bit[i] = false;
}
return result;
}
Byte Byte::operator>>(unsigned int shift)
{
Byte result;
for(int i = 0; i < 8 - shift; i++)
{
result.bit[i] = this->bit[i+shift];
}
for(int i = 8 - shift; i < 8; i++)
{
result.bit[i] = false;
}
return result;
}
Byte Byte::operator-(Byte other)
{
Byte uno(0x01);
return (*this) + (~other) + uno;
}
std::ostream& operator<<(std::ostream &o, Byte object)
{
for(int i = 7; i >= 0; i--)
{
o << object.bit[i] << " ";
}
}
std::istream& operator>>(std::istream &i, Byte &object)
{
//I bit vanno separati con uno spazio!
for(int j = 7; j >= 0; j--)
{
i >> object.bit[j];
}
}
#endif
#ifndef BYTE_H
#define BYTE_H
#include <iostream>
class Byte
{
private:
bool bit[8];
public:
Byte();
Byte(short int value);
void Set(short int value);
void SetBit(unsigned int place, bool value);
short int GetTen();
Byte operator+(Byte other);
Byte operator~();
Byte operator<<(unsigned int shift);
Byte operator>>(unsigned int shift);
Byte operator-(Byte other);
friend std::ostream& operator<<(std::ostream &o, Byte value);
friend std::istream& operator>>(std::istream &i, Byte &object);
};
#endif
#include <iostream>
#include "byte.cpp"
using namespace std;
int main()
{
Byte a(0x04);
Byte b(0x02);
Byte u;
cout << "a:\t" << a << "\n";
cout << "b:\t" << b << "\n";
cout << "u:\t" << u << "\n";
cout << "a+b:\t" << (a+b) << "\n";
cout << "a-b:\t" << (a-b) << "\n";
cout << "a<<1:\t" << (a << 1) << "\n";
cout << "a>>1:\t" << (a >> 1) << "\n";
cout << "~a:\t" << (~a) << "\n";
Byte c;
cin >> c;
cout << "c:\t" << c << "\n";
cout << "cten:\t" << c.GetTen() << "\n";
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment