Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstring>
- #include <vector>
- #include <cassert>
- #define MAX(x, y) ((x) > (y) ? (x) : (y))
- struct BigUInt192
- {
- using baseType = uint16_t;
- using oversizedType = uint32_t;
- BigUInt192()
- {
- parts = new baseType[arraySize];
- this->erase();
- }
- void erase()
- {
- std::memset(parts, 0, sizeof(baseType) * arraySize);
- }
- ~BigUInt192()
- {
- delete[] parts;
- }
- static const size_t bytes = 24;
- static const size_t arraySize = bytes / sizeof(baseType) + (bytes % sizeof(baseType) > 0);
- baseType *parts;
- };
- BigUInt192 operator+(const BigUInt192 &left, const BigUInt192 &right)
- {
- BigUInt192 result;
- BigUInt192::oversizedType last = 0;
- for (size_t i = 0; i < BigUInt192::arraySize; i++) {
- BigUInt192::oversizedType partSum = left.parts[i] + right.parts[i] + last;
- if (partSum >> 16 != 0) {
- last = 1; // Переносим бит из последнего разряда (17й)
- } else {
- last = 0;
- }
- result.parts[i] = static_cast<BigUInt192::baseType>(partSum);
- }
- return result;
- }
- BigUInt192 operator-(const BigUInt192 &left, const BigUInt192 &right)
- {
- BigUInt192 result;
- bool last = false;
- for (size_t i = 0; i < BigUInt192::arraySize; i++) {
- const BigUInt192::oversizedType toSub = right.parts[i] + last;
- BigUInt192::oversizedType partSub;
- if (left.parts[i] >= toSub) {
- partSub = left.parts[i] - toSub;
- last = false;
- } else {
- partSub = (1 << 8 * sizeof(BigUInt192::baseType)) + left.parts[i];
- partSub -= right.parts[i];
- last = true;
- }
- result.parts[i] = static_cast<BigUInt192::baseType>(partSub);
- }
- return result;
- }
- bool operator>(const BigUInt192 &left, const BigUInt192 &right)
- {
- for (size_t i = 0; i < BigUInt192::arraySize; i++) {
- const size_t index = BigUInt192::arraySize - i - 1;
- if (left.parts[index] == right.parts[index])
- continue;
- return left.parts[index] > right.parts[index];
- }
- return false;
- }
- bool operator==(const BigUInt192 &left, const BigUInt192 &right)
- {
- for (size_t i = 0; i < BigUInt192::arraySize; i++) {
- if (left.parts[i] != right.parts[i])
- return false;
- }
- return true;
- }
- bool operator!=(const BigUInt192 &left, const BigUInt192 &right)
- {
- return !(left == right);
- }
- bool operator<(const BigUInt192 &left, const BigUInt192 &right)
- {
- return !(left > right) && left != right;
- }
- bool operator<=(const BigUInt192 &left, const BigUInt192 &right)
- {
- return left < right || left == right;
- }
- bool operator>=(const BigUInt192 &left, const BigUInt192 &right)
- {
- return left > right || left == right;
- }
- BigUInt192 operator&(const BigUInt192 &left, const BigUInt192 &right)
- {
- BigUInt192 result;
- for (size_t i = 0; i < BigUInt192::arraySize; i++) {
- result.parts[i] = left.parts[i] & right.parts[i];
- }
- return result;
- }
- BigUInt192 operator|(const BigUInt192 &left, const BigUInt192 &right)
- {
- BigUInt192 result;
- for (size_t i = 0; i < BigUInt192::arraySize; i++) {
- result.parts[i] = left.parts[i] | right.parts[i];
- }
- return result;
- }
- BigUInt192 operator^(const BigUInt192 &left, const BigUInt192 &right)
- {
- BigUInt192 result;
- for (size_t i = 0; i < BigUInt192::arraySize; i++) {
- result.parts[i] = left.parts[i] ^ right.parts[i];
- }
- return result;
- }
- BigUInt192 operator~(const BigUInt192 &left)
- {
- BigUInt192 result;
- for (size_t i = 0; i < BigUInt192::arraySize; i++) {
- result.parts[i] = ~left.parts[i];
- }
- return result;
- }
- BigUInt192 operator<<(const BigUInt192 &left, size_t shift)
- {
- BigUInt192::baseType mask = 0x0;
- for (size_t i = 0; i < shift; i++) {
- mask = static_cast<BigUInt192::baseType>(mask << 1 | 1);
- }
- BigUInt192 result;
- BigUInt192::baseType last = 0;
- for (size_t i = 0; i < BigUInt192::arraySize; i++) {
- result.parts[i] = (left.parts[i] << shift) | last;
- last = (left.parts[i] >> (8 * sizeof(BigUInt192::baseType) - shift)) & mask;
- }
- return result;
- }
- BigUInt192 operator>>(const BigUInt192 &left, size_t shift)
- {
- BigUInt192::baseType mask = 0x0;
- for (size_t i = 0; i < shift; i++) {
- mask = static_cast<BigUInt192::baseType>(mask << 1 | 1);
- }
- BigUInt192 result;
- BigUInt192::baseType last = 0;
- for (size_t i = 0; i < BigUInt192::arraySize; i++) {
- const size_t index = BigUInt192::arraySize - i - 1;
- result.parts[index] = (left.parts[index] >> shift);
- result.parts[index] |= last << (8 * sizeof(BigUInt192::baseType) - shift);
- last = left.parts[index] & mask;
- }
- return result;
- }
- BigUInt192 circledShiftL(const BigUInt192 &left, size_t shift)
- {
- BigUInt192::baseType mask = 0x0;
- for (size_t i = 0; i < shift; i++) {
- mask = static_cast<BigUInt192::baseType>(mask << 1 | 1);
- }
- BigUInt192 result;
- BigUInt192::baseType last = 0;
- for (size_t i = 0; i < BigUInt192::arraySize; i++) {
- result.parts[i] = (left.parts[i] << shift) | last;
- last = (left.parts[i] >> (8 * sizeof(BigUInt192::baseType) - shift)) & mask;
- }
- result.parts[0] |= last;
- return result;
- }
- BigUInt192 circledShiftR(const BigUInt192 &left, size_t shift)
- {
- BigUInt192::baseType mask = 0x0;
- for (size_t i = 0; i < shift; i++) {
- mask = static_cast<BigUInt192::baseType>(mask << 1 | 1);
- }
- BigUInt192 result;
- BigUInt192::baseType last = 0;
- for (size_t i = 0; i < BigUInt192::arraySize; i++) {
- const size_t index = BigUInt192::arraySize - i - 1;
- result.parts[index] = (left.parts[index] >> shift);
- result.parts[index] |= last << (8 * sizeof(BigUInt192::baseType) - shift);
- last = left.parts[index] & mask;
- }
- result.parts[BigUInt192::arraySize - 1] |= last << (8 * sizeof(BigUInt192::baseType) - shift);
- return result;
- }
- std::istream &operator>>(std::istream &istream, BigUInt192 &value)
- {
- std::string s;
- istream >> s;
- value.erase();
- for (auto it = s.crbegin(); it != s.crend(); it++) {
- const size_t i = it - s.crbegin();
- assert(*it == '0' || *it == '1');
- if (*it == '0')
- continue;
- const size_t index = i / (sizeof(BigUInt192::baseType) * 8);
- const size_t shift = i % (sizeof(BigUInt192::baseType) * 8);
- value.parts[index] |= (*it - '0') << shift;
- }
- return istream;
- }
- std::ostream &operator<<(std::ostream &ostream, const BigUInt192 &value)
- {
- bool numberStarted = false;
- for (size_t i = 0; i < BigUInt192::arraySize; i++) {
- const size_t index = BigUInt192::arraySize - i - 1;
- const size_t bits = sizeof(BigUInt192::baseType) * 8;
- for (int j = 0; j < bits; j++) {
- const size_t shift = bits - j - 1;
- auto numeral = (value.parts[index] >> shift) & 0x1;
- if ((numeral == 0 && numberStarted) || numeral == 1)
- ostream << numeral;
- if (numeral == 1 && !numberStarted)
- numberStarted = true;
- }
- }
- return ostream;
- }
- int main()
- {
- BigUInt192 b;
- std::cout << (uint16_t)(12-32) << std::endl;
- BigUInt192 v;
- //std::cout << "Enter BIN big value: ";
- std::cin >> v;
- std::cin >> b;
- std::cout << circledShiftR(v, 5) << std::endl;
- std::cout << circledShiftL(v, 5) << std::endl;
- std::cout << v + b << std::endl;
- std::cout << v - b << std::endl;
- std::cout << (v & b) << std::endl;
- std::cout << (v | b) << std::endl;
- std::cout << (v > b) << std::endl;
- std::cout << "Hello, World!" << std::endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement