assignment-p-09.c ​
Metadata ​
- Author — Amit Dutta (amitdutta4255@gmail.com)
- Last updated — 12 Dec 2025
- License — MIT
Problem Statement ​
Problem Statement
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 ​
Printing the code
To print this file, open it on GitHub and click Raw before printing, or use the Download Raw button above and print directly from that page.
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