Home >Backend Development >C#.Net Tutorial >How to express 2 to the nth power in C language
In C language, you can use the left shift operator (
The expression of 2 to the nth power in C language
In C language, you can use left shift operator (
Syntax:
<code>x </code>
Where:
x
is the number to be calculated to the nth power of 2n
is the number of digits to be moved, which is the power of 2 Example:
Calculate 2 5th power:
int result = 2 << 5;
The above code moves 2 to the left by 5 places and gets the result 32, which is 2 raised to the 5th power.
Note:
It should be noted that this method can only be used to calculate the power of 2 of non-negative integers. If n
is negative, the behavior of this operator is undefined.
The above is the detailed content of How to express 2 to the nth power in C language. For more information, please follow other related articles on the PHP Chinese website!