assignment-p-09.c
Problem Statement
assignment-p-09.c
Write a C program that includes a user-defined function named setBit with the signature int setBit(int num, int position);. The function should set the bit at the specified position (0-indexed) to 1 and return the modified number.
Source Code
c
#include <stdio.h>
int setBit(int, int);
int main()
{
int num, position;
printf("Enter the number: ");
scanf("%d", &num);
printf("Enter the postion where you want to set the bit (0-indexed): ");
scanf("%d", &position);
printf("\nModified number= %d", setBit(num, position));
return 0;
}
int setBit(int num, int position)
{
int mask = 1 << position;
return num | mask;
}1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
● Author - Amit Dutta · Updated - 12 Dec 2025