summaryrefslogtreecommitdiff
path: root/1871/CH8/EX8.12/Ch08Ex12.sce
diff options
context:
space:
mode:
authorpriyanka2015-06-24 15:03:17 +0530
committerpriyanka2015-06-24 15:03:17 +0530
commitb1f5c3f8d6671b4331cef1dcebdf63b7a43a3a2b (patch)
treeab291cffc65280e58ac82470ba63fbcca7805165 /1871/CH8/EX8.12/Ch08Ex12.sce
downloadScilab-TBC-Uploads-b1f5c3f8d6671b4331cef1dcebdf63b7a43a3a2b.tar.gz
Scilab-TBC-Uploads-b1f5c3f8d6671b4331cef1dcebdf63b7a43a3a2b.tar.bz2
Scilab-TBC-Uploads-b1f5c3f8d6671b4331cef1dcebdf63b7a43a3a2b.zip
initial commit / add all books
Diffstat (limited to '1871/CH8/EX8.12/Ch08Ex12.sce')
-rwxr-xr-x1871/CH8/EX8.12/Ch08Ex12.sce71
1 files changed, 71 insertions, 0 deletions
diff --git a/1871/CH8/EX8.12/Ch08Ex12.sce b/1871/CH8/EX8.12/Ch08Ex12.sce
new file mode 100755
index 000000000..4188f6b68
--- /dev/null
+++ b/1871/CH8/EX8.12/Ch08Ex12.sce
@@ -0,0 +1,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