Connecting Tech Pros Worldwide Help | Site Map

java

Newbie
 
Join Date: Nov 2008
Posts: 2
#1: Nov 1 '08
How would you write a function that tells me if its argument is a power of 2?
RedSon's Avatar
Site Moderator
 
Join Date: Jan 2007
Location: America
Posts: 3,387
#2: Nov 3 '08

re: java


...Very Carefully!
markmcgookin's Avatar
Moderator
 
Join Date: Dec 2006
Location: Northern Ireland / England
Posts: 546
#3: Nov 4 '08

re: java


Quote:

Originally Posted by RedSon

...Very Carefully!

How incredibly helpful...

Can you not divide the number by two and then see if the remaining number is a whole number... math.floor or something (sorry I am useless at math...)

The Sun Java API is a fantastic tool for finding out anything ... *cough* much better than MSDN *cough* .... sorry.

You can find it here
RedSon's Avatar
Site Moderator
 
Join Date: Jan 2007
Location: America
Posts: 3,387
#4: Nov 4 '08

re: java


Sorry, sometimes I only feel the need to put as much effort into my answers as a the original poster put into making their question.

Also, we don't solve your homework questions for you. You need to use your resources at school to solve the problem. If you have specific technical problems that you want to ask about the provide some code so that we can help you, otherwise do not expect us to solve your problem for you.
Moderator
 
Join Date: Mar 2006
Posts: 1,103
#5: Nov 4 '08

re: java


There's many quick ways, depending on how the number is stored.
I prefer using some of the Long class members,
http://java.sun.com/j2se/1.5.0/docs/...lang/Long.html

eg. say number is num
if (Long.bitCount(num) == 1)
Reply