Community
cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
Beginner
33 Views

How to write C_INT32_T value in fortran?

I'm writing a hashing function using unsigned 32 bit integers, however I'm not sure if it can be written/printed? Haven't seen any documentation

What's the best method to write/print a c_int32_t integer in fortran ? In C printf would use PRIu32

 

 

0 Kudos
3 Replies
Highlighted
Valued Contributor II
33 Views

I would say: copy the bits into a 64-bits integer and print that. I have not tested it but try:

i64 = transfer( [0, unsigned_int32], i64 )

(or the other way around?) Or use the bit manipulation routines.

0 Kudos
Highlighted
Black Belt Retired Employee
33 Views

In Intel Fortran, you could use ZEXT(val,C_INT64_T). That is an extension, though widely implemented. In standard Fortran Arjen's suggestion is clever and workable. Unfortunately the various bit intrinsics, such as IBITS, don't allow changing the kind of the result. You could do something like IAND(INT(val,C_INT64_T),Z'00000000FFFFFFFF'), but that looks awkward.

Example:

use ISO_C_BINDING
integer(4) val
val = -214748364
print ('(Z8.8)'), IAND(INT(val,C_INT64_T),Z'00000000FFFFFFFF')
end
D:\Projects>ifort /stand t.f90
Intel(R) Visual Fortran Intel(R) 64 Compiler for applications running on
 Intel(R) 64, Version 18.0.2.185 Build 20180210
Copyright (C) 1985-2018 Intel Corporation.  All rights reserved.

Microsoft (R) Incremental Linker Version 14.13.26129.0
Copyright (C) Microsoft Corporation.  All rights reserved.

-out:t.exe
-subsystem:console
t.obj

D:\Projects>t.exe
F3333334

 

0 Kudos
Highlighted
Beginner
33 Views

I had thought of transfer but ZEXT works and is neater, thanks

 

0 Kudos