#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…


This content originally appeared on DEV Community and was authored by Hrishikesh Karande

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;
}

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;
    }
}

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;
}

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);
}
  • 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.


This content originally appeared on DEV Community and was authored by Hrishikesh Karande


Print Share Comment Cite Upload Translate Updates
APA

Hrishikesh Karande | Sciencx (2025-09-14T13:39:21+00:00) #3 Checking the K-th Bit in C. Retrieved from https://www.scien.cx/2025/09/14/3-checking-the-k-th-bit-in-c/

MLA
" » #3 Checking the K-th Bit in C." Hrishikesh Karande | Sciencx - Sunday September 14, 2025, https://www.scien.cx/2025/09/14/3-checking-the-k-th-bit-in-c/
HARVARD
Hrishikesh Karande | Sciencx Sunday September 14, 2025 » #3 Checking the K-th Bit in C., viewed ,<https://www.scien.cx/2025/09/14/3-checking-the-k-th-bit-in-c/>
VANCOUVER
Hrishikesh Karande | Sciencx - » #3 Checking the K-th Bit in C. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/09/14/3-checking-the-k-th-bit-in-c/
CHICAGO
" » #3 Checking the K-th Bit in C." Hrishikesh Karande | Sciencx - Accessed . https://www.scien.cx/2025/09/14/3-checking-the-k-th-bit-in-c/
IEEE
" » #3 Checking the K-th Bit in C." Hrishikesh Karande | Sciencx [Online]. Available: https://www.scien.cx/2025/09/14/3-checking-the-k-th-bit-in-c/. [Accessed: ]
rf:citation
» #3 Checking the K-th Bit in C | Hrishikesh Karande | Sciencx | https://www.scien.cx/2025/09/14/3-checking-the-k-th-bit-in-c/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.