Siksha Sarovar

Siksha Sarovar (sikshasarovar.com) is a free educational web application that helps students in India learn programming and prepare for academic and competitive exams. The platform offers structured coding courses (C, C++, Python, Java, HTML, CSS, PHP, Power BI, AI, Machine Learning, Data Science), complete university curriculum notes for BCA/MCA students with previous year question papers, Class 10 and Class 12 CBSE/HBSE school notes, and dedicated preparation material for SSC, UPSC, Banking, Railway and other government exams. Browsing the site is completely free and requires no account. Users may optionally sign in with Google solely to save their learning progress, quiz scores and personal preferences across devices.

Privacy Policy | Terms of Service | Contact Siksha Sarovar | About Siksha Sarovar

v4.0.9 · PWA
Siksha Sarovar logo
Siksha Sarovar
Your Learning Universe

Siksha Sarovar is a free e-learning platform for coding courses, BCA university notes and competitive exam preparation. Optional Google sign-in saves your learning progress across devices.

Initializing knowledge base…
Compiling modules 0%

Bit Manipulation: Advanced C Programming

Lesson 30 of 53 in the free Foundation of C & C++ notes on Siksha Sarovar, written by Rohit Jangra.

Thinking Like the CPU

At its most fundamental level, a computer doesn't understand "10" or "Hello." It only understands high and low voltages, represented as 1 and 0. Bit manipulation allows you to work directly with these individual bits.

The Bitwise Operators

  • & (AND): Result is 1 only if both bits are 1. (Used for "masking" or checking a specific bit).
  • | (OR): Result is 1 if at least one bit is 1. (Used for setting a specific bit to 1).
  • ^ (XOR): Result is 1 if the bits are different. (Commonly used for toggling bits or simple encryption).
  • ~ (NOT): Flips all bits. 1 becomes 0, 0 becomes 1.
  • << (Left Shift): Moves bits to the left. Shifting left by 1 is the same as multiplying by 2.
  • >> (Right Shift): Moves bits to the right. Shifting right by 1 is the same as dividing by 2.

Common Professional Tricks

  1. Is it Even or Odd? (n & 1). If result is 0, it's even. If 1, it's odd. This is much faster than n % 2.
  2. Swapping without a Temp variable:
    a = a ^ b;
    b = a ^ b;
    a = a ^ b;
  1. Checking the Nth Bit: if (num & (1 << n)).
  2. Setting the Nth Bit: num = num | (1 << n).

Why use Bitwise Ops?

  • Blazing Speed: These are the fastest instructions a CPU can execute.
  • Saving Memory: You can pack 8 different "True/False" flags into a single char (1 byte) instead of using 8 different int variables (32 bytes).
  • Embedded Systems: Turning a specific hardware pin on or off usually requires setting one specific bit in a "control register."
Bitwise XOR (^) is its own inverse. If you XOR a number with a "key," you get a scrambled result. XOR it with the same key again, and you get the original number back! This is the basis of simple data obfuscation.