473,394 Members | 1,714 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Powers of two

14
hey guys thanks for the vowel help it worked
i got one other question here.
here is the method/function signature
public int powerOfTwo(int number)

the function should return a number such that number - x is a power of two if number is already a power of two it should return 0.
Feb 28 '08 #1
5 1686
JosAH
11,448 Expert 8TB
here is the method/function signature
public int powerOfTwo(int number)

the function should return a number such that number - x is a power of two if number is already a power of two it should return 0.
I don't understand the question: what is 'x' and is that dash '-' a minus sign?
If it is then the last phrase is redundant. Sorry for being stupid.

kind regards,

Jos
Feb 28 '08 #2
BigDaddyLH
1,216 Expert 1GB
The experts on this site are more than happy to help you with your problems but they cannot do your assignment/program for you. Attempt the assignment/program yourself first and post questions regarding any difficulties you have or about a particular function of the code that you don't know how to achieve.

Please read the Posting Guidelines and particularly the Coursework Posting Guidelines.

Then when you are ready post a new question in this thread.

MODERATOR
Feb 28 '08 #3
Doegon
14
public int powerOfTwo(int number)
{
int base = 2;
for(int i=0;i< Math.pow(base,Integer.MAX_VALUE);i++)
{
int powerOf2 = Math.pow(base,i);
}
}

my apologies for breaking the rules,it won't happen again.as you can see i did attempt but i can only come up with a function which i'm not sure it will give me the powers of two.

I understand x is an arbitrary number which if subtracted from the given number in parameters and one subtracted it should result in a power of two.yes that's a minus sign.

i hope this clears the question cause I'm still struggling with it
Feb 29 '08 #4
JosAH
11,448 Expert 8TB
Ah, I think I understand your question now, you want a function f(n) such that
x= f(n) where n-x is a power of two. Lets ignore n == 0 for now. Right?

If so, note that every number n can be written as 1xxx...xxx where xxx...xxx
are zero or more random digits. Let x = xxx...xxx then n-x is a power of two
and x is minimal.

Also note that 2^m == 1 << m.

kind regards,

Jos
Feb 29 '08 #5
BigDaddyLH
1,216 Expert 1GB
public int powerOfTwo(int number)
{
int base = 2;
for(int i=0;i< Math.pow(base,Integer.MAX_VALUE);i++)
{
int powerOf2 = Math.pow(base,i);
}
}
You should check with your instructor, but I think using Math.pow defeats the purpose of this exercise. I suspect you're supposed to do the math yourself.
Feb 29 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Karen | last post by:
Most my experience has been with MySQL, so I write queries in terms of SQL statements. I am using access right now as it is on the machine I have. I am using the SQL window as I couldn't figure out...
3
by: dsptechie | last post by:
I wanted to how exactly malloc() function works. I came to know that malloc() allocates memory only in powers of 2. i.e if asked for say 17 bytes , in the process, it allocates 32 bytes and returns...
4
by: PengYu.UT | last post by:
Hi All, For example, I want to declare an array double array; Of cause, I can define an macro for the 4th power of VERY_VERY_VERY_LONG_NAME. But I want to know whether there is any build...
3
by: Janice | last post by:
I got this question from my textbook and I cannot understand the theory. When a signed positive integer X divided by pow(2,k), the result is shifting k bits to right and putting w-k bits of 0 from...
14
by: Lisa Simpson | last post by:
I am writing a program (precisely an image processing filter) which computes several multiplications of floating-point numbers by constant powers of 2. This means adding an integer constant to the...
7
by: Camellia | last post by:
hi all, I wrote a "table of powers" program and made the 5th power the highest, but when I tried to run the program I found a problem. Here's the output: Integer Square power...
4
by: benbridle38 | last post by:
Hi, dont really know much about programming or anything like this. But basicly in my program i am trying to write the following formula: http://img8.imageshack.us/img8/4379/fomula.jpg...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.