0
0
CProgramBeginner · 2 min read

C Program to Clear nth Bit of a Number

To clear the nth bit of a number in C, use number & ~(1 << (n - 1)), which sets the nth bit to 0 while keeping others unchanged.
📋

Examples

Inputnumber = 15, n = 2
Output13
Inputnumber = 8, n = 4
Output0
Inputnumber = 21, n = 1
Output20
🧠

How to Think About It

To clear the nth bit, think of the number in binary. We want to turn that specific bit to 0 without changing others. We create a mask with 1 at the nth bit, then invert it to have 0 at nth bit and 1s elsewhere. Using bitwise AND with this mask clears the nth bit.
📐

Algorithm

1
Get the input number and the bit position n to clear.
2
Create a mask by shifting 1 left by (n - 1) positions.
3
Invert the mask to have 0 at nth bit and 1s elsewhere.
4
Perform bitwise AND between the number and the inverted mask.
5
Return or print the result.
💻

Code

c
#include <stdio.h>

int clearNthBit(int number, int n) {
    return number & ~(1 << (n - 1));
}

int main() {
    int number = 15, n = 2;
    int result = clearNthBit(number, n);
    printf("Number after clearing %dnd bit: %d\n", n, result);
    return 0;
}
Output
Number after clearing 2nd bit: 13
🔍

Dry Run

Let's trace clearing the 2nd bit of number 15 through the code

1

Initial values

number = 15 (binary 1111), n = 2

2

Create mask

1 << (2 - 1) = 1 << 1 = 2 (binary 0010)

3

Invert mask

~2 = binary 1101 (decimal 13)

4

Bitwise AND

15 & 13 = binary 1111 & 1101 = 1101 (decimal 13)

5

Return result

Result = 13

StepOperationValue (decimal)Value (binary)
1Input number151111
2Mask (1 << (n-1))20010
3Inverted mask (~mask)131101
4Result (number & ~mask)131101
💡

Why This Works

Step 1: Create mask for nth bit

We shift 1 left by (n-1) to get a mask with only the nth bit set to 1.

Step 2: Invert mask

Using bitwise NOT (~) flips all bits, so the nth bit becomes 0 and others become 1.

Step 3: Clear nth bit

Bitwise AND with the inverted mask keeps all bits except the nth bit, which is cleared to 0.

🔄

Alternative Approaches

Using a function with zero-based bit index
c
#include <stdio.h>

int clearBitZeroBased(int number, int index) {
    return number & ~(1 << index);
}

int main() {
    int number = 15, index = 1;
    printf("Result: %d\n", clearBitZeroBased(number, index));
    return 0;
}
This method uses zero-based indexing for bits, which some programmers prefer for clarity.
Using inline macro
c
#include <stdio.h>
#define CLEAR_NTH_BIT(num, n) ((num) & ~(1 << ((n) - 1)))

int main() {
    int number = 15, n = 2;
    printf("Result: %d\n", CLEAR_NTH_BIT(number, n));
    return 0;
}
Using a macro can make code concise but may reduce type safety and debugging ease.

Complexity: O(1) time, O(1) space

Time Complexity

Clearing a bit uses a fixed number of bitwise operations, so it runs in constant time.

Space Complexity

Only a few variables are used, so space is constant.

Which Approach is Fastest?

All approaches use simple bitwise operations with constant time and space; macros may be slightly faster but less safe.

ApproachTimeSpaceBest For
Function with 1-based bit indexO(1)O(1)Clear nth bit with human-friendly indexing
Function with 0-based bit indexO(1)O(1)Programmer-friendly zero-based bit manipulation
MacroO(1)O(1)Concise code, inline expansion
💡
Remember bit positions start at 1 for this method, so subtract 1 when shifting.
⚠️
Forgetting to subtract 1 from n when shifting causes clearing the wrong bit.