Course contentsShow
← Computer Science
Lesson 1097 of 2,87225. Computer Architecture FoundationsFree lesson

Logical and Bitwise Instructions

Using AND, OR, XOR, NOT, and shift operations for bit manipulation and logical operations.

Logical and Bitwise Instructions

What you'll learn: How assembly languages use logical operations (AND, OR, XOR, NOT) and shift instructions to manipulate individual bits and perform Boolean logic directly in hardware.

What Are Logical and Bitwise Instructions?

While arithmetic instructions work with numbers as complete values, logical and bitwise instructions let you work with individual bits inside those values. These instructions come from Boolean algebra and are fundamental to how computers make decisions, set flags, and manipulate data at the hardware level.

The Core Operations

AND (AND reg1, reg2): Sets each bit to 1 only if both corresponding bits are 1. Think of it like a filter—you can use it to "mask" certain bits, keeping only what you want. For example, 10110101 AND 00001111 produces 00000101, extracting just the lower 4 bits.

OR (OR reg1, reg2): Sets each bit to 1 if either corresponding bit is 1. This lets you "turn on" specific bits without affecting others.

XOR (XOR reg1, reg2): Sets each bit to 1 only if the bits differ. XOR is particularly useful for toggling bits or checking if values differ. Fun fact: XOR reg, reg always produces zero—a common trick to clear a register efficiently.

NOT (NOT reg): Flips every bit (0→1, 1→0). This is like a photo negative for binary data.

Shift Operations

Shift Left (SHL reg, count): Moves all bits left, filling with zeros on the right. Each left shift multiplies by 2.

Shift Right (SHR reg, count): Moves bits right. Each right shift divides by 2 (for unsigned numbers).

Real-World Uses

These instructions power everything from permission checking (AND with masks) to encryption (XOR operations) to efficient multiplication (using shifts instead of slower MUL instructions).

Key Takeaway: Logical and bitwise instructions manipulate individual bits using AND, OR, XOR, NOT, and shift operations, enabling efficient bit masking, toggling, and arithmetic at the hardware level.