Offcourse not, that would be silly. Just overload the ^ operator like any normal person.
#include <iostream>
#include <bitset>
class Bits {
unsigned value;
public:
explicit Bits(unsigned v) : value(v) {}
// Shift operator ^ : positive = left, negative = right
Bits operator^(int shift) const {
if (shift > 0) {
return Bits(value << shift);
} else if (shift < 0) {
return Bits(value >> -shift);
} else {
return *this; // no shift
}
}
friend std::ostream& operator<<(std::ostream& os, const Bits& b) {
return os << std::bitset<8>(b.value); // print 8 bits
}
};int main() {
Bits x(0b00001111);
std::cout << "x = " << x << "\n";
std::cout << "x ^ 2 = " << (x ^ 2) << " (shift left 2)\n";
std::cout << "x ^ -2 = " << (x ^ -2) << " (shift right 2)\n";
}