This commit is contained in:
Alex Hyett 2024-10-04 14:28:50 +01:00
parent b758db79c6
commit 19bc46cd3b
8 changed files with 77 additions and 1 deletions

View file

@ -1,3 +1,6 @@
# bitwise-operators-example
The code for my video on Bitwise Operators
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.

13
and.py Normal file
View file

@ -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))

9
left.py Normal file
View file

@ -0,0 +1,9 @@
number = 5
print(number)
print(bin(number))
number = number << 2
print(number)
print(bin(number))

9
not.py Normal file
View file

@ -0,0 +1,9 @@
number = 5
print(number)
print(bin(number))
number = ~number
print(number)
print(bin(number))

8
or.py Normal file
View file

@ -0,0 +1,8 @@
READ_PERMISSION = 4
WRITE_PERMISSION = 2
EXECUTE_PERMISSION = 1
userPermissions = READ_PERMISSION | WRITE_PERMISSION
print(userPermissions)
print(bin(userPermissions))

10
or2.py Normal file
View file

@ -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))

14
right.py Normal file
View file

@ -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))

10
xor.py Normal file
View file

@ -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))