-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinary.c
61 lines (46 loc) · 1.11 KB
/
Binary.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// Name Ankesh Sharma
// Date 03-01-2024
// Language C
#include <stdio.h>
#include <stdio.h>
#include <conio.h>
int binary(int num);
int main() {
int num = -12;
binary(num);
return 0;
}
int binary(int num) {
if (num == 0) {
printf("Binary: 0\n");
return 0;
}
// Step 1: Find binary representation of the absolute value
int absNum = (num < 0) ? -num : num;
int binary[32]; // Assuming a 32-bit integer
int index = 0;
while (absNum > 0) {
binary[index++] = absNum % 2;
absNum /= 2;
}
// Step 2: Apply two's complement for negative numbers
if (num < 0) {
// Flip the bits
for (int i = 0; i < index; i++) {
binary[i] = (binary[i] == 0) ? 1 : 0;
}
// Add 1 to the result
int carry = 1;
for (int i = 0; i < index; i++) {
int sum = binary[i] + carry;
binary[i] = sum % 2;
carry = sum / 2;
}
}
printf("Binary: ");
for (int i = index - 1; i >= 0; i--) {
printf("%d", binary[i]);
}
printf("\n");
return 0;
}