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 is1
, so the output should be1
. 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:
Bitwise AND (
&
): This operator allows me to check specific bits in a number.Left Shift (
1 << k
): This creates a mask where only the K-th bit is1
. For example,1 << 3
becomes00001000
.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 complexif-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
Bit manipulation is powerful: It lets you control hardware directly and efficiently.
C is flexible but requires precision: Tiny mistakes, like
== 1
versus!= 0
, can break logic.Write for efficiency in firmware: Avoid unnecessary branching or comparisons.
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

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/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.