-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
32 lines (24 loc) · 672 Bytes
/
main.c
File metadata and controls
32 lines (24 loc) · 672 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
27
28
29
30
31
32
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
void printB(int n) {
for (int i = sizeof(int) * 8 - 1; i >= 0; i--) {
printf("%d", (n >> i) & 1);
}
printf("\n");
}
int main() {
int a, b;
printf("a and b: ");
scanf("%d %d", &a, &b);
int bitsOfB = b == 0 ? 0 : log2(abs(b)) + 1;
a = (unsigned int)((abs(a) << (bitsOfB + 2)) |
(a > 0 ? (b > 0 ? 0 : 1) : (b > 0 ? 2 : 3)));
b = (unsigned int)(a | (abs(b) << 2));
int ones = pow(2, bitsOfB + 2) - 1;
a = (b & ones) >> 2;
a = (b & 1) == 0 ? a : -a;
b = (b & 2) < 1 ? b >> (bitsOfB + 2) : -(b >> (bitsOfB + 2));
printf("a = %d, b = %d\n", a, b);
return 0;
}