From 1b9b14b04278894204147c96859dee778d6e5c38 Mon Sep 17 00:00:00 2001 From: Mathias Wagner Date: Sat, 11 Nov 2023 23:21:27 +0100 Subject: [PATCH] Created the bitwise_encoder.cpp --- bitwise_encoder.cpp | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 bitwise_encoder.cpp diff --git a/bitwise_encoder.cpp b/bitwise_encoder.cpp new file mode 100644 index 0000000..ebe8f86 --- /dev/null +++ b/bitwise_encoder.cpp @@ -0,0 +1,35 @@ +#include + +using namespace std; + +int main() { + int inventory = 0; + + string input; + do { + cout << "Drücke Enter, um das Programm zu beenden." << endl; + cout << "Gib ein Item ein, um es zu aktivieren oder zu deaktivieren (IDS 1-10): "; + getline(cin, input); + + if (input.empty()) break; + + int item = stoi(input); + + if (item < 1 || item > 10) { + cout << "Ungültiges Item. Bitte versuche es erneut." << endl; + continue; + } + + if ((inventory & (1 << (item - 1))) != 0) { + inventory &= ~(1 << (item - 1)); + cout << "Item #" << item << " wurde deaktiviert." << endl; + } else { + inventory |= 1 << (item - 1); + cout << "Item #" << item << " wurde aktiviert." << endl; + } + } while (!input.empty()); + + cout << "Dein Inventar als Bitset: " << inventory << endl; + + return 0; +} \ No newline at end of file