← Back to challenges

Bitwise Operations

JavaScriptHardlanguage_fundamentalslogicbit_operations

Instructions

A decimal number can be represented as a sequence of bits. To illustrate:

6 = 00000110
23 = 00010111

From the bitwise representation of numbers, we can calculate the bitwise AND, bitwise OR and bitwise XOR. Using the example above:

bitwiseAND(6, 23) ➞ 00000110

bitwiseOR(6, 23) ➞ 00010111

bitwiseXOR(6, 23) ➞ 00010001

Write three functions to calculate the bitwise AND, bitwise OR and bitwise XOR of two numbers.

Examples

bitwiseAND(7, 12) ➞ 4

bitwiseOR(7, 12) ➞ 15

bitwiseXOR(7, 12) ➞ 11

Notes

JavaScript has a useful function: toString(2), where you can see the binary representation of a decimal number.

javascript
Loading editor…
to run
Walks through the solution with reasoning and edge cases.
Next: Add up the Numbers from a Single Number