Loop until 2^n-1

I was just wondering if I was to loop through 0 to 2^n -1 can I just write the code as is? like this:

1
2
  for(int i = 0; i < 2^n -1; i++)
  { //code } 
hello, you should note that ^ doesn't mean "power" in c++, you should use
std::pow(2,n) in cmath.

Also it's simpler to store the value of an expression in a varible and use it in a condition rather than compute it every time in your loop.
1
2
3
int variable = std::pow(2,n) -1;

//use variable in  your loop. 
Last edited on
Topic archived. No new replies allowed.