-
Notifications
You must be signed in to change notification settings - Fork 319
/
ch-1.pl
53 lines (50 loc) · 1.27 KB
/
ch-1.pl
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
52
53
#!/usr/bin/perl ;
use strict ;
use warnings ;
use feature 'say' ;
sub toBinaryString {
my $number = shift ;
my @bits ;
while ( $number != 0 ) {
unshift @bits , $number % 2 ;
$number = int ( $number / 2 ) ;
}
return join( '' , @bits ) ;
}
sub fromBinaryString {
my $numberstring = shift ;
my $sum = 0 ;
my @reversed = reverse split( // , $numberstring ) ;
my $multiplier = 1 ;
for my $num ( @reversed ) {
$sum += $multiplier * $num ;
$multiplier *= 2 ;
}
return $sum ;
}
my $N = $ARGV[ 0 ] ;
if ( $N > 255 ) {
die "Only numbers up to and including 255 should be used!" ;
}
my $numberstring = toBinaryString( $N ) ;
my $len = length $numberstring ;
my $firstNibble ;
my $secondNibble ;
if ( $len < 4 ) {
$firstNibble = "0" x ( 4 - $len ) . $numberstring ;
$secondNibble = "0" x 4 ;
}
if ( $len == 4 ) {
$firstNibble = $numberstring ;
$secondNibble = "0" x 4 ;
}
if ( $len > 4 and $len < 8 ) {
$firstNibble = "0" x ( 8 - $len ) . substr( $numberstring , 0 , $len - 4 ) ;
$secondNibble = substr( $numberstring , $len - 4 ) ;
}
if ( $len == 8 ) {
$firstNibble = substr( $numberstring , 0 , 4 ) ;
$secondNibble = substr( $numberstring , 4 ) ;
}
my $reversedNumber = $secondNibble . $firstNibble ;
say fromBinaryString( $reversedNumber ) ;