cjn@homxb.UUCP (04/10/87)
------------------------------------------------ Penn State Univ. has two 3b-15 computers with FORTRAN-77 XLA PLUS. They would like to write fortran programs capable of bitwise operations, but are unusre how to go about it. Since I have little knowledge of FORTRAN-77, I post the following queries to the net: - how do you write a bitwise AND - how about bitwise OR - can you reference modules written in C from a fortran subroutine. If so, how do you go about it. Please send email to homxb!cjn Thanks in advance. - charlie northrup bell labs hr-1h332
jerraya@imag.UUCP (04/19/87)
in article <214@homxb.UUCP>, cjn@homxb.UUCP says: > Posted: Fri Apr 10 15:15:14 1987 > > ------------------------------------------------ > Penn State Univ. has two 3b-15 computers with FORTRAN-77 XLA PLUS. > They would like to write fortran programs capable of bitwise operations, > but are unusre how to go about it. Since I have little knowledge of > FORTRAN-77, I post the following queries to the net: > > - how do you write a bitwise AND > - how about bitwise OR > You can use the equivalence construction to achieve this. As example, I give a fortran function that compute an AND bitwise operation: integer function AND(i1,i2) integer k1, k2,k3 logical j1,j2,j3 equivalence (k1,j1), (k2,j2), (k3,j3) k1=i1 k2=i2 j3=j1.and.j2 AND=k3 end If you substitute the [and,AND] by [or,OR] ..... -- Ahmed Amine JERRAYA IMAG-TIM3/INPG, 46 Avenue Felix Viallet, F-38031 GRENOBLE CEDEX - FRANCE Tel: 76 47 98 55 Ext: 607 UUCP: ...!seismo!mcvax!inria!archi!titine!jerraya or: jerraya@archi
Beebe@SCIENCE (Nelson H.F. Beebe) (04/20/87)
X-Us-Mail: "Center for Scientific Computation, South Physics, University of Utah, Salt Lake City, UT 84112" X-Telephone: (801) 581-5254 Regarding this subterfuge for fullword bit twiddling: >> integer function AND(i1,i2) >> integer k1, k2,k3 >> logical j1,j2,j3 >> equivalence (k1,j1), (k2,j2), (k3,j3) >> >> k1=i1 >> k2=i2 >> j3=j1.and.j2 >> AND=k3 >> end >> IT WON'T WORK ON MANY MACHINES. First, a compiler is free to implement FORTRAN LOGICAL data as any object of size at least one bit. I have seen compilers that tested the sign bit, others that test the right-most bit, others that test either the right-most or left-most byte, and still others that test for zero/non-zero. The LOGICAL .OP. operators will rarely work on fullword data. Second, there may be no guarantee that a LOGICAL is of the same size as an INTEGER, and I could name one compiler where they are not (apparently in violation of ANSI F77, Section 4.7, p. 4-4 "A logical datum has one numeric storage unit in a storage sequence"). The code above will work on some machines, but comments should definitely flag it as non-portable. It is regrettable that ANSI never included a standard set of integer bit operations (.AND., .OR., .NOT., .XOR.), since almost all compilers (IBM notably excepted) have implemented them in some fashion. -------
spaf@gt-karloff.uucp (Gene Spafford) (04/26/87)
The following implements a bitwise logical AND operation in Fortran.
It is *NOT* portable, but the idea can be adapted fairly easily to
other representations. The other functions (OR, XOR, NOT, etc)
can be implemented in a similar manner.
Assumptions for this version:
1) Integer representation is 2's complement, with sign bit in
most significant bit position.
2) Integer addition quietly ignores overflow
3) Parameter NUMBITS is set to number of bits in an integer.
Way it works: by doubling the number each time through the loop, you
are doing an effective shift left by one position. If the sign bit
is set, the most significant bit is a one, and the number is negative.
By comparing bits, we know whether to introduce a 0 or 1 bit into
the result. The result is shifted too, to bring the bits into
the right positions.
integer function bitand (iarg1, iarg2)
integer iarg1, iarg2
integer loop, jarg1, jarg2, kret
parameter (numbits = 32)
kret = 0
jarg1 = iarg1
jarg2 = iarg2
do 10 loop = 1, numbits
kret = kret + kret
if (jarg1 .lt. 0 .and. jarg2 .lt. 0) kret = kret + 1
jarg1 = jarg1 + jarg1
jarg2 = jarg2 + jarg2
10 continue
bitand = kret
end
Gene Spafford
Software Engineering Research Center (SERC), Georgia Tech, Atlanta GA 30332
CSNet: Spaf @ GATech ARPA: Spaf@Gatech.EDU
uucp: ...!{akgua,decvax,hplabs,ihnp4,linus,seismo,ulysses}!gatech!spaf