From 19bc46cd3b31138d3a2bbea5cc3cdbe7f40db0d3 Mon Sep 17 00:00:00 2001 From: Alex Hyett Date: Fri, 4 Oct 2024 14:28:50 +0100 Subject: [PATCH] Add code --- README.md | 5 ++++- and.py | 13 +++++++++++++ left.py | 9 +++++++++ not.py | 9 +++++++++ or.py | 8 ++++++++ or2.py | 10 ++++++++++ right.py | 14 ++++++++++++++ xor.py | 10 ++++++++++ 8 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 and.py create mode 100644 left.py create mode 100644 not.py create mode 100644 or.py create mode 100644 or2.py create mode 100644 right.py create mode 100644 xor.py diff --git a/README.md b/README.md index 7b73007..51d9213 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,6 @@ # bitwise-operators-example -The code for my video on Bitwise Operators \ No newline at end of file +The code for my video on [Bitwise Operators and WHY we use them](https://youtu.be/igIjGxF2J-w). + +> [!NOTE] +> [This repository](https://code.alexhyett.com/alexhyett/bitwise-operators-example) is also mirrored on [Codeberg.org](https://codeberg.org/alexhyett/bitwise-operators-example) if you want to raise an issue. \ No newline at end of file diff --git a/and.py b/and.py new file mode 100644 index 0000000..58220dc --- /dev/null +++ b/and.py @@ -0,0 +1,13 @@ +READ_PERMISSION = 4 +WRITE_PERMISSION = 2 +EXECUTE_PERMISSION = 1 + +userPermissions = 6 + +if (userPermissions & READ_PERMISSION) == READ_PERMISSION: + print("Can Read") +else: + print("Cannot Read") + +print(userPermissions & READ_PERMISSION) +print(bin(userPermissions & READ_PERMISSION)) diff --git a/left.py b/left.py new file mode 100644 index 0000000..99caaa6 --- /dev/null +++ b/left.py @@ -0,0 +1,9 @@ +number = 5 + +print(number) +print(bin(number)) + +number = number << 2 + +print(number) +print(bin(number)) \ No newline at end of file diff --git a/not.py b/not.py new file mode 100644 index 0000000..a092fec --- /dev/null +++ b/not.py @@ -0,0 +1,9 @@ +number = 5 + +print(number) +print(bin(number)) + +number = ~number + +print(number) +print(bin(number)) \ No newline at end of file diff --git a/or.py b/or.py new file mode 100644 index 0000000..8b33857 --- /dev/null +++ b/or.py @@ -0,0 +1,8 @@ +READ_PERMISSION = 4 +WRITE_PERMISSION = 2 +EXECUTE_PERMISSION = 1 + +userPermissions = READ_PERMISSION | WRITE_PERMISSION + +print(userPermissions) +print(bin(userPermissions)) diff --git a/or2.py b/or2.py new file mode 100644 index 0000000..fc132fd --- /dev/null +++ b/or2.py @@ -0,0 +1,10 @@ +READ_PERMISSION = 4 +WRITE_PERMISSION = 2 +EXECUTE_PERMISSION = 1 + +userPermissions = READ_PERMISSION | WRITE_PERMISSION + +userPermissions |= EXECUTE_PERMISSION + +print(userPermissions) +print(bin(userPermissions)) \ No newline at end of file diff --git a/right.py b/right.py new file mode 100644 index 0000000..b6dd000 --- /dev/null +++ b/right.py @@ -0,0 +1,14 @@ +number = 5 + +print(number) +print(bin(number)) + +number = number << 2 + +print(number) +print(bin(number)) + +number = number >> 2 + +print(number) +print(bin(number)) \ No newline at end of file diff --git a/xor.py b/xor.py new file mode 100644 index 0000000..ef4002d --- /dev/null +++ b/xor.py @@ -0,0 +1,10 @@ +READ_PERMISSION = 4 +WRITE_PERMISSION = 2 +EXECUTE_PERMISSION = 1 + +userPermissions = READ_PERMISSION | WRITE_PERMISSION + +userPermissions ^= WRITE_PERMISSION + +print(userPermissions) +print(bin(userPermissions)) \ No newline at end of file