Побитовое сложение - PascalABC.NET

Формулировка задачи:

Pascal) Мне необходимо сложить 2 числа. Можно использовать только (shr, shl, xor, or, and). Подскажите пожалуйста код (Условия и Циклы использовать НЕЛЬЗЯ). Если это делать только через "xor", то одинаковые числа дают в ответе 0.

Код к задаче: «Побитовое сложение - PascalABC.NET»

textual
type __int32 = integer;
function SUM( a: __int32; b: __int32 ): __int32;
var
  res_b_AB, prv_b_AB, re_final: __int32;
begin
 
res_b_AB := a XOR b XOR prv_b_AB;
prv_b_AB := a AND b OR (a OR b) AND prv_b_AB;
re_final := re_final OR (res_b_AB AND (1 SHL 0));
prv_b_AB := prv_b_AB SHL 1;
    
res_b_AB := a XOR b XOR prv_b_AB;
prv_b_AB := a AND b OR (a OR b) AND prv_b_AB;
re_final := re_final OR (res_b_AB AND (1 SHL 1));
prv_b_AB := prv_b_AB SHL 1;
    
res_b_AB := a XOR b XOR prv_b_AB;
prv_b_AB := a AND b OR (a OR b) AND prv_b_AB;
re_final := re_final OR (res_b_AB AND (1 SHL 2));
prv_b_AB := prv_b_AB SHL 1;
    
res_b_AB := a XOR b XOR prv_b_AB;
prv_b_AB := a AND b OR (a OR b) AND prv_b_AB;
re_final := re_final OR (res_b_AB AND (1 SHL 3));
prv_b_AB := prv_b_AB SHL 1;
    
res_b_AB := a XOR b XOR prv_b_AB;
prv_b_AB := a AND b OR (a OR b) AND prv_b_AB;
re_final := re_final OR (res_b_AB AND (1 SHL 4));
prv_b_AB := prv_b_AB SHL 1;
    
res_b_AB := a XOR b XOR prv_b_AB;
prv_b_AB := a AND b OR (a OR b) AND prv_b_AB;
re_final := re_final OR (res_b_AB AND (1 SHL 5));
prv_b_AB := prv_b_AB SHL 1;
    
res_b_AB := a XOR b XOR prv_b_AB;
prv_b_AB := a AND b OR (a OR b) AND prv_b_AB;
re_final := re_final OR (res_b_AB AND (1 SHL 6));
prv_b_AB := prv_b_AB SHL 1;
    
res_b_AB := a XOR b XOR prv_b_AB;
prv_b_AB := a AND b OR (a OR b) AND prv_b_AB;
re_final := re_final OR (res_b_AB AND (1 SHL 7));
prv_b_AB := prv_b_AB SHL 1;
    
res_b_AB := a XOR b XOR prv_b_AB;
prv_b_AB := a AND b OR (a OR b) AND prv_b_AB;
re_final := re_final OR (res_b_AB AND (1 SHL 8));
prv_b_AB := prv_b_AB SHL 1;
    
res_b_AB := a XOR b XOR prv_b_AB;
prv_b_AB := a AND b OR (a OR b) AND prv_b_AB;
re_final := re_final OR (res_b_AB AND (1 SHL 9));
prv_b_AB := prv_b_AB SHL 1;
    
res_b_AB := a XOR b XOR prv_b_AB;
prv_b_AB := a AND b OR (a OR b) AND prv_b_AB;
re_final := re_final OR (res_b_AB AND (1 SHL 10));
prv_b_AB := prv_b_AB SHL 1;
    
res_b_AB := a XOR b XOR prv_b_AB;
prv_b_AB := a AND b OR (a OR b) AND prv_b_AB;
re_final := re_final OR (res_b_AB AND (1 SHL 11));
prv_b_AB := prv_b_AB SHL 1;
    
res_b_AB := a XOR b XOR prv_b_AB;
prv_b_AB := a AND b OR (a OR b) AND prv_b_AB;
re_final := re_final OR (res_b_AB AND (1 SHL 12));
prv_b_AB := prv_b_AB SHL 1;
    
res_b_AB := a XOR b XOR prv_b_AB;
prv_b_AB := a AND b OR (a OR b) AND prv_b_AB;
re_final := re_final OR (res_b_AB AND (1 SHL 13));
prv_b_AB := prv_b_AB SHL 1;
    
res_b_AB := a XOR b XOR prv_b_AB;
prv_b_AB := a AND b OR (a OR b) AND prv_b_AB;
re_final := re_final OR (res_b_AB AND (1 SHL 14));
prv_b_AB := prv_b_AB SHL 1;
    
res_b_AB := a XOR b XOR prv_b_AB;
prv_b_AB := a AND b OR (a OR b) AND prv_b_AB;
re_final := re_final OR (res_b_AB AND (1 SHL 15));
prv_b_AB := prv_b_AB SHL 1;
    
res_b_AB := a XOR b XOR prv_b_AB;
prv_b_AB := a AND b OR (a OR b) AND prv_b_AB;
re_final := re_final OR (res_b_AB AND (1 SHL 16));
prv_b_AB := prv_b_AB SHL 1;
    
res_b_AB := a XOR b XOR prv_b_AB;
prv_b_AB := a AND b OR (a OR b) AND prv_b_AB;
re_final := re_final OR (res_b_AB AND (1 SHL 17));
prv_b_AB := prv_b_AB SHL 1;
    
res_b_AB := a XOR b XOR prv_b_AB;
prv_b_AB := a AND b OR (a OR b) AND prv_b_AB;
re_final := re_final OR (res_b_AB AND (1 SHL 18));
prv_b_AB := prv_b_AB SHL 1;
    
res_b_AB := a XOR b XOR prv_b_AB;
prv_b_AB := a AND b OR (a OR b) AND prv_b_AB;
re_final := re_final OR (res_b_AB AND (1 SHL 19));
prv_b_AB := prv_b_AB SHL 1;
    
res_b_AB := a XOR b XOR prv_b_AB;
prv_b_AB := a AND b OR (a OR b) AND prv_b_AB;
re_final := re_final OR (res_b_AB AND (1 SHL 20));
prv_b_AB := prv_b_AB SHL 1;
    
res_b_AB := a XOR b XOR prv_b_AB;
prv_b_AB := a AND b OR (a OR b) AND prv_b_AB;
re_final := re_final OR (res_b_AB AND (1 SHL 21));
prv_b_AB := prv_b_AB SHL 1;
    
res_b_AB := a XOR b XOR prv_b_AB;
prv_b_AB := a AND b OR (a OR b) AND prv_b_AB;
re_final := re_final OR (res_b_AB AND (1 SHL 22));
prv_b_AB := prv_b_AB SHL 1;
    
res_b_AB := a XOR b XOR prv_b_AB;
prv_b_AB := a AND b OR (a OR b) AND prv_b_AB;
re_final := re_final OR (res_b_AB AND (1 SHL 23));
prv_b_AB := prv_b_AB SHL 1;
    
res_b_AB := a XOR b XOR prv_b_AB;
prv_b_AB := a AND b OR (a OR b) AND prv_b_AB;
re_final := re_final OR (res_b_AB AND (1 SHL 24));
prv_b_AB := prv_b_AB SHL 1;
    
res_b_AB := a XOR b XOR prv_b_AB;
prv_b_AB := a AND b OR (a OR b) AND prv_b_AB;
re_final := re_final OR (res_b_AB AND (1 SHL 25));
prv_b_AB := prv_b_AB SHL 1;
    
res_b_AB := a XOR b XOR prv_b_AB;
prv_b_AB := a AND b OR (a OR b) AND prv_b_AB;
re_final := re_final OR (res_b_AB AND (1 SHL 26));
prv_b_AB := prv_b_AB SHL 1;
    
res_b_AB := a XOR b XOR prv_b_AB;
prv_b_AB := a AND b OR (a OR b) AND prv_b_AB;
re_final := re_final OR (res_b_AB AND (1 SHL 27));
prv_b_AB := prv_b_AB SHL 1;
    
res_b_AB := a XOR b XOR prv_b_AB;
prv_b_AB := a AND b OR (a OR b) AND prv_b_AB;
re_final := re_final OR (res_b_AB AND (1 SHL 28));
prv_b_AB := prv_b_AB SHL 1;
    
res_b_AB := a XOR b XOR prv_b_AB;
prv_b_AB := a AND b OR (a OR b) AND prv_b_AB;
re_final := re_final OR (res_b_AB AND (1 SHL 29));
prv_b_AB := prv_b_AB SHL 1;
    
res_b_AB := a XOR b XOR prv_b_AB;
prv_b_AB := a AND b OR (a OR b) AND prv_b_AB;
re_final := re_final OR (res_b_AB AND (1 SHL 30));
prv_b_AB := prv_b_AB SHL 1;
    
res_b_AB := a XOR b XOR prv_b_AB;
prv_b_AB := a AND b OR (a OR b) AND prv_b_AB;
re_final := re_final OR (res_b_AB AND (1 SHL 31));
prv_b_AB := prv_b_AB SHL 1;
    
 
SUM := re_final;
end;
 
 
 
 
 
begin
  writeln( SUM( 51, -110200 ) );
  writeln( SUM( -8, 12 ) );
  writeln( SUM( 8, 102 ) );
end.

10   голосов, оценка 3.800 из 5


СОХРАНИТЬ ССЫЛКУ