Skip to the workspace
Pebellslearn by building
Expressions & Operators  ›  Lesson

Bitwise Operators

Bitwise Operators 12 min

javascript / Bitwise Operators

Explore each scene at your own pace.

01 · Bitwise Operators

Bitwise Operators

Bitwise Operators

Bitwise operators act on integer bit patterns. Number bitwise operations typically convert operands to 32-bit integers; BigInt has separate integer bitwise behavior.

Flags can share one number, but ordinary reading counts should not be rounded with a bitwise shortcut.

See the mechanism

Walk through the modelBitwise OperatorsAuthored snapshots of the example; use Run below to test the code.
SAVED01
FEATURED10
Each independent flag occupies one bit.
1 of 3

Follow one example

javascript

Binary 01 and 10 combine with OR into 11, decimal 3. AND with 01 checks the saved bit. AND with the inverted saved mask clears that bit, leaving 10, decimal 2. Number coercion can discard fractions and high-order information, so do not use | 0 as a general safe conversion.

Predict, change and check

Predict: If only FEATURED is set, what does flags & SAVED produce?

Test your explanation: Test 0, 1, 2 and 3. Write their two-bit forms beside the results before adding another flag.

The tasks below ask you to explain that result and apply the idea to different inputs.

Apply and distinguish

A bit mask is a chosen representation

Most Number bitwise operations convert values to 32-bit integer representations, which can discard information from larger or fractional inputs. The unsigned right shift produces an unsigned 32-bit Number result; BigInt has different supported operators and does not support unsigned right shift.

For permission-like flags, define which bit represents each flag and keep those meanings stable. x & mask tests selected bits; x | mask sets bits; x & ~mask clears bits. A nonzero mask result is not automatically true unless you convert it or compare it deliberately. Test absent, present and combined flags.

Check your understanding: State the input contract, identify the operation that changes the result, and give one boundary input that would distinguish the correct rule from the mistake described above.

Scene 1 of 2
Project context and further resources
Additional resources
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.