C Program to Set nth Bit of a Number
To set the nth bit of a number in C, use the bitwise OR operator with a mask:
number |= (1 << n); where n is the bit position starting from 0.Examples
Inputnumber = 8, n = 1
Output10
Inputnumber = 0, n = 3
Output8
Inputnumber = 15, n = 4
Output31
How to Think About It
To set the nth bit of a number, think of the number in binary form. You create a mask that has only the nth bit set to 1 by shifting 1 to the left n times using
1 << n. Then, combine this mask with the original number using the bitwise OR operator |=. This ensures the nth bit is set to 1 without changing other bits.Algorithm
1
Get the input number and the bit position n.2
Create a mask by shifting 1 to the left by n positions.3
Use bitwise OR to set the nth bit of the number with the mask.4
Return or print the updated number.Code
c
#include <stdio.h> int setNthBit(int number, int n) { return number | (1 << n); } int main() { int number = 8, n = 1; int result = setNthBit(number, n); printf("Number after setting %dth bit: %d\n", n, result); return 0; }
Output
Number after setting 1th bit: 10
Dry Run
Let's trace setting the 1st bit of number 8 through the code.
1
Initial values
number = 8 (binary 1000), n = 1
2
Create mask
mask = 1 << 1 = 2 (binary 0010)
3
Set nth bit
result = number | mask = 8 | 2 = 10 (binary 1010)
| Step | Operation | Value |
|---|---|---|
| 1 | number | 8 (1000) |
| 2 | mask = 1 << n | 2 (0010) |
| 3 | result = number | mask | 10 (1010) |
Why This Works
Step 1: Create mask with nth bit set
Using 1 << n shifts 1 left by n bits, creating a mask with only the nth bit set to 1.
Step 2: Combine mask with number
The bitwise OR | sets the nth bit of the number to 1 without changing other bits.
Step 3: Return updated number
The result is the original number with the nth bit guaranteed to be set.
Alternative Approaches
Using a function with pointer to modify in place
c
#include <stdio.h> void setNthBitInPlace(int *number, int n) { *number |= (1 << n); } int main() { int num = 8; setNthBitInPlace(&num, 1); printf("Number after setting %dth bit: %d\n", 1, num); return 0; }
This modifies the original number directly using a pointer, useful when you want to change the variable without returning a new value.
Using inline macro for setting nth bit
c
#include <stdio.h> #define SET_NTH_BIT(num, n) ((num) | (1 << (n))) int main() { int number = 8; int result = SET_NTH_BIT(number, 1); printf("Number after setting %dth bit: %d\n", 1, result); return 0; }
Using a macro can make the code concise but may reduce type safety and debugging clarity.
Complexity: O(1) time, O(1) space
Time Complexity
Setting a bit uses a single bitwise operation, so it runs in constant time regardless of input size.
Space Complexity
No extra memory is needed beyond a few variables, so space complexity is constant.
Which Approach is Fastest?
All approaches use simple bitwise operations with constant time and space; using a macro or inline function may be slightly faster due to inlining.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Function returning new value | O(1) | O(1) | Clear and reusable code |
| Function modifying in place | O(1) | O(1) | When original variable must be changed |
| Macro | O(1) | O(1) | Concise code, inline expansion |
Remember bit positions start at 0 from the rightmost bit when setting bits.
Beginners often forget to use parentheses around
1 << n, causing unexpected results.