If I am generating bits within an 8 byte integer, I should be able to use any power of two up to 63 for positive results, right ?
integer (8) ix
but that gives a wrong answer.
apparently the math library cannot do powers of two higher then 31.
Is there a way around this problem, other than using a 4 byte integer.
I should have added that the expression with 4 byte integers will be evaluated with 4 byte precision and then the result will be transferred to the 8 byte variable on the left hand side. Doing 2_8**40 would also work because the 8 byte constant would cause the 40 to get promoted to the higher kind before the calculation. This is standard conforming, your code isn't. The is a common error. I would recommend specifying kind on all constants at all times other then where default kind is being used.
Here is a code fragment:
The bits2 is correct, until the answer is more than 32 bits.
Then it goes beserk !
Please read what Andrew wrote again.
Look at the statement
In Fortran, there are rules for establishing the type of an expression, which you will find in a Fortran manual or tutorial. The expression is evaluated using these (and other applicable) rules without any concern for the type/kind of the variable on the left of the '='. If necessary, the result of the evaluation is converted to the type of the variable to the left of '=' and then the variable is updated to have that value.
Both 5 and ibit are of type default integer, i.e., they are 4-byte integers. Now look up the rules and follow them to their logical consequence.