-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path15_replace_Nbits.c
45 lines (39 loc) · 1.68 KB
/
15_replace_Nbits.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/*
Name : Prabhat Kiran
Date : 14th July 2022
Description : WAP to replace 'n' bits of a given number.
Eg. - If num is 10 and n is 3 and val is 12
10 -> 0 0 0 0 1 0 1 0
12 -> 0 0 0 0 1 1 0 0
The program should print result as 12 (1 1 0 0).
Sample Input : 1) Enter the number: 10
Enter number of bits: 3
Enter the value: 12
2) Enter the number: 15
Enter number of bits: 2
Enter the value: 1
Sample Output: 1) Result = 12
2) Result = 13
*/
#include <stdio.h>
int replace_nbits (int, int, int); //Function declaration of 'get_nbits' to tell the compiler to search for the function definition later in the code.
int main()
{
int num, n, val, res = 0;
printf ("Enter the number: ");
scanf ("%d", &num);
printf ("Enter number of bits: ");
scanf ("%d", &n);
printf ("Enter the value: ");
scanf ("%d", &val);
res = replace_nbits (num, n, val); //Function call to 'replace_nbits' with the function arguments 'num', 'n' and 'value'.
printf ("Result = %d\n", res);
return 0;
}
int replace_nbits (int num, int n, int val)
{
return ((num & (~((1 << n) - 1))) | (val & ((1 << n) - 1))); //This statement evaluates the expression and returns the 'n' bits from LSB of the user entered number in 'int' type.
//'(num & (~((1 << n) - 1)))' : It clears the 'n' bits from LSB which will be replaced by the 'n' bits from the other number.
//'(val & ((1 << n) - 1)) : It gets the 'n' bits from the other number.
//Both the expressions are operated by OR to achieve the final result.
}