Bitwise Operators
In short: bitwise operators work directly on a number's individual
binary digits (bits), each either 0 or 1. & (AND) turns on a result
bit only where both sides have it on. | (OR) turns it on where
either side does. Think of each number as a row of light switches: &
keeps a switch on only if both rows agree; | keeps it on if either row
does. Their everyday use is packing several true/false flags into a single
number, and testing one flag without disturbing the others.

Combine and isolate bits of 6 and 3
Write 6 and 3 as binary — 0110 and 0011 — and &/| compare them
position by position:
6 & 3 is 2 — only the third position (...10) has a 1 in both
0110 and 0011, so only that bit survives. 6 | 3 is 7 — every
position that's 1 in either number ends up 1: 0111. .toString(2)
converts a number to its binary text form, which is how 0110 and 0011
were checked.
Pack three permissions into one number
Each permission gets its own bit, so | can combine several flags into a
single number, and & can test for one of them later:
perms is 5 — the CAN_EDIT bit and the CAN_SHARE bit are both on,
CAN_DELETE's bit is not. perms & CAN_DELETE masks out every bit except
CAN_DELETE's, leaving 0 — so === 0 is true, meaning delete was
never granted. perms & CAN_EDIT isolates a bit that is set, giving a
non-zero result, so === 0 is false — edit is granted.
Quick practice
What is 6 & 3?
What is 6 | 3?
Given const perms = 5; (binary 101) and const CAN_DELETE = 2; (binary 010), what is perms & CAN_DELETE?
What is the everyday use of bitwise & and | shown in this lesson?
What does (6).toString(2) return?
🎯 Your turn
Given const CAN_EDIT = 1, CAN_DELETE = 2, CAN_SHARE = 4; const perms = CAN_EDIT | CAN_SHARE;,
explain what perms equals in binary and decimal, and how
(perms & CAN_DELETE) === 0 tells you whether delete permission is
granted.
Write your explanation and submit it — there's no code to run here, just your reasoning.
