

0 / 2 embers
0 / 3000 xp
click for more info
Complete a lesson to start your streak
click for more info
Difficulty: 5
click for more info
Not enough gems
Cost: 6 gems
1: Python Numbers
incomplete
2: Numbers Review
incomplete
3: Floor Division
incomplete
4: Exponents
incomplete
5: Changing in Place
incomplete
6: Plus Equals
incomplete
7: Scientific Notation
incomplete
8: Logical Operators
incomplete
9: Not
incomplete
10: Binary Numbers
incomplete
11: Bitwise '&' Operator
incomplete
12: Bitwise '|' Operator
incomplete
13: Damage Meter
incomplete
14: Converting Binary
incomplete
Back
ctrl+,
Next
ctrl+.
This lesson's interactive features are locked, please to keep using them
Bitwise operators are similar to logical operators, but instead of operating on boolean values, they apply the same logic to all the bits in a value by column. For example, say you had the numbers 5 and 7 represented in binary. You could perform a bitwise AND operation and the result would be 5.
0101 is 50111 is 70101
&
0111
=
0101
Interactive example available with JavaScript enabled.
A 1 in binary is the same as True, while 0 is False. So really a bitwise operation is just a bunch of logical operations that are completed in tandem by column.
0 & 0 = 0
1 & 1 = 1
1 & 0 = 0
Ampersand & is the bitwise AND operator in Python. "AND" is the name of the bitwise operation, while ampersand & is the symbol for that operation. For example, 5 & 7 = 5, while 5 & 2 = 0.
0101 is 50010 is 20101
&
0010
=
0000
When writing a number in binary, the prefix 0b is used to indicate that what follows is a binary number. 0b10 is two in binary, but 10 without the 0b prefix is simply ten.
0b0101 is 50b0111 is 70b0101 & 0b0111
# equals 5
binary_five = 0b0101
binary_seven = 0b0111
binary_five & binary_seven
# equals 5
Click to play video
Sometimes applications store user permissions as binary values. If I have 4 different permissions a user can have, then I can store that as a 4-digit binary number, and if a certain bit is present, I know the permission is enabled. This can be a lot more efficient than storing entire strings.
Let's pretend we have 4 permissions related to "guilds" in Fantasy Quest ("guild" is just a fancy videogame word for "team"):
can_create_guild – Leftmost bit (0b1000)can_review_guild – Second to leftmost bit (0b0100)can_delete_guild – Second to rightmost bit (0b0010)can_edit_guild – Rightmost bit (0b0001)If a user has no permissions, their binary permissions would be 0b0000.
If a user only has the can_create_guild permission, their binary permissions would be 0b1000, but a user with can_review_guild and can_edit_guild permissions would be 0b0101.
To check for, say, the can_review_guild permission, we can perform a bitwise AND operation on the user's permissions and the enabled can_review_guild bit (0b0100). If the result is 0b0100 again, we know they have that specific permission!
user_permissions = 0b0101
can_review_guild = 0b0100
# perform bitwise AND to get the user's review permission
user_review_guild_permission = user_permissions & can_review_guild
# check if the user's review permission is equal to `can_review_guild`
Complete each of the get_XXX_bits functions. Simply use the bitwise & operation on the input of the user's permission bits and the appropriate guild permission bits variable, and return the resulting bits for them to be checked by the tests.
4 values have been provided, use the appropriate one for each function:
can_create_guildcan_review_guildcan_delete_guildcan_edit_guildThe get_XXX_bits functions return the bits and the test code compares the result to the original permission value to see if it matches!