## Perl Weekly Challenge 140: Add Binary

These are some answers to the Week 140 of the Perl Weekly Challenge organized by Mohammad S. Anwar.

*Spoiler Alert:* This weekly challenge deadline is due in a few days from now (on November 28, 2021 at 24:00). This blog post offers some solutions to this challenge, please don’t read on if you intend to complete the challenge on your own.

## Task 1: Add Binary

*You are given two decimal-coded binary numbers, $a and $b.*

*Write a script to simulate the addition of the given binary numbers.*

The script should simulate something like`$a + $b`

. (operator overloading)

*Example 1:*

```
Input: $a = 11; $b = 1;
Output: 100
```

*Example 2:*

```
Input: $a = 101; $b = 1;
Output: 110
```

*Example 3:*

```
Input: $a = 100; $b = 11;
Output: 111
```

### Add Binary in Raku

In Raku, we simply convert the binary strings to regular integers, using the parse-base method, add these integers with normal addition, and convert the result back to a binary string with the base built-in method.

```
use v6;
sub add-bin ($a, $b) {
($a.Str.parse-base(2) + $b.Str.parse-base(2)).base(2);
}
for (11, 1), (101, 1), (100, 11), (1011,11) -> $test {
say "$test\t=> ", add-bin $test[0], $test[1];
}
```

This scipt displays the following output:

```
$ raku ./add-binary.raku
11 1 => 100
101 1 => 110
100 11 => 111
```

### Add Binary in Perl

In Perl, we implemented manually an `add_bin`

subroutine to perform direct addition of decimal-coded binary numbers.

```
use strict;
use warnings;
use feature "say";
sub add_bin {
my ($c, $d) = @_;
($d, $c) = ($c, $d) if $d > $c;
my $result = "";
my @c = reverse split //, $c;
my @d = reverse split //, $d;
my $carry = 0; # carry over
for my $i (0 .. $#c){
my $e = $d[$i] // 0;
my $t = $c[$i] + $e + $carry;
$result .= $t and $carry = 0 if $t <= 1;
if ($t == 2) {
$result .= 0;
$carry = 1;
} elsif ($t == 3) {
$result .= 1;
$carry = 1;
}
}
$result .= ($carry == 0 ? '' : $carry == 1 ? 1 : '01');
return scalar reverse $result;
}
for my $test ( [11, 1], [101, 1], [100, 11], [100, 100], [1011, 11]) {
say "@$test\t=> ", add_bin @$test;
}
```

This script displays the following output:

```
$ perl ./add-binary.pl
11 1 => 100
101 1 => 110
100 11 => 111
100 100 => 1000
1011 11 => 1110
```

## Task 2: Multiplication Table

I have no time now to complete this task and will be busy most of the weekend. I might be able to complete it on Sunday evening and will post accordingly if I do.

*Update:* I manage to complete Task 2 in time. Please find below a link to my new blog post providing Raku and Perl solutions to task 2 of PWC 140:

https://blogs.perl.org/users/laurent_r/2021/11/perl-weekly-challenge-140-multiplication-tables.html

## Wrapping up

The next week Perl Weekly Challenge will start soon. If you want to participate in this challenge, please check https://perlweeklychallenge.org/ and make sure you answer the challenge before 23:59 BST (British summer time) on December 5, 2021. And, please, also spread the word about the Perl Weekly Challenge if you can.

## Leave a comment