diff options
Diffstat (limited to '1871/CH8')
-rwxr-xr-x | 1871/CH8/EX8.1/Ch08Ex1.sce | 18 | ||||
-rwxr-xr-x | 1871/CH8/EX8.10/Ch08Ex10.sce | 80 | ||||
-rwxr-xr-x | 1871/CH8/EX8.11/Ch08Ex11.sce | 80 | ||||
-rwxr-xr-x | 1871/CH8/EX8.12/Ch08Ex12.sce | 71 | ||||
-rwxr-xr-x | 1871/CH8/EX8.13/Ch08Ex13.sce | 85 | ||||
-rwxr-xr-x | 1871/CH8/EX8.14/Ch08Ex14.sce | 36 | ||||
-rwxr-xr-x | 1871/CH8/EX8.15/Ch08Ex15.sce | 36 | ||||
-rwxr-xr-x | 1871/CH8/EX8.16/Ch08Ex16.sce | 36 | ||||
-rwxr-xr-x | 1871/CH8/EX8.17/Ch08Ex17.sce | 36 | ||||
-rwxr-xr-x | 1871/CH8/EX8.18/Ch08Ex18.sce | 50 | ||||
-rwxr-xr-x | 1871/CH8/EX8.19/Ch08Ex19.sce | 64 | ||||
-rwxr-xr-x | 1871/CH8/EX8.2/Ch08Ex2.sce | 19 | ||||
-rwxr-xr-x | 1871/CH8/EX8.20/Ch08Ex20.sce | 11 | ||||
-rwxr-xr-x | 1871/CH8/EX8.21/Ch08Ex21.sce | 25 | ||||
-rwxr-xr-x | 1871/CH8/EX8.3/Ch08Ex3.sce | 32 | ||||
-rwxr-xr-x | 1871/CH8/EX8.4/Ch08Ex4.sce | 31 | ||||
-rwxr-xr-x | 1871/CH8/EX8.5/Ch08Ex5.sce | 31 | ||||
-rwxr-xr-x | 1871/CH8/EX8.6/Ch08Ex6.sce | 31 | ||||
-rwxr-xr-x | 1871/CH8/EX8.7/Ch08Ex7.sce | 31 | ||||
-rwxr-xr-x | 1871/CH8/EX8.8/Ch08Ex8.sce | 31 | ||||
-rwxr-xr-x | 1871/CH8/EX8.9/Ch08Ex9.sce | 31 |
21 files changed, 865 insertions, 0 deletions
diff --git a/1871/CH8/EX8.1/Ch08Ex1.sce b/1871/CH8/EX8.1/Ch08Ex1.sce new file mode 100755 index 000000000..6313e7f4c --- /dev/null +++ b/1871/CH8/EX8.1/Ch08Ex1.sce @@ -0,0 +1,18 @@ +// Scilab code Ex8.1 : Pg:327(2008)
+clc;clear;
+function [dec]= binary_decimal(n) // Function to convert binary to decimal
+ dec = 0;
+ i = 0;
+ while (n <> 0)
+ rem = n-fix(n./10).*10;
+ n = int(n/10);
+ dec = dec + rem*2.^i;
+ i = i + 1;
+ end
+endfunction
+
+num = 11001; // Initialize the binary number
+printf("%d in binary = %d in decimal", num, binary_decimal(num));
+
+// Result
+// 11001 in binary = 25 in decimal
diff --git a/1871/CH8/EX8.10/Ch08Ex10.sce b/1871/CH8/EX8.10/Ch08Ex10.sce new file mode 100755 index 000000000..d2b16d10e --- /dev/null +++ b/1871/CH8/EX8.10/Ch08Ex10.sce @@ -0,0 +1,80 @@ +// Scilab code Ex8.10 : Pg:335(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 bin_cmp = ones_cmp(bin) // Function to perform ones complement
+ binc = zeros(5);
+ i = 1;
+ while(i <= 5)
+ 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
+bin_cmp = vector_to_bin(binc);
+endfunction
+
+function plus_one_res = twos_cmp(r) // Function to perform twos complement
+ onec = zeros(5);
+ i = 1;
+ while(i <= 5)
+ rem = r-fix(r./10).*10;
+ r = int(r/10);
+ onec(i)=rem;
+ i = i+1;
+ end
+plus_one_res = vector_to_bin(onec);
+ plus_one_res = binary_decimal(plus_one_res)+1;
+endfunction
+
+function fr = check_result(res) // Function to check the occurence of end-around carry
+ max_result = 11111;
+ if binary_decimal(res) > binary_decimal(max_result) then
+ fr = decimal_binary(twos_cmp(res));
+ else
+ fr = ones_cmp(res);
+ end
+endfunction
+
+sub = 11011; // Initialize the first binary number
+men = 01101; // Initialize the second binary number
+result = decimal_binary(binary_decimal(sub)+binary_decimal(ones_cmp(men)));
+final_result = check_result(result);
+printf("%5d - 0%4d = 0%4d", sub, men, final_result);
+
+// Result
+// 11011 - 01101 = 01110
diff --git a/1871/CH8/EX8.11/Ch08Ex11.sce b/1871/CH8/EX8.11/Ch08Ex11.sce new file mode 100755 index 000000000..941bdaddf --- /dev/null +++ b/1871/CH8/EX8.11/Ch08Ex11.sce @@ -0,0 +1,80 @@ +// Scilab code Ex8.11 : 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 bin_cmp = ones_cmp(bin) // Function to perform ones complement
+ binc = zeros(5);
+ i = 1;
+ while(i <= 5)
+ 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
+bin_cmp = vector_to_bin(binc);
+endfunction
+
+function plus_one_res = twos_cmp(r) // Function to perform twos complement
+ onec = zeros(5);
+ i = 1;
+ while(i <= 5)
+ rem = r-fix(r./10).*10;
+ r = int(r/10);
+ onec(i)=rem;
+ i = i+1;
+ end
+plus_one_res = vector_to_bin(binc);
+ plus_one_res = binary_decimal(plus_one_res)+1;
+endfunction
+
+function fr = check_result(res) // Function to check the occurence of end-around carry
+ max_result = 11111;
+ if binary_decimal(res) > binary_decimal(max_result) then
+ fr = decimal_binary(twos_cmp(res));
+ else
+ fr = ones_cmp(res);
+ end
+endfunction
+
+sub = 01101; // Initialize the first binary number
+men = 11011; // Initialize the second binary number
+result = decimal_binary(binary_decimal(sub)+binary_decimal(ones_cmp(men)));
+final_result = check_result(result);
+printf("0%4d - %5d = -0%4d", sub, men, final_result);
+
+// Result
+// 01101 - 11011 = -01110
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
diff --git a/1871/CH8/EX8.13/Ch08Ex13.sce b/1871/CH8/EX8.13/Ch08Ex13.sce new file mode 100755 index 000000000..2d23bf4ac --- /dev/null +++ b/1871/CH8/EX8.13/Ch08Ex13.sce @@ -0,0 +1,85 @@ +// Scilab code Ex8.13 : 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 bin_cmp = ones_cmp(bin) // Function to perform ones 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
+bin_cmp = vector_to_bin(binc);
+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 = check_result(res) // Function to check the occurence of end-around carry
+ max_result = 11111;
+ if binary_decimal(res) < binary_decimal(max_result) then
+ fr = ones_cmp(res-1);
+ else
+ fr = res;
+ end
+endfunction
+
+sub = 1010; // Initialize the first binary number
+men = 1101; // Initialize the second binary number
+result = decimal_binary(binary_decimal(sub)+binary_decimal(twos_cmp(men)));
+final_result = check_result(result);
+printf("%4d - %4d = -00%2d", sub, men, final_result);
+
+// Result
+// 1010 - 1101 = -0011
diff --git a/1871/CH8/EX8.14/Ch08Ex14.sce b/1871/CH8/EX8.14/Ch08Ex14.sce new file mode 100755 index 000000000..32c59b7a6 --- /dev/null +++ b/1871/CH8/EX8.14/Ch08Ex14.sce @@ -0,0 +1,36 @@ +// Scilab code Ex8.14 : Pg:337(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 = 111; // Initialize the first binary multiplicand
+mul2 = 101; // Initialize the second binary multiplicand
+product = bin_product(mul1, mul2);
+
+printf("%3d X %3d = %6d", mul1, mul2, product);
+
+// Result
+// 111 X 101 = 100011
diff --git a/1871/CH8/EX8.15/Ch08Ex15.sce b/1871/CH8/EX8.15/Ch08Ex15.sce new file mode 100755 index 000000000..2ee0308d5 --- /dev/null +++ b/1871/CH8/EX8.15/Ch08Ex15.sce @@ -0,0 +1,36 @@ +// Scilab code Ex8.15 : Pg:337(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
diff --git a/1871/CH8/EX8.16/Ch08Ex16.sce b/1871/CH8/EX8.16/Ch08Ex16.sce new file mode 100755 index 000000000..d7116e599 --- /dev/null +++ b/1871/CH8/EX8.16/Ch08Ex16.sce @@ -0,0 +1,36 @@ +// Scilab code Ex8.16 : Pg:337(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 = 1111 ; // Initialize the first binary multiplicand
+mul2 = 0111; // Initialize the second binary multiplicand
+product = bin_product(mul1, mul2);
+
+printf("%4d X 0%3d = %7d", mul1, mul2, product);
+
+// Result
+// 1111 X 0111 = 1101001
diff --git a/1871/CH8/EX8.17/Ch08Ex17.sce b/1871/CH8/EX8.17/Ch08Ex17.sce new file mode 100755 index 000000000..9a845bc7b --- /dev/null +++ b/1871/CH8/EX8.17/Ch08Ex17.sce @@ -0,0 +1,36 @@ +// Scilab code Ex8.17 : Pg:338(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_division(op1, op2)
+ binp = decimal_binary(binary_decimal(op1)/binary_decimal(op2));
+endfunction
+
+dividend = 11001 ; // Initialize the first binary multiplicand
+divisor = 101; // Initialize the second binary multiplicand
+product = bin_division(dividend, divisor);
+
+printf("%5d divided by %3d gives %3d", dividend, divisor, product);
+
+// Result
+// 11001 divided by 101 gives 101
diff --git a/1871/CH8/EX8.18/Ch08Ex18.sce b/1871/CH8/EX8.18/Ch08Ex18.sce new file mode 100755 index 000000000..4b44c3f27 --- /dev/null +++ b/1871/CH8/EX8.18/Ch08Ex18.sce @@ -0,0 +1,50 @@ +// Scilab code Ex8.18 : Pg:339(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 [binf]= decifrac_binfrac(nf) // Function to convert binary fraction to decimal fraction
+ binf = 0; i = 0.1;
+ while (nf <> 0)
+ nf = nf*2;
+ rem = int(nf);
+ nf = nf-rem;
+ binf = binf + 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_division(op1, op2)
+int_Q = int(binary_decimal(op1)/binary_decimal(op2));
+frac_Q = binary_decimal(op1)/binary_decimal(op2) - int_Q;
+ binp = decimal_binary(int_Q)+decifrac_binfrac(frac_Q);
+endfunction
+
+dividend = 11011 ; // Initialize the first binary multiplicand
+divisor = 100; // Initialize the second binary multiplicand
+
+product = bin_division(dividend, divisor);
+
+printf("%5d divided by %3d gives %6.2f", dividend, divisor, product);
+
+// Result
+// 11011 divided by 100 gives 110.11
diff --git a/1871/CH8/EX8.19/Ch08Ex19.sce b/1871/CH8/EX8.19/Ch08Ex19.sce new file mode 100755 index 000000000..6d455a1d1 --- /dev/null +++ b/1871/CH8/EX8.19/Ch08Ex19.sce @@ -0,0 +1,64 @@ +// Scilab code Ex8.19 : Pg:346(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 octal = decimal_octal(n) // Function to convert decimal to octal
+ i=1; octal = 0;
+ while (n<>0)
+ rem = n-fix(n./8).*8;
+ octal = octal + rem*i;
+ n = int(n/8);
+ i = i*10;
+ end
+endfunction
+
+function hex = decimal_hex(n) // Function to convert decimal to hexadecimal
+ hex = emptystr();
+ while (n <>0)
+ rem = n-fix(n./16).*16;
+ if rem == 10 then
+ hex(i)=hex+'A';
+ elseif rem == 11 then
+ hex=hex+'B';
+ elseif rem == 12 then
+ hex=hex+'C';
+ elseif rem == 13 then
+ hex=hex+'D';
+ elseif rem == 14 then
+ hex=hex+'E';
+ elseif rem == 15 then
+ hex=hex+'F';
+ else
+ hex=hex+string(rem);
+ end
+ n = int(n/16);
+ end
+ hex = strrev(hex); // Reverse string
+endfunction
+
+n = [32, 256, 51]; // Initialize a vector to the given decimals
+printf("\n__________________________________________________");
+printf("\nDecimal Binary Octal Hexadecimal ");
+printf("\n__________________________________________________");
+for i = 1:1:3
+printf("\n%d %10d %5d %4s", n(i), decimal_binary(n(i)), decimal_octal(n(i)), decimal_hex(n(i)));
+end
+printf("\n__________________________________________________");
+
+// Result
+// __________________________________________________
+// Decimal Binary Octal Hexadecimal
+// __________________________________________________
+// 32 100000 40 20
+// 256 100000000 400 100
+// 51 110011 63 33
+// __________________________________________________
diff --git a/1871/CH8/EX8.2/Ch08Ex2.sce b/1871/CH8/EX8.2/Ch08Ex2.sce new file mode 100755 index 000000000..7fe068b7b --- /dev/null +++ b/1871/CH8/EX8.2/Ch08Ex2.sce @@ -0,0 +1,19 @@ +// Scilab code Ex8.2 : Pg:328(2008)
+clc;clear;
+function [dec]= binfrac_decifrac(n) // Function to convert binary fraction to decimal fraction
+ dec = 0;
+ i = -1;
+ while (i >= -3)
+ n = n*10;
+ rem = round(n);
+ n = n-rem;
+ dec = dec + rem*2.^i;
+ i = i - 1;
+ end
+endfunction
+
+n = 0.101; // Initialize the binary number
+printf("Binary fraction %5.3f = Decimal frac = %5.3f", n, binfrac_decifrac(n));
+
+// Result
+// Binary fraction 0.101 = Decimal frac = 0.625
diff --git a/1871/CH8/EX8.20/Ch08Ex20.sce b/1871/CH8/EX8.20/Ch08Ex20.sce new file mode 100755 index 000000000..3b359f4e9 --- /dev/null +++ b/1871/CH8/EX8.20/Ch08Ex20.sce @@ -0,0 +1,11 @@ +// Scilab code Ex8.20 : Pg:346(2008)
+clc;clear;
+n1 = '11010', n2 = 'AB60', n3 = "777";
+printf("\nThe %s of binary = %d of decimal", n1, bin2dec(n1)); // Convert from binary to decimal
+printf("\nThe %s of hex = %d of decimal", n2, hex2dec(n2)); // Convert from hex to decimal
+printf("\nThe %s of octal = %d of decimal", n3, oct2dec(n3)); // Convert from octal to decimal
+
+// Result
+// The 11010 of binary = 26 of decimal
+// The AB60 of hex = 43872 of decimal
+// The 777 of octal = 511 of decimal
diff --git a/1871/CH8/EX8.21/Ch08Ex21.sce b/1871/CH8/EX8.21/Ch08Ex21.sce new file mode 100755 index 000000000..28b4d9872 --- /dev/null +++ b/1871/CH8/EX8.21/Ch08Ex21.sce @@ -0,0 +1,25 @@ +// Scilab code Ex8.21 : Pg:347(2008)
+clc;clear;
+bin = ['10001100', '00101110', '01011111', '01111011', '00111010', '10010101', '10110110', '01011011'];
+printf("\n___________________________________");
+printf("\nBinary Octal Hexadecimal");
+printf("\n___________________________________");
+for i=1:1:8
+printf("\n%8s %4s %4s", bin(i), dec2oct(bin2dec(bin(i))), dec2hex(bin2dec(bin(i))));
+end
+printf("\n___________________________________");
+
+
+// Result
+// ___________________________________
+// Binary Octal Hexadecimal
+// ___________________________________
+// 10001100 214 8C
+// 00101110 56 2E
+// 01011111 137 5F
+// 01111011 173 7B
+// 00111010 72 3A
+// 10010101 225 95
+// 10110110 266 B6
+// 01011011 133 5B
+// ___________________________________
diff --git a/1871/CH8/EX8.3/Ch08Ex3.sce b/1871/CH8/EX8.3/Ch08Ex3.sce new file mode 100755 index 000000000..8862781e3 --- /dev/null +++ b/1871/CH8/EX8.3/Ch08Ex3.sce @@ -0,0 +1,32 @@ +// Scilab code Ex8.3 : Pg:328(2008)
+clc;clear;
+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 [decf]= binfrac_decifrac(nf) // Function to convert binary fraction to decimal fraction
+ decf = 0;
+ i = -1;
+ while (i >= -3)
+ nf = nf*10;
+ rem = round(nf);
+ nf = nf-rem;
+ decf = decf + rem*2.^i;
+ i = i - 1;
+ end
+endfunction
+
+n = 101.101; // Initialize the binary number
+n_int = int(n); // Extract the integral part
+n_frac = n-n_int; // Extract the fractional part
+printf("Decimal equivalent of %7.3f = %5.3f", n, binary_decimal(n_int)+binfrac_decifrac(n_frac));
+
+// Result
+// Decimal equivalent of 101.101 = 5.625
diff --git a/1871/CH8/EX8.4/Ch08Ex4.sce b/1871/CH8/EX8.4/Ch08Ex4.sce new file mode 100755 index 000000000..cabb514e3 --- /dev/null +++ b/1871/CH8/EX8.4/Ch08Ex4.sce @@ -0,0 +1,31 @@ +// Scilab code Ex8.4 : Pg:330(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 [binf]= decifrac_binfrac(nf) // Function to convert binary fraction to decimal fraction
+ binf = 0; i = 0.1;
+ while (nf <> 0)
+ nf = nf*2;
+ rem = int(nf);
+ nf = nf-rem;
+ binf = binf + rem*i;
+ i = i/10;
+ end
+endfunction
+
+n = 25.625; // Initialize the decimal number
+n_int = int(n); // Extract the integral part
+n_frac = n-n_int; // Extract the fractional part
+printf("Binary equivalent of %6.3f = %9.3f", n, decimal_binary(n_int)+decifrac_binfrac(n_frac));
+
+// Result
+// Binary equivalent of 25.625 = 11001.101
diff --git a/1871/CH8/EX8.5/Ch08Ex5.sce b/1871/CH8/EX8.5/Ch08Ex5.sce new file mode 100755 index 000000000..cd25f6a6c --- /dev/null +++ b/1871/CH8/EX8.5/Ch08Ex5.sce @@ -0,0 +1,31 @@ +// Scilab code Ex8.5 : Pg:332(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
+
+num1 = 110011; // Initialize the first binary number
+num2 = 101101; // Initialize the second binary number
+
+printf("%6d + %6d = %7d", num1, num2, decimal_binary(binary_decimal(num1)+binary_decimal(num2)));
+
+// Result
+// 110011 + 101101 = 1100000
diff --git a/1871/CH8/EX8.6/Ch08Ex6.sce b/1871/CH8/EX8.6/Ch08Ex6.sce new file mode 100755 index 000000000..fa938041f --- /dev/null +++ b/1871/CH8/EX8.6/Ch08Ex6.sce @@ -0,0 +1,31 @@ +// Scilab code Ex8.6 : Pg:333(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
+
+sub = 1110; // Initialize the first binary number
+men = 0101; // Initialize the second binary number
+
+printf("%4d - 0%3d = %4d", sub, men, decimal_binary(binary_decimal(sub)-binary_decimal(men)));
+
+// Result
+// 1110 - 0101 = 1001
diff --git a/1871/CH8/EX8.7/Ch08Ex7.sce b/1871/CH8/EX8.7/Ch08Ex7.sce new file mode 100755 index 000000000..2cce5723c --- /dev/null +++ b/1871/CH8/EX8.7/Ch08Ex7.sce @@ -0,0 +1,31 @@ +// Scilab code Ex8.7 : Pg:333(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
+
+sub = 1000; // Initialize the first binary number
+men = 0001; // Initialize the second binary number
+
+printf("%4d - 000%1d = 0%3d", sub, men, decimal_binary(binary_decimal(sub)-binary_decimal(men)));
+
+// Result
+// 1000 - 0001 = 0111
diff --git a/1871/CH8/EX8.8/Ch08Ex8.sce b/1871/CH8/EX8.8/Ch08Ex8.sce new file mode 100755 index 000000000..c5c468aee --- /dev/null +++ b/1871/CH8/EX8.8/Ch08Ex8.sce @@ -0,0 +1,31 @@ +// Scilab code Ex8.8 : Pg:334(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
+
+sub = 1001; // Initialize the first binary number
+men = 0111; // Initialize the second binary number
+
+printf("%4d - 0%1d = 00%2d", sub, men, decimal_binary(binary_decimal(sub)-binary_decimal(men)));
+
+// Result
+// 1001 - 0111 = 0010
diff --git a/1871/CH8/EX8.9/Ch08Ex9.sce b/1871/CH8/EX8.9/Ch08Ex9.sce new file mode 100755 index 000000000..c3e894518 --- /dev/null +++ b/1871/CH8/EX8.9/Ch08Ex9.sce @@ -0,0 +1,31 @@ +// Scilab code Ex8.9 : Pg:334(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
+
+sub = 10110; // Initialize the first binary number
+men = 01011; // Initialize the second binary number
+
+printf("%5d - 0%4d = 0%4d", sub, men, decimal_binary(binary_decimal(sub)-binary_decimal(men)));
+
+// Result
+// 10110 - 01011 = 01011
|