-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathques15.c
More file actions
26 lines (21 loc) · 768 Bytes
/
ques15.c
File metadata and controls
26 lines (21 loc) · 768 Bytes
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
#include <stdio.h>
#include <math.h>
/* question 15 */
int ques15(int x, int n) {
/* for ques15 only, assume n is not a negative number */
int temp = (1 << n); // Set temp to 2^n
int z = temp + ~0; // Set z to temp - 1 or 2^n-1
// if x is less than 2^n, return x, otherwise return x mod 2^n
return (z & x); //return bitwise and of z nd x
}
int ans15(int x, int n) {
return (x % (1 << n)); //Return x mod 2^n, the remainder when diving x by 2^n
}
int main () {
int x;
int n;
printf("Enter two integers: ");
scanf("%d %d", &x, &n);
printf("\nques15(%d, %d) returns the remainder when dividing %d by 2^%d: %d\n", x,n,x,n,ques15(x,n));
printf("anss15(%d, %d) returns the remainder when dividing %d by 2^%d: %d\n", x,n,x,n,ans15(x,n));
}