Course offer · HLD · LLD · System Design at ₹799 until 30 Nov (not interview packs)

Basic Bitwise Operations in C++

C++ bitwise operators with binary output: AND, OR, XOR, NOT, left and right shift on 12 and 25, and why ~12 prints as -13 in two's complement.

BeginnerBitwise Operations ProgramsExample 1 of 5
basic-bitwise-operations.cpp
1#include <iostream>
2#include <bitset>
3using namespace std;
4
5void printBinary(int num) {
6 cout << bitset<8>(num) << " (" << num << ")" << endl;
7}
8
9int main() {
10 int a = 12; // 00001100
11 int b = 25; // 00011001
12
13 cout << "a = ";
14 printBinary(a);
15 cout << "b = ";
16 printBinary(b);
17 cout << endl;
18
19 // Bitwise AND
20 int andResult = a & b;
21 cout << "a & b (AND) = ";
22 printBinary(andResult);
23
24 // Bitwise OR
25 int orResult = a | b;
26 cout << "a | b (OR) = ";
27 printBinary(orResult);
28
29 // Bitwise XOR
30 int xorResult = a ^ b;
31 cout << "a ^ b (XOR) = ";
32 printBinary(xorResult);
33
34 // Bitwise NOT
35 int notResult = ~a;
36 cout << "~a (NOT) = ";
37 printBinary(notResult);
38
39 // Left shift
40 int leftShift = a << 2;
41 cout << "a << 2 (Left shift) = ";
42 printBinary(leftShift);
43
44 // Right shift
45 int rightShift = a >> 2;
46 cout << "a >> 2 (Right shift) = ";
47 printBinary(rightShift);
48
49 return 0;
50}

Sample Output

~a (NOT) = 11110011 (-13)

Low 8 bits shown; the int value is −13.

a << 2 (Left shift) = 00110000 (48)

12 × 4.

In this one

  • What &, |, ^, ~, <<, and >> do bit by bit
  • How to print binary with bitset
  • Why ~12 is −13
  • How shifts multiply and divide by powers of 2

How it works

  1. 1

    a = 12 (00001100), b = 25 (00011001).

  2. 2

    Print a & b, a | b, and a ^ b.

  3. 3

    Print ~a: low byte 11110011, value −13.

  4. 4

    Print a << 2 = 48 and a >> 2 = 3.

Dry run: Basic Bitwise Operations

00001100 (12)

  1. 1.& 00011001 (25)
  2. 2.= 00001000 (8)
  3. 3.| gives 00011101 (29)
  4. 4.^ gives 00010101 (21)
  00001100  (12)
& 00011001  (25)
= 00001000  (8)
| gives 00011101 (29)
^ gives 00010101 (21)

Edge cases

NOT on an unsigned byte

unsigned char c = 12; cout << (int)(unsigned char)~c;

Prints 243, because only 8 bits are kept and they are read as unsigned.

Shifting too far

int x = 1 << 32;

Shifting a 32-bit int by 32 or more is undefined behavior.

Right shift of a negative number

cout << (-16 >> 2);

Since C++20 this is defined as an arithmetic shift and gives −4; earlier it was implementation-defined, though compilers did the same.

What's going on

Bitwise operators work on the individual binary digits of an integer. This program applies each one to a = 12 and b = 25 and prints the results in binary with bitset<8>, so you can line the bits up and check them by eye.

12 is 00001100 and 25 is 00011001.

a & b (AND) keeps a bit only where both numbers have a 1. Only bit 3 is shared, so the result is 00001000 = 8.

a | b (OR) sets a bit where either number has a 1: 00011101 = 29.

a ^ b (XOR) sets a bit where the numbers differ: 00010101 = 21.

~a (NOT) flips every bit. The catch is that a is a 32-bit int, so all 32 bits flip, not just the 8 shown. The low 8 bits are 11110011, but the full value is negative. Signed integers use two's complement, where ~x always equals −x − 1, so ~12 = −13. The program prints that: 11110011 (−13). (An older version of this page showed 243, which is what you would get only if the value were an 8-bit unsigned number.)

a << 2 shifts every bit two places left and fills with zeros: 00110000 = 48. Each left shift multiplies by 2, so this is 12 × 4.

a >> 2 shifts two places right: 00000011 = 3. Each right shift divides by 2 and drops the remainder, so 12 ÷ 4 = 3.

bitset<8>(num) shows only the lowest 8 bits. Use bitset<32> to see the whole int, including all the leading 1s of ~a.

The headers come first. <iostream> is there because it provides cout and cin for input/output operations. <bitset> is there because it provides bitset for binary representation display. Leave one out and the compiler stops at the first name it does not recognize.

Understanding Bitwise Operations

Bitwise Concept:

Operations on individual bits
Work at binary level
More efficient than arithmetic
Essential for low-level programming

vs Logical Operations:

Bitwise: works on bits
Logical: works on boolean values
Different purposes
Both important

Bitwise AND (&)

Operation:

a & b // AND operation

What happens here:

1 if both bits are 1
0 otherwise
Performed bit by bit
Example: 12 & 25 = 8

Truth Table:

0 & 0 = 0
0 & 1 = 0
1 & 0 = 0
1 & 1 = 1

Bitwise OR (|)

Operation:

a | b // OR operation

What happens here:

1 if either bit is 1
0 only if both are 0
Performed bit by bit
Example: 12 | 25 = 29

Truth Table:

0 | 0 = 0
0 | 1 = 1
1 | 0 = 1
1 | 1 = 1

Bitwise XOR (^)

Operation:

a ^ b // XOR operation

What happens here:

1 if bits are different
0 if bits are same
Performed bit by bit
Example: 12 ^ 25 = 21

Truth Table:

0 ^ 0 = 0
0 ^ 1 = 1
1 ^ 0 = 1
1 ^ 1 = 0

Bitwise NOT (~)

Operation:

~a // NOT operation

What happens here:

Inverts all bits
0 becomes 1, 1 becomes 0
One's complement
Example: ~12 = 243 (for 8-bit)

Note:

Result depends on data type size
Signed vs unsigned matters
Two's complement representation

Left Shift (<<)

Operation:

a << n // Left shift by n positions

What happens here:

Shifts bits left by n positions
Fills right with zeros
Effectively multiplies by 2^n
Example: 12 << 2 = 48 (12 × 4)

Use Cases:

Fast multiplication by powers of 2
Bit manipulation
Efficient calculations

Right Shift (>>)

Operation:

a >> n // Right shift by n positions

What happens here:

Shifts bits right by n positions
Fills left (sign bit or zero)
Effectively divides by 2^n
Example: 12 >> 2 = 3 (12 ÷ 4)

Use Cases:

Fast division by powers of 2
Bit manipulation
Efficient calculations

When to Use Bitwise Operations

Good fits:

Flags and masks
Low-level programming
Performance optimization
Bit manipulation problems
System programming

Example Scenarios:

Setting/clearing flags
Checking permissions
Efficient calculations
Cryptography
Embedded systems

Important Considerations

Data Types:

Works on integer types
Signed vs unsigned matters
Size affects results
Be careful with signed shifts

Performance:

Very fast operations
Hardware-level efficiency
Faster than arithmetic
Optimized by CPU

Readability:

Can be less readable
Document complex operations
Use for performance-critical code
Balance clarity and speed

return 0;

return 0; closes main and tells the operating system the program finished without an error.

Quiz

Tap an option. The answer stays hidden until you pick.

Question 1

What is 6 ^ 3?

Pick an answer to check it.

Question 2

What does ~12 print for an int?

Pick an answer to check it.

Questions people ask

Is & the same as &&?

No. & works on each bit; && is logical AND on true/false and short-circuits.

Why use bitwise operators?

Flags, permissions, compact sets, hashing, graphics, and fast multiply or divide by powers of 2.

How do I print the full 32 bits?

cout << bitset<32>(x);