Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Expressions & Operators  ›  Lesson

Bitwise Operators

Bitwise Operators 12 minBitwise operators (& | ^ ~ << >>) work on the individual binary digits of a number: & keeps a bit only where both sides have it on, | keeps it where either side has it on, often used to pack and test flags in one number.

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.

Two rows of switches: 6 = 0110, 3 = 0011; 6 & 3 keeps only the position where both rows are on (0010 = 2); a note that | keeps a switch on wherever EITHER row is on; plus a flags example with CAN_EDIT/CAN_DELETE/CAN_SHARE packed into one number

Combine and isolate bits of 6 and 3

Write 6 and 3 as binary — 0110 and 0011 — and &/| compare them position by position:

javascript

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:

javascript

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

quick drill

What is 6 & 3?

quick drill

What is 6 | 3?

quick drill

Given const perms = 5; (binary 101) and const CAN_DELETE = 2; (binary 010), what is perms & CAN_DELETE?

quick drill

What is the everyday use of bitwise & and | shown in this lesson?

quick drill

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.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainTesting one permission

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.