HomeAbout UsContact Us

C program showcasing bitwise operations

By Jithin Tom
Published in Embedded C/C++
September 21, 2022
1 min read
C program showcasing bitwise operations

Bitwise operations are fundamental to embedded C programming. When developing firmware for microcontrollers, you rarely work with standard data types like int or float in a vacuum; instead, you directly manipulate hardware registers. These registers control everything from GPIO pins (turning an LED on or off) to complex peripherals like UARTs, SPI buses, and timers. Because hardware registers are typically memory-mapped and represent individual control bits, bitwise operators are the primary tool for reading and writing these bits efficiently without affecting adjacent bits in the same register.

Understanding how to set, clear, toggle, and test individual bits using masks is a critical skill for any embedded software engineer. In many coding interviews for embedded roles, candidates are expected to demonstrate fluency in bitwise logic to prove they can safely interact with hardware at the lowest level.

The bitwise operators available in C are:

  • bitwise AND – &
  • bitwise OR – |
  • bitwise XOR – ^
  • left-shift operator – <<
  • right-shift operator – >>
  • bitwise NOT (1’s complement) – ~

This C program shows some common usages of the bitwise operators, serving as a quick reference for standard bit manipulation idioms.

#include <stdio.h>
int main(void)
{
int x = 0x1u;
// Set 2nd bit
x |= (1u << 2);
printf("%d\n", x);
// Clear 2nd bit
x &= ~(1u << 2);
printf("%d\n", x);
// Toggle 2nd bit
x ^= (1 << 2);
printf("%d\n", x);
// Test 2nd bit
x & (1u << 2) ? puts("true") : puts("false");
return 0;
}

Code walk-through

The code fragment x |= (1u << 2); sets the 2nd bit by shifting the value 1 left by 2 positions. Similarly the code fragment x &= ~(1u << 2); clears the second bit by shifting the value 1 by 2 positions to the left and doing a 1’s complement.

The line x ^= (1 << 2); toggles the 2nd bit by left shifting the value 1 by 2 positions and XORing with the value to be modified.

The code fragment  x & (1u << 2) ? puts(“true”) : puts(“false”); tests the bit at position 2 by creating a mask by shifting 1 by 2 positions to the left and use the bit-wise & operator to check if the result is not 0 or otherwise.

Frequently Asked Questions

What is the practical takeaway of using bitwise AND, OR, and XOR?

AND (&) is used to mask or clear specific bits, OR (|) is used to set specific bits, and XOR (^) is used to toggle bits. These form the foundation of controlling microcontroller peripheral registers.

How does shift operators differ between signed and unsigned integers in C?

For unsigned types, right shift (`>>`) always inserts zeros (logical shift). For signed types, right shift behavior is compiler-defined but usually preserves the sign bit (arithmetic shift), which can cause bugs if not handled properly.

Why do we use hex notation (like 0x01) instead of decimal in bitwise operations?

Hexadecimal notation aligns perfectly with binary nibbles (4 bits). For example, 0xF0 represents the top 4 bits set and bottom 4 bits cleared (11110000), making the code much easier to read and debug.

Tags

#C#C++#bitwise

Share


Previous Article
Angle between hour and minute hands of analogue clock using C
Jithin Tom

Jithin Tom

A Closer Look at C/C++, RTOS, and Embedded Systems

Related Posts

Example C code to set 5 bits starting from position 10
Example C code to set 5 bits starting from position 10
September 22, 2022
1 min
© 2026, All Rights Reserved.
Powered By Netlyft

Quick Links

Advertise with usAbout UsContact Us

Social Media