summaryrefslogtreecommitdiff
path: root/1646/CH15/EX15.10/Ch015Ex10.sce
blob: 7d997d7672de8d49134b358eb43b7b99c2409f7b (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
// Scilab code Ex15.10 :Pg:773(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 binp = bin_product(op1, op2)
    binp = decimal_binary(binary_decimal(op1)*binary_decimal(op2));   
endfunction

mul1 = 1101;    // Initialize the first binary multiplicand
mul2 = 1100;    // Initialize the second binary multiplicand
product = bin_product(mul1, mul2);

printf("%4d X %4d = %8d", mul1, mul2, product);

// Result
//  1101 X 1100 = 10011100