aboutsummaryrefslogtreecommitdiff
path: root/challenge-119/colin-crain/perl/ch-1.pl
blob: 0b0814766498a8c24398f240605555025acc79f1 (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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/Users/colincrain/perl5/perlbrew/perls/perl-5.32.0/bin/perl
#
#       nibble-n-swap.pl
# 
#       Swap Nibbles
#         Submitted by: Mohammad S Anwar
#         You are given a positive integer $N.
# 
#         Write a script to swap the two nibbles of the binary representation of
#         the given number and print the decimal number of the new binary
#         representation.
# 
#         A nibble is a four-bit aggregation, or half an octet.
# 
#         To keep the task simple, we only allow integer less than or equal to
#         255.
# 
#         Example
#         Input: $N = 101
#         Output: 86
# 
#         Binary representation of decimal 101 is 1100101 or as 2 nibbles (0110)(0101).
#         The swapped nibbles would be (0101)(0110) same as decimal 86.
# 
#         Input: $N = 18
#         Output: 33
# 
#         Binary representation of decimal 18 is 10010 or as 2 nibbles (0001)(0010).
#         The swapped nibbles would be (0010)(0001) same as decimal 33.
#
#
#       © 2021 colin crain
## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##

use 5.32.0;

use warnings;
use strict;
use utf8;
use feature qw(signatures);
no warnings 'experimental::signatures';


my $num = shift @ARGV // 3;

0 < $num < 256 or die "number must be in range 1 to 255 inclusive";

my $bin = sprintf "%08b", $num;
substr( $bin, 0, 4 ) = substr( $bin, 4, 4, substr( $bin, 0, 4));
say oct "0b$bin";