- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- checking if number is power of 2
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-07-2002 12:18 PM
06-07-2002 12:18 PM
Do you know if shell provides some kind of resource that will allow to check if the number is a number power of 2, such as: 4, 16, 32, etc.?
Thank you!
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-07-2002 12:35 PM
06-07-2002 12:35 PM
Solution$num=32
x=`echo "obase=2 ; $num" | bc`
y=${x%%0*}
if [ $y = "1" ] ; then
echo "power of 2!"
else
echo "not power of 2"
fi
By using bc to output in base 2, then a power of 2 number will be a 1 followed by all zeroes.
The y= will strip off the trailing zeroes and if we are left with only "1", it is a power of 2.
-- Rod Hills
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-07-2002 12:43 PM
06-07-2002 12:43 PM
Re: checking if number is power of 2
# return 0 if not power of 2; 1 otherwise
is_power_of_two()
{
X=$1
shift
if [ ${X} -lt 0 ]
then
X=$((${X} * -1))
fi
Y=1
while [ ${X} -ge ${Y} -a ${Y} -gt 0 ]
do
if [ ${X} -eq ${Y} ]
then
return 1
fi
Y=$((${Y} * 2))
done
return 0
} # is_power_of_two
while [ $# -ge 1 ]
do
echo "Value: ${1} \c"
is_power_of_two ${1}
STAT=$?
if [ ${STAT} -ne 0 ]
then
echo "Yes"
else
echo "No"
fi
shift
done
------------------------------------------
The integer underflow takes care of falling off the edge of the world.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-07-2002 01:34 PM
06-07-2002 01:34 PM
Re: checking if number is power of 2
tmp=$1
while true
do
let tmp3=$tmp1*2
tmp1=$tmp3
if [ $tmp3 -eq $tmp ]; then
#echo "number is of power 2"
retStatus=0
break
fi
if [ $tmp3 -gt $tmp ]; then
echo "\tNumber is not power of 2"
retStatus=1
break
fi
done
return $retStatus
Instead of dividing, I multiplied.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-10-2002 06:35 PM
06-10-2002 06:35 PM
Re: checking if number is power of 2
#cat test2power
echo "Please input a number:\c"
read num
count=0;
while true
do
let newnum=$num\/2;
let remain=$num\%2;
let count=$count+1;
if (( $remain == 1 ))
then
echo "It is not power of 2!"
exit 1
fi
if (( $newnum < 2 ))
then
echo "It is $count power of 2"
exit 0
fi
num=$newnum
done