Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!seismo!gatech!gt-karloff!spaf From: spaf@gt-karloff.uucp (Gene Spafford) Newsgroups: comp.lang.fortran Subject: Re: Re:FORTRAN-77 XLA PLUS BITWISE OPERATORS Message-ID: <15115@gatech.gatech.edu> Date: Sun, 26-Apr-87 14:11:34 EDT Article-I.D.: gatech.15115 Posted: Sun Apr 26 14:11:34 1987 Date-Received: Sun, 26-Apr-87 23:38:11 EDT References: <12296045502.17.BEEBE@SCIENCE.UTAH.EDU> Sender: news@gatech.edu Reply-To: spaf@karloff.gatech.edu (Gene Spafford) Organization: Software Engineering Research Center (SERC), Georgia Tech Lines: 42 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