bit manipulation functions
Steve Kargl
sgk@troutmask.apl.washington.edu
Sun Dec 5 18:00:00 GMT 2004
Gang,
It seems that our bit manipulation functions are more
restrictive than implied by the language of the standard
(where I only have a PDF of the May 2004 working draft of
the newest standard). Anyway, the language for IBSET is
------------------------------------------------------
13.7.49 IBSET(I, POS)
Description. Sets one bit to one.
Class. Elemental function.
Arguments.
I shall be of type integer.
POS shall be of type integer. It shall be nonnegative and
less than BIT_SIZE(I).
Result Characteristics. Same as I.
Result Value. The result has the value of the sequence of bits of
I, except that bit POS is one.
------------------------------------------------------
Note I and POS "shall be of type integer". The kind parameter is
not specified. We currently restrict POS to default integer
kind with an implied restriction of I to default integer kind.
The restriction is in check.c
try
gfc_check_ibset (gfc_expr * i, gfc_expr * pos)
{
if (type_check (i, 0, BT_INTEGER) == FAILURE
|| type_check (pos, 1, BT_INTEGER) == FAILURE
|| kind_value_check (pos, 1, gfc_default_integer_kind) == FAILURE)
return FAILURE;
return SUCCESS;
}
If I remove the kind_value_check() from the above. The attached
program produces the following output:
kargl[205] ./bits
8 16 32 64
6 6 6 6
20 20 20 20
12 4108 4108 4108
This agrees with both G77 and NAG's Fortran 95 compiler. So, do
we want to loosen the restrictions?
--
Steve
-------------- next part --------------
program bits
integer b1, b2, b4, b8
integer*1 i1, j1
integer*2 i2, j2
integer*4 i4, j4
integer*8 i8, j8
b1 = bit_size(i1)
b2 = bit_size(i2)
b4 = bit_size(i4)
b8 = bit_size(i8)
write(*,'(4I3)') b1, b2, b4, b8
i1 = 2
j1 = ibset(i1,i1)
i2 = 2
j2 = ibset(i2,i2)
i4 = 2
j4 = ibset(i4,i4)
i8 = 2
j8 = ibset(i8,i8)
write(*,'(4I3)') j1, j2, j4, j8
i1 = 4
j1 = ibset(i1,i1)
i2 = 4
j2 = ibset(i2,i1)
i4 = 4
j4 = ibset(i4,i1)
i8 = 4
j8 = ibset(i8,i1)
write(*,'(4I3)') j1, j2, j4, j8
i1 = 12
j1 = ibset(i1,i1)
i2 = 12
j2 = ibset(i2,i1)
i4 = 12
j4 = ibset(i4,i1)
i8 = 12
j8 = ibset(i8,i1)
write(*,'(4I5)') j1, j2, j4, j8
end
More information about the Fortran
mailing list