wleci
AboutProjectsBlogContact
Contact
Back to blog
C++TutorialLow-levelFeatured

Bit Magic: A Comprehensive Guide to Bitwise Operations in C++

Understand bitwise operations from scratch. We explain step-by-step how to manipulate zeros and ones in C++ to write more efficient code.

February 4, 20265 min read
Share:

Bit Magic: Understanding Bitwise Operations in C++ (Step-by-Step)

Welcome to a world where the decimal system doesn't exist, and everything boils down to two states: 0 and 1. If you've ever wondered how computers "think" under the hood, this article is for you. We will explain bitwise operations so simply that anyone can understand them, even if your only contact with math was counting change at the store.


1. The Foundations: What is a Bit?

Imagine a row of lightbulbs. Each bulb can either be off (0) or on (1). This is exactly what a bit is – the smallest unit of information.

In a computer, numbers you know (e.g., 5, 10, 42) are stored as sequences of these bulbs (bits). This system is called the binary system.

Example: The number 5 in binary

In the decimal system, we have ones, tens, hundreds. In binary, we have powers of 2:

  • 22=42^2 = 422=4
  • 21=22^1 = 221=2
  • 20=12^0 = 120=1

The number 5 is 4+14 + 14+1, which in binary notation is: 101.


2. The AND Operator (&) – "The Interrogator"

The & operator compares two bits. The result is 1 only if both input bits are 1. In any other case, the result is 0.

Truth Table for AND:

ABResult (A & B)
000
010
100
111

Example in C++:

cpp
int a = 5; // Binary: 0101 int b = 3; // Binary: 0011 int res = a & b; // Step-by-step analysis: // 0 1 0 1 (5) // & 0 0 1 1 (3) // --------- // 0 0 0 1 (Result is 1)

3. The OR Operator (|) – "The Optimist"

The | operator returns 1 if at least one of the compared bits is 1. It is very liberal.

Truth Table for OR: | A | B | Result (A | B) | |---|---|----------------| | 0 | 0 | 0 | | 0 | 1 | 1 | | 1 | 0 | 1 | | 1 | 1 | 1 |

Example in C++:

cpp
int a = 5; // Binary: 0101 int b = 3; // Binary: 0011 int res = a | b; // Analysis: // 0 1 0 1 // | 0 0 1 1 // --------- // 0 1 1 1 (Result is 7: 4+2+1)

4. The XOR Operator (^) – "The Exclusive Choice"

This is my favorite operator. It returns 1 only when the bits are different. If they are the same (both 0 or both 1), it returns 0.

Truth Table for XOR:

ABResult (A ^ B)
000
011
101
110

Why is XOR brilliant?

You can use it to swap two variables without using a third, temporary variable!

cpp
int x = 10, y = 20; x = x ^ y; y = x ^ y; x = x ^ y; // Now x = 20, y = 10. Magic!

5. Bit Shifts (<< and >>)

This is like shifting all the digits in a number to the left or right.

Left Shift (<<)

We add zeros from the right side. Every shift by 1 position is a multiplication by 2.

cpp
int x = 5; // 0000 0101 int res = x << 1; // 0000 1010 (That's 10!)

Right Shift (>>)

We remove bits from the right side. Every shift by 1 position is an integer division by 2.

cpp
int x = 20; // 0001 0100 int res = x >> 2; // 0000 0101 (That's 5!)

6. Practical Application: Bitmasks

Imagine you are building a game and want to store a character's status (is alive, has poison, can fly) in a single number to save memory.

cpp
const unsigned char ALIVE = 1 << 0; // 0001 const unsigned char POISONED = 1 << 1; // 0010 const unsigned char FLYING = 1 << 2; // 0100 unsigned char playerStatus = 0; // Setting a flag (OR) playerStatus |= ALIVE; // Now the player is alive // Checking a flag (AND) if (playerStatus & ALIVE) { // The player is indeed alive } // Turning off a flag (AND with NOT) playerStatus &= ~ALIVE; // The player dies

Summary

Bitwise operations might seem difficult at first, but it's just a matter of looking at numbers as a collection of switches. They are incredibly fast because the processor executes them directly on its registers in a single clock cycle.

Remember:

  • & – both must be 1.
  • | – at least one must be 1.
  • ^ – they must be different.
  • ~ – flip everything.
  • << and >> – multiply or divide by powers of 2.

Want to practice? Try writing a function that checks if a number is even using only the & operator!

You might also like

C++Programming

C++ String and Number Conversion: Deep Dive into sprintf and strtof

A comprehensive guide to classic conversion methods in C++. Learn how to safely use sprintf and strtof, and explore their modern alternatives.

4 min read
C++Memory

Dynamic Memory Allocation in C++: Building and Demolishing in RAM

Understand the new and delete operators. We explain the difference between the stack and the heap, how to manage memory, and avoid memory leaks.

4 min read
C++Tutorial

C++ References: Meet the 'Nicknames' of Your Variables

Understand references once and for all. How do they differ from pointers? How to use the & operator in declarations? A step-by-step guide.

4 min read
Back to blog
wleci.pl

Full-stack Developer

I build modern web applications with passion for clean code and good design.

[email protected]
Poland

Navigation

  • Home
  • About
  • Projects
  • Blog
  • Contact

Services

  • Web applications
  • Websites
  • API & Backend
  • Consulting

Technologies

  • React / Next.js
  • TypeScript
  • Node.js
  • PostgreSQL

Social

© 2026 wleci.pl. All rights reserved.

Privacy policy•Terms of service

Made with in Poland