Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <algorithm>
- using namespace std;
- // Класс для выполнения операций с 64-битовыми строками
- class BitString {
- private:
- string bitString;
- unsigned long firstPart = 0;
- unsigned long secondPart = 0;
- int stringLength = 64;
- // Перевод битовой 64-строки в два числа типа unsigned long
- void translateStringToNumbers() {
- for (int i = 0; i < stringLength; i++) {
- if (i < stringLength / 2) {
- firstPart += (bitString[i] - '0') * pow(2, stringLength / 2 - 1 - i);
- }
- else {
- secondPart += (bitString[i] - '0') * pow(2, stringLength - 1 - i);
- }
- }
- }
- public:
- // Ввод с клавиатуры
- void read() {
- do {
- cout << "Ввод битовой строки: ";
- cin >> bitString;
- } while (bitString.size() != stringLength);
- cout << "\n";
- translateStringToNumbers();
- }
- // Операция "И"
- BitString _and(BitString other) {
- BitString newBitString;
- unsigned long tmpFirstThis, tmpSecondThis, tmpFirstOther, tmpSecondOther;
- tmpFirstThis = firstPart;
- tmpSecondThis = secondPart;
- tmpFirstOther = other.firstPart;
- tmpSecondOther = other.secondPart;
- for (int i = 0; i < stringLength; i++) {
- if (i < stringLength / 2) {
- newBitString.firstPart += (tmpFirstThis % 2) * (tmpFirstOther % 2)
- * pow(2, i);
- tmpFirstThis /= 2;
- tmpFirstOther /= 2;
- }
- else {
- newBitString.secondPart += (tmpSecondThis % 2) * (tmpSecondOther % 2)
- * pow(2, i - stringLength / 2);
- tmpSecondThis /= 2;
- tmpSecondOther /= 2;
- }
- }
- return newBitString;
- }
- // Операция "ИЛИ"
- BitString _or(BitString other) {
- BitString newBitString;
- unsigned long tmpFirstThis, tmpSecondThis, tmpFirstOther, tmpSecondOther;
- tmpFirstThis = firstPart;
- tmpSecondThis = secondPart;
- tmpFirstOther = other.firstPart;
- tmpSecondOther = other.secondPart;
- for (int i = 0; i < stringLength; i++) {
- if (i < stringLength / 2) {
- newBitString.firstPart +=
- min((int)((tmpFirstThis % 2) + (tmpFirstOther % 2)), 1)
- * pow(2, i);
- tmpFirstThis /= 2;
- tmpFirstOther /= 2;
- }
- else {
- newBitString.secondPart +=
- min((int)((tmpSecondThis % 2) + (tmpSecondOther % 2)), 1)
- * pow(2, i - stringLength / 2);
- tmpSecondThis /= 2;
- tmpSecondOther /= 2;
- }
- }
- return newBitString;
- }
- // Операция "Исключающее ИЛИ"
- BitString _xor(BitString other) {
- BitString newBitString;
- unsigned long tmpFirstThis, tmpSecondThis, tmpFirstOther, tmpSecondOther;
- tmpFirstThis = firstPart;
- tmpSecondThis = secondPart;
- tmpFirstOther = other.firstPart;
- tmpSecondOther = other.secondPart;
- for (int i = 0; i < stringLength; i++) {
- if (i < stringLength / 2) {
- newBitString.firstPart +=
- (abs((int)((tmpFirstThis % 2) - (tmpFirstOther % 2))))
- * pow(2, i);
- tmpFirstThis /= 2;
- tmpFirstOther /= 2;
- }
- else {
- newBitString.secondPart +=
- (abs((int)((tmpSecondThis % 2) - (tmpSecondOther % 2))))
- * pow(2, i - stringLength / 2);
- tmpSecondThis /= 2;
- tmpSecondOther /= 2;
- }
- }
- return newBitString;
- }
- // Инверсия
- void _not() {
- unsigned long tmpFirst, tmpSecond;
- tmpFirst = firstPart;
- tmpSecond = secondPart;
- firstPart = 0;
- secondPart = 0;
- for (int i = 0; i < stringLength; i++) {
- if (i < stringLength / 2) {
- if (tmpFirst % 2 == 0) {
- firstPart += pow(2, i);
- }
- tmpFirst /= 2;
- }
- else {
- if (tmpSecond % 2 == 0) {
- secondPart += pow(2, i - stringLength / 2);
- }
- tmpSecond /= 2;
- }
- }
- }
- // Сдвиг влево
- void shiftLeft() {
- unsigned long tmpFirst, tmpSecond;
- tmpFirst = firstPart;
- tmpSecond = secondPart;
- firstPart = 0;
- secondPart = 0;
- for (int i = 0; i < stringLength; i++) {
- if (i < stringLength / 2) {
- if (i != stringLength / 2 - 1) {
- firstPart += (tmpFirst % 2) * pow(2, i + 1);
- }
- tmpFirst /= 2;
- }
- else {
- if (i != stringLength - 1) {
- secondPart += (tmpSecond % 2) * pow(2, i - stringLength / 2 + 1);
- }
- else {
- firstPart += (tmpSecond % 2);
- }
- tmpSecond /= 2;
- }
- }
- }
- // Сдвиг вправо
- void shiftRight() {
- unsigned long tmpFirst, tmpSecond;
- tmpFirst = firstPart;
- tmpSecond = secondPart;
- firstPart = 0;
- secondPart = 0;
- for (int i = 0; i < stringLength; i++) {
- if (i < stringLength / 2) {
- if (i != 0) {
- firstPart += (tmpFirst % 2) * pow(2, i - 1);
- }
- else {
- secondPart += (tmpFirst % 2) * pow(2, stringLength / 2 - 1);
- }
- tmpFirst /= 2;
- }
- else {
- if (i != stringLength) {
- secondPart += (tmpSecond % 2) * pow(2, i - stringLength / 2 - 1);
- }
- tmpSecond /= 2;
- }
- }
- }
- // Вывод на экран
- void display() {
- bitString = "";
- unsigned long tmpFirst, tmpSecond;
- tmpFirst = firstPart;
- tmpSecond = secondPart;
- // Формирование битовой строки
- for (int i = stringLength - 1; i >= 0; i--) {
- if (i < stringLength / 2) {
- bitString = to_string(tmpFirst % 2) + bitString;
- tmpFirst /= 2;
- }
- else {
- bitString = to_string(tmpSecond % 2) + bitString;
- tmpSecond /= 2;
- }
- }
- cout << "Вывод битовой строки: " << bitString << "\n\n";
- }
- };
- int main() {
- // Пример работы с объектами класса
- BitString str1, str2, str3;
- str1.read();
- str2.read();
- str3 = str1._and(str2);
- str3.display();
- str3 = str1._or(str2);
- str3.display();
- str3 = str1._xor(str2);
- str3.display();
- str3._not();
- str3.display();
- str3.shiftLeft();
- str3.display();
- str3.shiftRight();
- str3.display();
- return 0;
- }
Add Comment
Please, Sign In to add comment