Skip to content

Commit b486a78

Browse files
2s complement
1 parent c17d990 commit b486a78

5 files changed

+59
-28
lines changed
+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
int finComplement(int arr[], int n)
5+
{
6+
int m = n + 1;
7+
int twoCom[m] = {0};
8+
9+
for (int i = n - 1, k = m - 1; i > 0; i--, k--)
10+
{
11+
twoCom[k] = arr[i] == 1 ? 0 : 1;
12+
}
13+
cout << " 1s` Comp : " << " ";
14+
for (int i = 0; i < m; i++)
15+
{
16+
cout << twoCom[i] << " ";
17+
}
18+
cout << endl;
19+
20+
int carry = 1;
21+
for (int i = m - 1; i >= 0; i--)
22+
{
23+
int sum = twoCom[i] + carry;
24+
twoCom[i] = sum % 2;
25+
carry = sum / 10;
26+
}
27+
cout<<" 2nd` comp : ";
28+
for (int i = 0; i < n + 1; i++)
29+
{
30+
cout <<twoCom[i] << " ";
31+
}
32+
33+
// return twoCom;
34+
}
35+
36+
int main()
37+
{
38+
int arr[] = {1, 0, 0, 1, 0, 0, 1,1};
39+
int n = 8;
40+
cout << "normal arr : " << " ";
41+
for (int i = 0; i < n; i++)
42+
{
43+
cout << arr[i] << " ";
44+
}
45+
cout << endl;
46+
int ans = finComplement(arr, n);
47+
48+
// cout << "twos complement : " << " ";
49+
// for (int i = 0; i <n+1; i++)
50+
// {
51+
// cout << arr[i] << " ";
52+
// }
53+
}
44.6 KB
Binary file not shown.
+5-28
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,5 @@
1-
#include<iostream>
2-
using namespace std;
3-
4-
int main(){
5-
// cout<<"Hello";
6-
int j=-1;
7-
int n=9;
8-
int arr[]={1,1,0,0,1,0,1,0,1};
9-
10-
for(int i=0;i<n;i++){
11-
if(arr[i]==0){
12-
j=i;
13-
break;
14-
}
15-
}
16-
17-
for(int i=j+1;j<n;j++){
18-
if(arr[i]!=0){
19-
swap(arr[i],arr[j]);
20-
j++;
21-
}
22-
}
23-
24-
for(int i=0;i<n;i++){
25-
cout<<arr[i]<<" ";
26-
}
27-
return 1;
28-
}
1+
// cout << "first Comp : " << " ";
2+
// for (int i = (n + 1); i > 0; i--)
3+
// {
4+
// cout << twoCom[i] << " ";
5+
// }
-44 KB
Binary file not shown.

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# Learning Data Structure And Algorithmns From Zero T0 Advanced
22

33

4+
45

0 commit comments

Comments
 (0)