-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask1.txt
More file actions
36 lines (30 loc) · 815 Bytes
/
task1.txt
File metadata and controls
36 lines (30 loc) · 815 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
32
33
34
35
36
#!/bin/bash
echo "Numarul de argumente este $#."
re='^[+-]?[0-9]+([.][0-9]+)?$'
suma=0
for number do
if [[ $number =~ $re ]] ; then
w=0 # Hamming weight (count of bits that are 1)
n=$number # work on $n to save $number for later
# test the last bit of the number, and right-shift once
# repeat until number is zero
while (( n > 0 )); do
if (( (n & 1) == 1 )); then
# last bit was 1, count it
w=$(( w + 1 ))
fi
if (( w > 1 )); then
# early bail-out: not a power of 2
break
fi
# right-shift number
n=$(( n >> 1 ))
done
if (( w == 1 )); then
# this was a power of 2
printf '%d\n' "$number"
fi
suma=$((suma+$number))
fi
done
echo "Suma numerelor este $suma."