Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,845 questions

51,766 answers

573 users

How to sum two binary strings in Pascal

1 Answer

0 votes
program AddBinaryStrings;

function BinStrToDec(const binStr: string): LongInt;
var
  i, power: Integer;
begin
  BinStrToDec := 0;
  power := 0;
  for i := Length(binStr) downto 1 do
  begin
    if binStr[i] = '1' then
      Inc(BinStrToDec, 1 shl power);
    Inc(power);
  end;
end;

function DecToBinStr(num: LongInt): string;
begin
  DecToBinStr := '';
  if num = 0 then
    DecToBinStr := '0';
  while num > 0 do
  begin
    DecToBinStr := Chr((num mod 2) + Ord('0')) + DecToBinStr;
    num := num div 2;
  end;
end;

function AddBinaryStrings(a, b: string): string;
var
  num1, num2, sum: LongInt;
begin
  num1 := BinStrToDec(a);
  num2 := BinStrToDec(b);
  sum := num1 + num2;
  AddBinaryStrings := DecToBinStr(sum);
end;

begin
  Writeln(AddBinaryStrings('11', '1'));      
  Writeln(AddBinaryStrings('1010', '1011')); 
end.



(*
run:

100
10101

*)

 



answered Jul 2, 2025 by avibootz
...