summaryrefslogtreecommitdiff
path: root/1871/CH8/EX8.12/Ch08Ex12.sce
blob: 4188f6b68f0c047dcb0468c6314451d518d561c7 (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
63
64
65
66
67
68
69
70
71
// Scilab code Ex8.12 : Pg:336(2008)
clc;clear;
function [bini]= decimal_binary(ni) // Function to convert decimal to binary
    bini = 0;
    i = 1;
    while (ni <> 0)
      rem = ni-fix(ni./2).*2; 
      ni = int(ni/2);
      bini = bini + rem*i;
      i = i * 10;
    end
endfunction   

function [deci]= binary_decimal(ni) // Function to convert binary to decimal
    deci = 0;
    i = 0;
    while (ni <> 0)
      rem = ni-fix(ni./10).*10; 
      ni = int(ni/10);
      deci = deci + rem*2.^i;
      i = i + 1;
    end
endfunction  

// Function to convert a vector with binary elements to a binary number
function vtob = vector_to_bin(vector)   
    cnt = 1; vtob = 0;
for i = 1:1:length(vector)
    vtob = vtob + vector(i)*cnt;
    cnt = cnt*10;
end                  
endfunction 

function bcmp_plus_one = twos_cmp(bin)       // Function to perform twos complement
    binc = zeros(4);
    i = 1;
    while(i <= 4)
        rem = bin-fix(bin./10).*10; 
        if rem == 1 then
            rem = 0;
        else
           rem = 1;      
        end
         bin = int(bin/10);
         binc(i)=rem;
         i = i+1;
    end 
bcmp_plus_one = vector_to_bin(binc);
    bcmp_plus_one = binary_decimal(bcmp_plus_one)+1;
endfunction

function fr = refine_result(res)     // Function to refine the resut
    binc = zeros(4);
    i = 1;
    while(i <= 4)
        rem = res-fix(res./10).*10; 
        res = int(res/10);
        binc(i)=rem;
        i = i+1;
    end 
fr = vector_to_bin(binc);
endfunction

sub = 1101;    // Initialize the first binary number
men = 1010;    // Initialize the second binary number
result = decimal_binary(binary_decimal(sub)+binary_decimal(twos_cmp(men)));
final_result = refine_result(result);
printf("%4d - %4d = 00%2d", sub, men, final_result);

// Result
//   1101 - 1010 = 0011