Please note that the int casting in the above example is required if we want to have an integer result.
Java math power of 2.
If the second argument is 1 0 then the result is the same as the first argument.
A simple math pow example display 2 to the power of 8.
Introduction to power function in java.
Of set bits and if you get 1 then number is a power of 2.
Let s now pass a double as an argument and see the results.
It is mainly used to return the value of the first argument raised to the power of the second argument.
The math pow function returns the base to the exponent power that is base exponent the base and the exponent are in decimal numeral system.
Double dblresult math pow 4 2 3.
If the base is negative and the exponent is.
All power of two numbers have only one bit set.
The java lang math pow is used to calculate a number raise to the power of some other number.
There are some special cases as listed below.
Power function in java is of type java lang math pow library.
Int intresult int math pow 2 3.
The java lang math pow double a double b returns the value of the first argument raised to the power of the second argument.
This function accepts two parameters and returns the value of first parameter raised to the second parameter.
Examples of power function.
The output will be 74 08800000000001.
Please see count set bits in an integer for counting set bits.
In math 2 8 2x2x2x2x2x2x2x2 256 in java math pow 2 8 note in java the symbol is a xor operator don t use this for the power calculation.
Because pow is a static method of math you always use it as math pow rather than as a method of a math object you created math has no constructor.
The java lang math pow is used to return the value of first argument raised to the power of the second argument.
Special cases if the second argument is positive or negative zero then the result is 1 0.
Introduction to power function in java.
The return type of pow method is double.
And the set bit become unset.
The output will be 8.