From 7724f41709fab6db7dcd3ed4343dab68d72a734b Mon Sep 17 00:00:00 2001 From: drbaggy Date: Mon, 5 Jul 2021 11:51:47 +0100 Subject: first pass at solution using & | << and >> --- challenge-120/james-smith/perl/ch-1.pl | 29 +++++++++++++++++++++++++++++ challenge-120/james-smith/perl/ch-2.pl | 22 ++++++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 challenge-120/james-smith/perl/ch-1.pl create mode 100644 challenge-120/james-smith/perl/ch-2.pl diff --git a/challenge-120/james-smith/perl/ch-1.pl b/challenge-120/james-smith/perl/ch-1.pl new file mode 100644 index 0000000000..31b470c961 --- /dev/null +++ b/challenge-120/james-smith/perl/ch-1.pl @@ -0,0 +1,29 @@ +#!/usr/local/bin/perl + +use strict; + +use warnings; +use feature qw(say); +use Test::More; +use Benchmark qw(cmpthese timethis); +use Data::Dumper qw(Dumper); + +my @TESTS = ( + [ 101, 154 ], + [ 18, 33 ], +); + +is( switch_bits($_->[0]), $_->[1] ) foreach @TESTS; +is( switch_bits($_->[1]), $_->[0] ) foreach @TESTS; + +done_testing(); + +## Similar to last weeks calculation. We and +## with 10101010 to get the higher bits and +## with 01010101 to get the lower bits... +## and use bit shift operators to move them +## left and right respectively. +sub switch_bits { + ($_[0]&0xaa)>>1 | ($_[0]&0x55)<<1; +} + diff --git a/challenge-120/james-smith/perl/ch-2.pl b/challenge-120/james-smith/perl/ch-2.pl new file mode 100644 index 0000000000..2348c8b946 --- /dev/null +++ b/challenge-120/james-smith/perl/ch-2.pl @@ -0,0 +1,22 @@ +#!/usr/local/bin/perl + +use strict; + +use warnings; +use feature qw(say); +use Test::More; +use Benchmark qw(cmpthese timethis); +use Data::Dumper qw(Dumper); + +my @TESTS = ( + [ 0, 1 ], +); + +is( my_function($_->[0]), $_->[1] ) foreach @TESTS; + +done_testing(); + +sub my_function { + return 1; +} + -- cgit