Skip to content

Instantly share code, notes, and snippets.

@hpetru
Created March 12, 2015 22:13
Show Gist options
  • Select an option

  • Save hpetru/e07c72c3d79f87a324ca to your computer and use it in GitHub Desktop.

Select an option

Save hpetru/e07c72c3d79f87a324ca to your computer and use it in GitHub Desktop.
#include <iostream>
#include <string>
#include <bitset>
#include <fstream>
class bitn : public std::bitset<16>
{
public:
bitn() : std::bitset<16>() {}
bitn(int x) : std::bitset<16>(x) {}
bitn(long unsigned int x) : std::bitset<16>(x) {}
bitn(std::string s) : std::bitset<16>(s) {}
friend bitn operator + (bitn lth, bitn rth);
friend bitn operator - (bitn lth, bitn rth);
friend bitn operator / (bitn lth, bitn rth);
friend bitn operator * (bitn lth, bitn rth);
friend bool operator > (bitn lth, bitn rth);
friend bool operator < (bitn lth, bitn rth);
};
bitn operator + (bitn lth, bitn rth)
{
bitn result((int)(lth.to_ulong() + rth.to_ulong()));
return result;
}
bitn operator - (bitn lth, bitn rth)
{
bitn result((int)(lth.to_ulong() - rth.to_ulong()));
return result;
}
bitn operator / (bitn lth, bitn rth)
{
bitn result((int)(lth.to_ulong() / rth.to_ulong()));
return result;
}
bitn operator * (bitn lth, bitn rth)
{
bitn result((int)(lth.to_ulong() * rth.to_ulong()));
return result;
}
bool operator > (bitn lth, bitn rth)
{
return (lth.to_ulong() > rth.to_ulong());
}
bool operator < (bitn lth, bitn rth)
{
return (lth.to_ulong() < rth.to_ulong());
}
int main()
{
bitn bs[10];
int n = 0;
std::ifstream in("./binary.in");
in >> n;
for(int i = 0; i < n; i++)
{
std::string buff;
in >> buff;
bs[i] = bitn(buff);
}
bitn max;
max = bs[0];
for(int i = 0; i < n; i++)
{
if(bs[i] > max) max = bs[i];
}
std::cout << "Numarul maxim este: \t" << max << std::endl;
std::cout << "Valoare lui zecimala: \t" << max.to_ulong() << std::endl;
unsigned int sum;
for(int i = 0; i < n; i++)
{
sum += bs[i].to_ulong();
}
std::cout << "Suma este: \t";
std::cout << sum << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment