Functional NegBin implementaion
This commit is contained in:
commit
82c2bb9f1b
9 changed files with 332 additions and 0 deletions
57
NegBin/NegBin.h
Normal file
57
NegBin/NegBin.h
Normal file
|
@ -0,0 +1,57 @@
|
|||
//
|
||||
// Created by trotfunky on 04/11/2020.
|
||||
//
|
||||
|
||||
#ifndef MISC_NEGBIN_H
|
||||
#define MISC_NEGBIN_H
|
||||
|
||||
#include <cmath>
|
||||
#include <bitset>
|
||||
|
||||
/// Bitset the size of a long, for convenience
|
||||
typedef std::bitset<sizeof(long)*8> longBitField;
|
||||
|
||||
/***
|
||||
* Represents a number in base negative two.
|
||||
* For ex. : 0b1 is 1, 0b10 is -2, 0b1010 is 6.
|
||||
*/
|
||||
class NegBin {
|
||||
public:
|
||||
NegBin() = default;
|
||||
|
||||
/// Create a NegBin with a specific binary representation
|
||||
explicit NegBin(longBitField binaryRepresentation);
|
||||
/// Converts from long to NegBin. For ex 3 would set value to 0b111
|
||||
explicit NegBin(long number);
|
||||
|
||||
NegBin& operator+=(const NegBin& rhs);
|
||||
// Uses += and ~ to do a subtraction
|
||||
NegBin& operator-=(const NegBin& rhs);
|
||||
NegBin& operator*=(const int& rhs);
|
||||
/// Used to obtain the binary representation of value's opposite
|
||||
NegBin operator~() const;
|
||||
|
||||
/// Shifts the underlying binary representation
|
||||
NegBin operator>>(const int& rhs);
|
||||
NegBin operator<<(const int& rhs);
|
||||
|
||||
friend NegBin operator+(NegBin lhs, const NegBin& rhs);
|
||||
friend NegBin operator-(NegBin lhs, const NegBin& rhs);
|
||||
// Doubled, for symmetry
|
||||
friend NegBin operator*(NegBin lhs, const int& rhs);
|
||||
friend NegBin operator*(const int& lhs, NegBin rhs);
|
||||
|
||||
/// Converts from NegBin to signed long, converting to unsigned would not make sense
|
||||
explicit operator long() const;
|
||||
|
||||
private:
|
||||
/// Binary description of the number.
|
||||
unsigned long value; // TODO : Maybe use bitset everywhere ?
|
||||
};
|
||||
|
||||
/// Define literal to create NegBin numbers. Calls the NegBin::NegBin(longBitField) constructor
|
||||
NegBin operator "" _nb(unsigned long long);
|
||||
|
||||
std::ostream& operator<<(std::ostream& ostream, const NegBin& number);
|
||||
|
||||
#endif //MISC_NEGBIN_H
|
Loading…
Add table
Add a link
Reference in a new issue