From 5562f3eb30418f33ba2d2b4634e8d7a2f5d49dc5 Mon Sep 17 00:00:00 2001 From: Oskar Date: Tue, 20 Aug 2024 18:47:07 +0200 Subject: added huge comment for myself to visualize how bitwise operators work. Pretty difficult to wrap your head around. --- bitwise-operator-tests.cpp | 62 +++++++++++++++++++++++++++++++++++++++------- 1 file changed, 53 insertions(+), 9 deletions(-) diff --git a/bitwise-operator-tests.cpp b/bitwise-operator-tests.cpp index f3f8a8f..110d52e 100644 --- a/bitwise-operator-tests.cpp +++ b/bitwise-operator-tests.cpp @@ -2,11 +2,55 @@ #include /* - * - * - * - * - */ + +Awfully long comment here, but i did this to visualize what's happening and make it easier to understand + +------------------------------------------------------------------------------------------------------------------------- + +00000000 00000000 00000000 00000000 (uint32_t Quiz1) +00000000 00000000 00000000 00000001 (1UL) +<< 27 +00001000 00000000 00000000 00000000 (1UL << 27(Student)) + +passed: Quiz1 OR (1UL << 27) +00000000 00000000 00000000 00000000 (Quiz1) +00001000 00000000 00000000 00000000 (1UL << 27) +if ther is a 1 on either of the two sides then add the bit (1 -- 1 = 1) (1 -- 0 = 1) (0 -- 0 = 0) +result of Quiz1 OR (1UL << 27): +00001000 00000000 00000000 00000000 + +------------------------------------------------------------------------------------------------------------------------- + +but what if the student failed? (from the start) +failed: Quiz1 AND NOT(1UL << 27) +00000000 00000000 00000000 00000000 (Quiz1) +11110111 11111111 11111111 11111111 NOT(1UL << 27) +if there is a 1 on both sides then keep it at 1. Otherwise set it at 0. (1 -- 1 = 1) (1 -- 0 = 0) (0 -- 0 = 0) +result of Quiz1 AND NOT(1UL << 27): +00000000 00000000 00000000 00000000 + +------------------------------------------------------------------------------------------------------------------------- + +but what if we set the student to passed but then realise they failed? +failed: Quiz1 AND NOT(1UL << 27) +00001000 00000000 00000000 00000000 (Quiz1) +11110111 11111111 11111111 11111111 NOT(1UL << 27) + (1 -- 1 = 1) (1 -- 0 = 0) (0 -- 0 = 0) +result of Quiz1 AND NOT(1UL << 27): +00000000 00000000 00000000 00000000 + +------------------------------------------------------------------------------------------------------------------------- + +okay but what if we have several students that pass and we need to change one to failed? +failed: Quiz1 AND NOT(1UL << 27) +00001000 01100000 00010000 11100000 (Quiz1) +11110111 11111111 11111111 11111111 NOT(1UL << 27) + (1 -- 1 = 1) (1 -- 0 = 0) (0 -- 0 = 0) +result of Quiz1 AND NOT(1UL << 27): +00000000 01100000 00010000 11100000 + +------------------------------------------------------------------------------------------------------------------------- +*/ void cin_clear() { @@ -15,10 +59,10 @@ void cin_clear() { } int main () { - - unsigned long Quiz1 = 0; - unsigned long Student = 0; - unsigned long Pass = 0; + + uint32_t Quiz1 = 0; + uint32_t Student = 0; + uint32_t Pass = 0; const int maxval = 31; std::string choice; while(1) { -- cgit v1.2.3