You can also use bitshift since the base is 2

I did it in rust but in c++ the shift operator should be the same:

println!("2 to the power of 24 is: {}", 2u32 << 23);

I’m using 23 as the shift number since every increment of the position of the bit corresponds to a multiplication. 2u32 << 1 would already be 4 = 2^2

Oh this is really clever. I just did the standard/boring approach of i32.pow()

If you are the only one looking at the code then this is perfect and efficient. If someone else looks at it who is not a CS student, they might not know this is a left shift lol. I love it though.

1 Like

Privacy & Terms