Resource · Guide or article
Bit Manipulation
Collection of short bit manipulation recipes, each with a one-paragraph explanation and a pointer to its JavaScript file in the folder. Good for brushing up before questions on masks, shifts and two's complement.
Reviewed
Open the original: Bit Manipulation (opens the original site in a new tab)Add to a roadmap
Keep this resource with a roadmap you follow. It appears as your own extra resource; the roadmap's steps don't change and it doesn't count toward your tracked steps.
Source content: English
Read this in other languages: français, 简体中文. This method shifts the relevant bit to the zeroth position. Then we perform AND operation with one which has bit pattern like 0001. This clears all bits from the original number except the relevant one. If the relevant bit is one, the result is 1, otherwise the result is 0. This method shifts 1 over by bitPosition bits, creating a value that looks like 00100. Then we perform OR operation that sets specific bit into 1 but it does not affect on other bits of the number.
PrepKit overview
PrepKit editors wrote and reviewed this overview from the source repository. It is not text from the resource itself.
- Suited to
- The source does not state a level
- Format
- Cheatsheet
What it covers
- Get, set, clear and update a bit with shifted masks and AND or OR
- Parity and sign checks, multiply and divide by two with shifts, and sign switching by two's complement
- Signed multiplication by recursive halving, stated as O(log(b)), and unsigned multiplication as a sum of shifted values
- Counting set bits, bits to flip between two numbers using XOR, and bit length
- The n AND (n - 1) power-of-two test with worked examples for 4 and 10
- A full adder that sums two 32-bit integers bit by bit, traced for 3 plus 6 in a table
How to use it
PrepKit study advice, not a claim made by the source.
Cover each heading's explanation, write the operation from memory, then compare with the matching .js file. Finish by solving single-number and counting-bits style problems using only these recipes.
Scope and limitations
Explanations are brief and the implementations live in separate JavaScript files. JavaScript bitwise operators work on 32-bit integers, which the page touches only in the full adder section. No practice problems.
Access and format
README on GitHub in the MIT-licensed trekhleb/javascript-algorithms repository; English, free to read; French and Chinese translations are linked.
Reviewed by PrepKit on Oct 2, 2026. Listing source commit: 85293e3e2b88
Evidence behind this overview
- src/algorithms/math/bits/README.md
Pages that include this
Get the PrepKit newsletter
Optional. You don't need it to use PrepKit.