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