From d211659a48fcc4a2b2516cd1f0f7b6c97bbac149 Mon Sep 17 00:00:00 2001 From: E7-87-83 Date: Sun, 18 Jul 2021 22:13:30 +0800 Subject: week 121: 2 Perl scripts, 1 bash script --- challenge-121/cheok-yin-fung/bash/ch-1.sh | 40 +++++++++++++++++++++++++++++++ challenge-121/cheok-yin-fung/perl/ch-1.pl | 13 ++++++++++ 2 files changed, 53 insertions(+) create mode 100644 challenge-121/cheok-yin-fung/bash/ch-1.sh create mode 100644 challenge-121/cheok-yin-fung/perl/ch-1.pl 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 -- cgit