Saturday, 20 October 2018

Perform OR operation on two decimal numbers (Octave Code)

function  Or_conversion(dec1,dec2)
i=1;
j=1;
dec_count=1;

for dec_count=1:1:2
  if dec_count==1
    dividend=dec1;
    i=i;
  end
  if dec_count==2
    dividend=dec2;
    i=j;
  end
  if dividend==0
    binary_value=0
  else
q_temp=dividend;
    while (q_temp>1)
      r_temp=mod(q_temp,2);
      binary_matrix(i,1)=q_temp;
      binary_matrix(i,2)=r_temp;

      q_temp=floor(dividend/2);
      dividend=q_temp;
      i=i+1;
    end
   binary_matrix(i,2)=1;
   a=binary_matrix(:,2);
   b=a';
   binary_value=flip(b);
   end
 
  if dec_count==1
    A_binary_value=binary_value
  end
  if dec_count==2
    B_binary_value=binary_value
  end
end

% bits padding
  A_count=size(A_binary_value);
  A_count=A_count(1,2);
  B_count=size(B_binary_value);
  B_count=B_count(1,2);
  count=max(A_count,B_count)
  for net_count=count: -1 : 1
    if A_count == 0
      A_binary_value(1,net_count)=0;
    else
      A_binary_value(1,net_count)=A_binary_value(1,A_count);
      A_count=A_count-1;
    end
    if  B_count == 0
     B_binary_value(1,net_count)=0;
    else
     B_binary_value(1,net_count)=B_binary_value(1,B_count);
     B_count=B_count-1;
    end
 
  end
    A= A_binary_value(1,:)
    B= B_binary_value(1,:)
 
% Zero padding end

% Oring the two values
  for Or_count=count:-1:1
    if A(1,Or_count)==B(1,Or_count);
      Or_result(1,Or_count)=A(1,Or_count);
    else
      Or_result(1,Or_count)=1;
    end
  end
    Or_result=Or_result(1,:)
   
end

Flaws of the above code:

1. It will only work correctly if smaller number is entered as dec1 and greater number as dec2.
2. This program contains many loops so speed of evaluation will be slow.

No comments:

Post a Comment

Program to solve 4 variable equation(octave code)

function [x,y,z,alpha]=eq4variable( aaaa1,bbbb1,dddd1,eeee1,cccc1,aaaa2,bbbb2,dddd2,eeee2,cccc2,aaaa3,bbbb3,dddd3,eeee3,cccc3,aaaa4,bbbb4,d...