Coding Grandmaster ยท project 16 of 46 ยท premium, free for now
Set, clear and toggle
Write set_bit(n, i), clear_bit(n, i), toggle_bit(n, i) and test_bit(n, i) (True or False) using shifts and bitwise operators.
Learn
To work with a single bit, make a mask with 1 << position. OR (|) with the mask turns a bit on, AND (&) with the inverted mask (~mask) turns it off, XOR (^) flips it, and AND with the mask tests it. These four moves are the whole toolbox of low-level programming.
Your task
Write set_bit(n, i), clear_bit(n, i), toggle_bit(n, i) and test_bit(n, i) (True or False) using shifts and bitwise operators.
Stretch challenge
Use these to turn the seven lights of a display on and off with one number.
Press Run to see what your program prints.
More in this module
Keep going in Coding Grandmaster
Seeing the bits
Print each of 5, 12 and 255 as 8 binary digits, and write count_ones(n) that counts the 1 bits using & and >>.
Start โ14One-line bit tricks
Write is_power_of_two(n) using n & (n - 1), is_odd(n) using n & 1, and print the powers of two up to 200 using a list comprehension.
Start โ15XOR magic
Swap a and b with three XOR lines (no temporary variable), and write lonely(numbers) that returns the number that appears only once by XOR-ing everything together.
Start โ17Every subset
Write subsets(items) returning a list of every subset as a list, in the order of the counter 0 to 2^n โ 1, using bit tests. Print how many subsets four items have, and the subsets of two items.
Start โ18Permission flags
With READ = 4, WRITE = 2 and EXEC = 1, write grant(perms, flag), revoke(perms, flag), has(perms, flag) and describe(perms) which returns text like r-x (r, w and x, or - for a missing one).
Start โPractise the idea
๐ก Play Binary Bits
Switch bit values to make the decimal number shown on screen.
FAQ
Set, clear and toggle: common questions
I am stuck. What can I do?
Press Hint for a nudge, read the message next to each goal, and compare your code with the lesson text. If you still need it, Show answer reveals a model solution. Try to type it yourself rather than copy it.
Does my work get saved?
Your ticks and XP are saved in this browser, and in your account if you log in. The code you type is not stored on a server.
Start a conversation
Enjoying coding? Meet a teacher.
Tell us the learner's age, country and what they would like to build, and we will suggest a class. A parent or guardian can make the enquiry.
- Tell us the learnerโs age or grade and goals
- Weโll suggest a suitable class or learning path
- Weโll confirm format, timing and fees with a parent or guardian
Play it, don't just read it
Write some code right now
Free games that need no sign-up. Pick one and play for five minutes.
Pick a game
- Code BuilderThe lines of a Python program are mixed up. Tap them into the rightโฆPlay
- Bug HunterA Python program prints the wrong answer. Find the one line with theโฆPlay
- Predict the OutputRead a short Python program and choose what it prints. Then watch it runโฆPlay
- Code JungleWrite real Python to guide Miko the monkey to every banana. 24 levels:โฆPlay