I am kinda new to C and was wondering how would i do
2 to the power of n and then subtract one.
thank you
I am kinda new to C and was wondering how would i do
2 to the power of n and then subtract one.
thank you
you could use the pow() function:
it would look something like this
When you have that number, it is relatively easy to subtract 1 from it.Code:pow(base, n); /* in your case pow is the function call, base would be 2 and n is whatever number you want to raise it to */
to use pow() make sure you #include math.h, and if you are using gcc, be sure to the -lm flag to link the math library.
pow returns a double, so be sure to keep that in mind when you use it.
~/
Last edited by kermit; 08-14-2004 at 02:19 PM.
pow is unnesseserry here, he only wants power of 2. If n is less than 31 then:
Code:unsigned int answer, n = SOMETHING; answer = ~(0xFFFFFFFF<<n);
Bitwise operations should not be used in place of pow(). What happens if the integer is a negative, is a 64 bit number, is a 128 bit number. What if they want to use 3 instead of 2, or negitive 2, or a negative power.
Use pow() and save yourself a lot of trouble.
Also important, what if they want to raise a real value to an exponent. Shifting certainly won't work.
edit: I know he said 2 raised to n , but still thought it should be mentioned.
"...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers