Skip to content

Commit 80605cf

Browse files
committed
starting with the structure lecture
0 parents commit 80605cf

33 files changed

+2294
-0
lines changed

Binary.c

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#include <stdio.h>
2+
3+
#include <stdio.h>
4+
#include <conio.h>
5+
6+
7+
8+
int binary(int num);
9+
10+
int main() {
11+
int num = -12;
12+
binary(num);
13+
14+
return 0;
15+
}
16+
17+
18+
int binary(int num) {
19+
if (num == 0) {
20+
printf("Binary: 0\n");
21+
return 0;
22+
}
23+
24+
// Step 1: Find binary representation of the absolute value
25+
int absNum = (num < 0) ? -num : num;
26+
int binary[32]; // Assuming a 32-bit integer
27+
int index = 0;
28+
29+
while (absNum > 0) {
30+
binary[index++] = absNum % 2;
31+
absNum /= 2;
32+
}
33+
34+
// Step 2: Apply two's complement for negative numbers
35+
if (num < 0) {
36+
// Flip the bits
37+
for (int i = 0; i < index; i++) {
38+
binary[i] = (binary[i] == 0) ? 1 : 0;
39+
}
40+
41+
// Add 1 to the result
42+
int carry = 1;
43+
for (int i = 0; i < index; i++) {
44+
int sum = binary[i] + carry;
45+
binary[i] = sum % 2;
46+
carry = sum / 2;
47+
}
48+
}
49+
printf("Binary: ");
50+
for (int i = index - 1; i >= 0; i--) {
51+
printf("%d", binary[i]);
52+
}
53+
printf("\n");
54+
55+
return 0;
56+
}

Binary.exe

230 KB
Binary file not shown.

BitWiseOperators.c

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#include <stdio.h>
2+
#include <conio.h>
3+
// int binarydigit();
4+
// int bitwiseand();
5+
// int bitwiseor();
6+
// int bitwisexor();
7+
// int bitwiseleftshift();
8+
int bitwiserightshift();
9+
int main()
10+
{
11+
// binarydigit();
12+
// bitwiseand();
13+
// bitwiseor();
14+
// bitwisexor();
15+
// bitwiseleftshift();
16+
bitwiserightshift();
17+
printf("HERE WE GO BROOOOOOO!!!!!!!!!");
18+
19+
}
20+
21+
int binarydigit()
22+
{
23+
int a = 10000;
24+
int ch = 23;
25+
int dh;
26+
dh = ~ch;
27+
printf("~ch = %d\n", dh);
28+
printf("~ch = %d\n", dh);
29+
printf("~ch = %d\n", dh);
30+
return 0;
31+
}
32+
33+
34+
35+
int bitwiseand(){
36+
int a = 10;
37+
int b = 6;
38+
int c= 8;
39+
printf("%d",a&b&c);
40+
}
41+
42+
int bitwiseor(){
43+
int a = 10;
44+
int b = 6;
45+
printf("%d",a|b);
46+
}
47+
48+
int bitwisexor(){
49+
int a = 10;
50+
int b = 6;
51+
int c= 8;
52+
printf("%d",a^b^c);
53+
}
54+
55+
int bitwiseleftshift(){
56+
int a = 16;
57+
int shift_integer = 4;
58+
int d = a<<shift_integer;
59+
printf("%d",d);
60+
}
61+
62+
int bitwiserightshift(){
63+
int a = -12;
64+
int shift_integer = 4;
65+
int d = a<<shift_integer;
66+
printf("%d%d",d,a);
67+
}

BitWiseOperators.exe

230 KB
Binary file not shown.

0 commit comments

Comments
 (0)