Bit Manipulation
9 JavaScript bit manipulation challenges, each with test cases you can run in the browser.
- isPowerOfTwo — Easy. Given an integer n, return true if it is a power of two. Otherwise, return false. An integer n is a power of two, if there exists an integer
- countSetBits — Medium. Given a non-negative integer, count the number of '1's in its binary representation (also known as the Hamming weight).
- isEvenOddBitwise — Easy. Determine if a number is even or odd using bitwise operators. Do not use the modulo operator (%).
- flipNthBit — Medium. Write a function that flips the nth bit of a given integer (0-indexed from the right). For example, flipping the 2nd bit of 10 (binary 1010)
- isBitSet — Easy. Check if the nth bit of a number is set (i.e., is 1). The index n is 0-based from the right.
- multiplyByTwo — Easy. Multiply an integer by 2 using only bitwise operators.
- divideByTwo — Easy. Divide an integer by 2 and truncate the result (integer division) using only bitwise operators.
- singleNumber — Medium. Given a non-empty array of integers, every element appears twice except for one. Find that single one.
- toBinaryString — Easy. Given a non-negative integer, return its binary representation as a string.