-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDecimal-Binary.sh
More file actions
31 lines (31 loc) · 809 Bytes
/
Decimal-Binary.sh
File metadata and controls
31 lines (31 loc) · 809 Bytes
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
main() {
if [ $# -ne 1 ]; then
echo "You must provide only 1 number."
exit 1
fi
re='^[0-9]+$'
if ! [[ $1 =~ $re ]]; then
echo "$1 is not a positive integer."
exit 1
fi
echo "Conversion of a decimal number $1 to its binary representation."
number=$1
reminder=1
binary_representation=" "
while [ "$number" -gt 0 ]
do
reminder=$(( number % 2))
binary_representation="$binary_representation$reminder"
number=$(( number / 2))
done
i=${#binary_representation}
result=" "
while [ "$i" -gt 0 ]
do
rev=$(echo "$binary_representation" | awk '{ printf substr( $0, "$i",1 ) }')
result="$result$rev"
i=$(( i - 1 ))
done
echo "Binary representation: $result"
}
main "$@"