Ad space (banner)
🟨JavaScript Lessons
Lesson 55 / 69

Bitwise Operations (AND, OR, XOR, and Shift Operations)

In this lesson you'll learn the basics of bitwise operations in JavaScript, so you can understand this low-level way of treating numbers as binary. This is for people searching "JavaScript bitwise operations AND OR."

Bitwise operations treat a number as binary and compute it bit by bit. There's & (AND), | (OR), ^ (XOR), and << (left shift), commonly used for managing flags and other low-level processing. This requires a different way of thinking than ordinary arithmetic — comparing and computing digit by digit in binary.

The sample code tries out AND, OR, XOR, and left shift on two numbers, 12 (1100 in binary) and 10 (1010 in binary). & becomes 1 only when both bits are 1, and | becomes 1 if either bit is 1 — the final number is determined by piling up these digit-by-digit results.

You won't get many opportunities to use this in everyday app development, but it's useful knowledge for performance-critical processing, game development, and the foundations of cryptography. The technique of packing several on/off flags into a single number, called a "bit flag," is a representative application of bitwise operations.

Even though everyday app development uses it rarely, it helps you understand how a computer actually works under the hood. Bitwise operations come up as essential foundational knowledge whenever you step into lower-level territory like image processing, cryptography, or implementing a network protocol.

JavaScript
OUTPUT

💡 Anything passed to console.log() appears in the output below.

Ad space (banner)
Ad space (in-article)