DEV Community

Cover image for #3 Checking the K-th Bit in C
Hrishikesh Karande
Hrishikesh Karande

Posted on

#3 Checking the K-th Bit in C

While working on this small problem on EWskill of checking whether the K-th bit of an integer is set, I learned several important lessons about C programming and firmware development. I want to walk you through my experience, step by step, as if you’re seeing it for the first time.

Understanding the Problem

The task was simple: given an integer N and a bit position K (0-based index), I had to determine if the K-th bit in the binary representation of N is 1 (set) or 0 (not set).

For example:

  • N = 8 → binary: 00001000
  • K = 3 → the 3rd bit is 1, so the output should be 1. At first glance, it seems trivial, but this problem taught me a lot about bit manipulation, efficient C coding, and embedded thinking.

The Code I wrote:

#include <stdio.h>

int isKthBitSet(int n, int k) {
    // Write your code here
    if (n & (1 << k)){
        return 1;
    } else {
        return 0;
    }
}

int main() {
    int n, k;
    scanf("%d %d", &n, &k);
    printf("%d", isKthBitSet(n, k));
    return 0;
}
Enter fullscreen mode Exit fullscreen mode

Writing the Function Step by Step

The first version I wrote looked like this:

int isKthBitSet(int n, int k) {
    if ((n & (1 << k)) == 1){
        return 1;
    } else {
        return 0;
    }
}
Enter fullscreen mode Exit fullscreen mode

Here’s what I learned while writing this:

  1. Bitwise AND (&): This operator allows me to check specific bits in a number.

  2. Left Shift (1 << k): This creates a mask where only the K-th bit is 1. For example, 1 << 3 becomes 00001000.

  3. Logical comparison mistake: I realized that comparing with == 1 doesn’t work for bits other than the least significant one. For example, (8 & 8) == 1 would fail, even though the 3rd bit is set.

This taught me to always think carefully about what the bitwise operations actually return.

Making the Function Efficient

After understanding the logic, I rewrote the function in a more compact way:

int isKthBitSet(int n, int k) {
    return (n & (1 << k)) != 0;
}
Enter fullscreen mode Exit fullscreen mode

Here, I learned:

  • In C, any non-zero value is considered true, so we don’t always need complex if-else statements.
  • Writing concise code is important in firmware because it saves memory and execution time.

Microcontroller-Style Firmware Thinking

Finally, I learned the “microcontroller mindset.” Firmware developers often write code that is minimal, fast, and hardware-friendly. The ultimate version of my function became:

int isKthBitSet(int n, int k) {
    return n & (1 << k);
}
Enter fullscreen mode Exit fullscreen mode
  • No comparisons, no extra branching—just raw bitwise logic.
  • The result is either 0 (bit not set) or some non-zero value (bit set), which works perfectly in embedded systems.

This taught me to trust how C treats non-zero values and to think in bits rather than abstract numbers—a fundamental mindset for firmware development.

My Key Learnings

  1. Bit manipulation is powerful: It lets you control hardware directly and efficiently.

  2. C is flexible but requires precision: Tiny mistakes, like == 1 versus != 0, can break logic.

  3. Write for efficiency in firmware: Avoid unnecessary branching or comparisons.

  4. Think in bits, not just integers: Embedded systems often deal with registers and flags where each bit matters.

Top comments (2)

Collapse
 
pauljlucas profile image
Paul J. Lucas

You really should be using unsigned or uint64_t, not a signed type like int.

Collapse
 
hrishikesh_karande profile image
Hrishikesh Karande • Edited

Thank you for reading my blog good sir. Well yes, you are correct. The thing is, I am solving exercises on EWskills to get better at firmware development using C, and documenting the learnings and solutions here. The declarations were like that i.e. int n, k; in the code snippet that I had to solve (I only had to write the function isKthBitSet() in this case). But yes, your observation makes me help see things with even more depth. Thanks again for reading and commenting (I did not expect any one to read my blog!). This inspires me to keep going.