aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorE7-87-83 <fungcheokyin@gmail.com>2021-07-18 22:13:30 +0800
committerE7-87-83 <fungcheokyin@gmail.com>2021-07-18 22:13:30 +0800
commitd211659a48fcc4a2b2516cd1f0f7b6c97bbac149 (patch)
tree2affb24bc6dc1071eeef4baba122a1b268416a16
parentabe8a6c8742c120716905f375029b5cf26b9474f (diff)
downloadperlweeklychallenge-club-d211659a48fcc4a2b2516cd1f0f7b6c97bbac149.tar.gz
perlweeklychallenge-club-d211659a48fcc4a2b2516cd1f0f7b6c97bbac149.tar.bz2
perlweeklychallenge-club-d211659a48fcc4a2b2516cd1f0f7b6c97bbac149.zip
week 121: 2 Perl scripts, 1 bash script
-rw-r--r--challenge-121/cheok-yin-fung/bash/ch-1.sh40
-rw-r--r--challenge-121/cheok-yin-fung/perl/ch-1.pl13
2 files changed, 53 insertions, 0 deletions
diff --git a/challenge-121/cheok-yin-fung/bash/ch-1.sh b/challenge-121/cheok-yin-fung/bash/ch-1.sh
new file mode 100644
index 0000000000..f2fec6ffc1
--- /dev/null
+++ b/challenge-121/cheok-yin-fung/bash/ch-1.sh
@@ -0,0 +1,40 @@
+#!/bin/bash
+# The Weekly Challenge - 121
+# Task 1 Invert Bit
+# Usage: $ chmod +x ch-1.sh
+# $ ./ch-1.sh n m
+
+n=$1
+m=$2
+
+if [ $n -gt 255 ] || [ $n -lt 0 ] ;
+then
+ echo "Integer within 0 to 255."
+ exit
+fi
+
+t=$n
+c=0
+declare -a arr
+
+while [ $t -gt 0 ]
+do
+ c=$(($c+1))
+ if [ $c -ne $m ] ;
+ then arr+=($(($t%2)))
+ else arr+=($(($(($t%2+1))%2)))
+ fi
+ t=$(($t/2))
+done
+
+ans=0
+
+if [ $c -lt $m ];
+ then ans=$(($n+2**($m-1)))
+ else
+ for (( i = 0 ; i < $c ; i++ )); do
+ ans=$(( $ans + $[arr[i]] * 2**i ))
+ done
+fi
+
+echo $ans
diff --git a/challenge-121/cheok-yin-fung/perl/ch-1.pl b/challenge-121/cheok-yin-fung/perl/ch-1.pl
new file mode 100644
index 0000000000..ef2293cbc2
--- /dev/null
+++ b/challenge-121/cheok-yin-fung/perl/ch-1.pl
@@ -0,0 +1,13 @@
+say ($ARGV[0] ^ (1 << $ARGV[1]-1));
+
+=pod
+ https://en.wikipedia.org/wiki/Bitwise_operation
+ #Truth_table_for_all_binary_logical_operators
+
+p q
+1 1 -> 0
+1 0 -> 1
+0 1 -> 1
+0 0 -> 0
+
+operation: XOR