DEV Community

Cover image for #2 Toggling Bits in C
Hrishikesh Karande
Hrishikesh Karande

Posted on

#2 Toggling Bits in C

When I started learning firmware development, I quickly realized how important bit manipulation is. Whether it’s configuring registers in a microcontroller, controlling hardware pins, or managing flags, bits are everywhere. Recently, I solved a simple but eye-opening problem on EWskill: toggle the 5th bit of a given integer.

This problem not only helped me practice C programming but also gave me insights into how firmware engineers think when working close to the hardware. Let me walk you through what I learned.

Understanding the Problem

The task was straightforward:

  • Take an integer Nas input.
  • Toggle (flip) the 5th bit (0-based index) of N.
  • Print the result.

For example:

  • Input: 10 (binary: 00001010) → After toggling the 5th bit → 00101010 → Output: 42.
  • Input: 0 (binary: 00000000) → After toggling the 5th bit → 00100000 → Output: 32.

My Code

Here’s the C program I wrote:

#include <stdio.h>

int toggleFifthBit(int n) {
    n ^= (1 << 5);   // XOR with 1 shifted left by 5
    return n;
}

int main() {
    int n;
    scanf("%d", &n); // Read integer input
    printf("%d", toggleFifthBit(n));
    return 0;
}
Enter fullscreen mode Exit fullscreen mode

Walking Through the Code

1. Including the Standard I/O Library

#include <stdio.h>
Enter fullscreen mode Exit fullscreen mode

This gives us access to functions like scanf (for input) and printf (for output). In embedded systems, you may not always have these functions, but for practice on a PC, they’re perfect.

2. Bit Manipulation with XOR

n ^= (1 << 5);
Enter fullscreen mode Exit fullscreen mode

This is the heart of the program. Let’s break it down:

  • (1 << 5) means take the binary 1 and shift it left by 5 positions. That gives 00100000 in binary (which is decimal 32).
  • ^= is the XOR assignment operator. It flips the bit at the position where the mask has 1.

    If the 5th bit was 0, it becomes 1.
    If the 5th bit was 1, it becomes 0.

This single line of code shows how powerful bitwise operations are in C.

3. Input and Output

scanf("%d", &n);
printf("%d", toggleFifthBit(n));
Enter fullscreen mode Exit fullscreen mode

Here, %d tells scanf and printf that we are working with integers.

scanf takes input from the user.

&n means “store the input at the memory address of n.” This was a new concept for me — in C, when we pass variables to functions like scanf, we often need to give their address so the function can modify them.

printf displays the final result.

My Learnings

  1. Bitwise operations are essential in firmware – Toggling bits is a common task when dealing with hardware registers. I now understand how to flip a single bit without disturbing the others.

  2. The power of XOR (^) – I learned that XOR is the perfect tool for toggling.

    • OR (|) sets a bit.
    • AND (&) clears a bit
    • XOR (^) flips a bit.

Knowing when to use which operator is critical for writing efficient firmware.

  1. Shifting (<<) is like positioning switches – Using (1 << position) creates a bitmask. This helps in targeting a specific bit in an integer.

  2. C syntax and operators start making sense – I had always seen operators like ^, &, and | but never fully understood their purpose. This exercise gave me a practical use case.

  3. Thinking like a firmware engineer – Even though this was a simple problem, it trained me to think in terms of binary, registers, and masks, which is exactly how embedded systems work.

Conclusion

This exercise taught me that even a small C program can unlock big lessons in firmware development. Now, when I see a microcontroller register with 32 bits, I know how to target and manipulate a single bit efficiently.

For beginners in C and embedded systems, I’d say: learn bit manipulation early. It’s one of the most valuable skills you’ll carry forward into real hardware projects.

Top comments (0)