aboutsummaryrefslogtreecommitdiff
path: root/challenge-120/james-smith/perl/ch-1.pl
blob: 31b470c9615c2fea2c844c1f9e97d0d1a07d9c27 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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;
}