How to find 2^x quickly in C. If you guys have any idea please help.
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Bitshift to the left, this multiplies numbers by 2 for every place shift, in the same way that shifting decimal numbers to the left multiplies them by 10.
Use the
<<operator, like so:and so on until you get to
1 << 30. If you’re using a signed 32-bit integer then1 << 31will give you -2147483648 because of two’s complement. If you want to go higher than uselong long unsigned intoruint64_t(64-bit integer). Or if your platform supports it:uint128_t.If you want to go even higher, you’ll need to roll your own “big integer” code. Note that some platforms and compilers come with a 128-bit integer type, but runtime performance varies: they may require a processor that can perform 128-bit operations, or they might break it down into two 64-bit operations.