Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

flag variables using bit functions or logical variables

Status
Not open for further replies.

clabrag

Programmer
Jun 6, 2008
3
ES
Hi,
I need use flag variables for intensive use in user defined types, but I'm not sure about the faster way for this. I'm considering two posibilities. The first one is use directly logical variables, as follow:

type node_t
....
logical(1), dimension(4) :: flag
...
end type
integer, parameter :: ACTIVE = 1

and the use is:

subroutine foo( node, ... )
type(node_t), intent(in) :: node
....
if( node%flag(ACTIVE) ) then
... do something ...
end if
....
end subroutine foo

The second possibility is use bit intrinsic function, with integer variables:

type node_t
....
integer(1) :: flag
...
end type
integer(1), parameter :: ACTIVE

with:

subroutine foo( node, ... )
type(node_t), intent(in) :: node
....
if( btest(node%flag,ACTIVE) ) then
... do something ...
end if
....
end subroutine foo


Someone can say something about the better way for this,
in order to obtain a good performance, I will really appreciated.

thanks

 
Method 1
If you're using logicals, you should really be using .true. and .false.

Method 2
You need a bit shift to test for 4 flags.

Basically it is a space time thing. Method 1 is faster but it takes more space.
 
Thanks for the answer.

The type(node) have several variables ( coord, veloc, force, ...). I thing if I use 4-logical(1) is not really expensive for the memory.
The most important thing is the speed, then if the use of logical is faster than the bit functions, thats enough for me.

thanks again.
 
Something not quite right about your code.

Are you using ACTIVE as an index or ACTIVE as a flag value?
 
In both cases is an index.
In the first one is the index in the logical(1) array. In the second one is the bit index.
Is a flag for activate or deactivate the node.
( in the second case, the value of ACTIVE is missed, but is just the bit position for the flag)

 
Sorry, nothing wrong - ignore my comment about .true. and .false. - I thought you were using active in place of .true. and .false. (as some C programmers do).
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top