blob: 4cc46ef359c0539fae0b7f42d245a4ebad115c04 (
plain)
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
62
|
clc
clear
disp("Example 8.31b")
printf("\n")
disp("perform the following binary substraction using 2s complement")
disp("a)1010-0111 b)0110-1101")
a=[1 0 1 0]
b=~[0 1 1 1]
d=0
h=1
for i=1:length(b)
n(i)=b(length(b)+1-i)+h
if(n(i)==1)
h=0
end
if(n(i)==2)
h=1
n(i)=0
end
end
for i=1:length(a)
b(i)=n(i)
end
for i=1:length(a)
c(i)=a(length(a)+1-i)+b(length(a)+1-i)+d
if(c(i)==1)
d=0
end
if[c(i)==2]
d=1
c(i)=0
end
end
if(d==0)
for i=1:length(a)
c(i)=~c(i)
end
j=1
for i=1:length(b)
m(i)=c(i)+j
if(m(i)==1)
f=0
end
if(m(i)==2)
j=1
m(i)=0
end
end
for i=1:length(a)
c(i)=m(i)
end
end
for i=1:length(a)
C(i)=c(i)
end
printf("result =%d%d%d%d",c(4),c(3),c(2),c(1))
|