123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 |
- #!/bin/bash
- add_byte() {
- local a=$1
- local b=$2
- if [ $a -lt 0 ]; then
- a=$((256 + a))
- fi
- if [ $b -lt 0 ]; then
- b=$((256 + b))
- fi
- local sum=$((a + b))
- sum=$((sum % 256))
- if [ $sum -gt 127 ]; then
- sum=$((sum - 256))
- fi
- echo $sum
- }
- add_word() {
- local a=$1
- local b=$2
- if [ $a -lt 0 ]; then
- a=$((65536 + a))
- fi
- if [ $b -lt 0 ]; then
- b=$((65536 + b))
- fi
- local sum=$((a + b))
- sum=$((sum % 65536))
- if [ $sum -gt 32767 ]; then
- sum=$((sum - 65536))
- fi
- echo $sum
- }
- result1=$(add_byte 79 -46)
- echo "79 + (-46) = $result1"
- result2=$(add_byte -97 96)
- echo "-97 + 96 = $result2"
- result3=$(add_word 12868 -1219)
- echo "12868 + (-1219) = $result3"
|