We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
2 parents 69ae666 + e2ebdad commit 167ec29Copy full SHA for 167ec29
algorithms/maths/twos_complement_of_binary.m
@@ -0,0 +1,35 @@
1
+%% 2's complement of binary no;
2
+
3
+function twos_comp = twos_complement_of_binary(bin)
4
+c=0;
5
+temp=bin;
6
+twos_comp=0;
7
+while(temp>0 && rem(temp,10)==0)
8
+ twos_comp=twos_comp*10 + rem(temp,10);
9
+ temp=fix(temp/10);
10
+ c=c+1;
11
+end
12
+if(temp>0)
13
14
15
16
17
+while(temp>0)
18
+ if(rem(temp,10)==1)
19
+ twos_comp=twos_comp*10 + 0;
20
+ else
21
+ twos_comp=twos_comp*10 + 1;
22
+ end
23
24
25
26
27
+temp=twos_comp;
28
29
+while(c>0) %reversing the order;
30
31
32
+ c=c-1;
33
34
35
0 commit comments