PROGRAM Binary_Adder_Subtractor_And_Multiplyer
!-----------------------------------------------



IMPLICIT NONE
INTEGER :: Input1, Input2, m, c
LOGICAL :: zero
LOGICAL, DIMENSION(1:32) :: BN1, BN2, CO, BN3, BNM1, BNM2, BNM3, TM, RNA, RNM  !Binary Numbers 1, 2, and 3, and the same for multiplication, BN3 and BNM3 are the sum and product respectivly
                            !TM is temp Multiply, and RN is realnumber representation of a given number, addition or multiplication.
OPEN(1, FILE = "ProjectAssignment2.txt",STATUS = "REPLACE")

zero = .FALSE.

!Get the Binary numbers
WRITE (*,*) "Enter the first number within integer range: "
READ (*,*) Input1
WRITE (*,*) "Enter the first number within integer range: "
READ (*,*) Input2

IF Input1 < 0                !Determines sign of binary represnation of int
   THEN BN1(32) = .TRUE.
   ELSE BN1(32) = .FALSE.
END IF

IF Input2 < 0
   THEN BN2(1) = .TRUE.
   ELSE BN2(1) = .FALSE.
ENDIF

DO i = 30, 0                !Determines value of binary representations
IF (Input1/2**i > 0)
   THEN
      BN1(32 - i) = 1
   ELSE
      BN1(32 - i) = 0
END IF
Input1 = MOD(Input1,2**i)
END DO

DO i = 30, 0
IF (Input2/2**i > 0)
   THEN
      BN2(32 - i) = 1
   ELSE
      BN2(32 - i) = 0
END IF
Input2 = MOD(Input2,2**i)
END DO

WRITE (1,*) "Binary Number 1 is: ",BN1
WRITE (*,*) "Binary Number 1 is: ",BN1
WRITE (1,*) "Binary Number 1 is: ",BN2
WRITE (*,*) "Binary Number 1 is: ",BN2

BNM1 = BN1 !Preserve valuse for multiplication
BNM2 = BN2

IF (BN1(1) == BN2(1))
   THEN                             !Adding algorthym

      CO(32) = .FALSE.
      DO i = 32, 2
      LOGICAL :: temp1, temp2, temp3

         temp1 = BN1(i) .AND. BN2(i)
         temp2 = (BN1(i) .OR. BN2(i)) .AND. (.NOT. temp1)
         temp3 = CO(i) .AND. temp2
         BN3(i) = (CO(i) .OR. temp2) .AND. (.NOT, temp3)
         CO(i-1) = temp1 .OR. temp3

      End DO
      
      IF(CO(1) == .TRUE.)
         THEN
            WRITE (*,*) "Sum excedes integer limit for 1 word representation of integer."
            WRITE (1,*) "Sum excedes integer limit for 1 word representation of integer."            

      End IF
                      
   ELSE                       !Subtracting algorthym
      m = 2
      DO
         IF (m > 32)
	    THEN
               WRITE (*,*) "The two numbers cancel eachother out perfectly to account for a 0 resultant."
               zero = .TRUE.
               EXIT
         END IF
            IF (BN1(m) == BN2(m))
               THEN
                  m = m + 1
               ELSE
                  IF (BN1(m) == .TRUE.)  !Checks to see which integer is greater.
                     THEN
                        BN3(1) = BN1(1)
                        EXIT
                     ELSE
                        BN3(1) = BN2(1)
                        EXIT
                     END IF
               END IF
      END DO
      
      IF (zero == .FALSE.)
         THEN
            IF(BN1(1) == BN3(1)) !Redetermines which integer is greater based on above assignment
               THEN
                  DO i = 2, 32
                     BN2(i) = .NOT. BN2(i)
                  END DO
                  m = 32
                  DO  !Inverses smaller number and adds 1 to it.
                     IF( m < 2)
                        WRITE (*,*) "There is an error in the previous Do loop."
                        EXIT
                     END IF
                     
                     IF(BN2(m) == .FALSE.)
                        THEN
                           BN2(m) = .TRUE.
                           EXIT
                        ELSE
                           m = m - 1
                     END IF
                  END DO
               ELSE
                  DO i = 2, 32
                     BN1(i) = .NOT. BN1(i)
                  END DO
                  m = 32
                  DO  !Inverses smaller number and adds 1 to it.
                     IF( m < 2)
                        WRITE (*,*) "There is an error in the previous Do loop."
                        EXIT
                     END IF
                     
                     IF(BN1(m) == .FALSE.)
                        THEN
                           BN1(m) = .TRUE.
                           EXIT
                        ELSE
                           m = m - 1
                     END IF
                  END DO
            END IF
            CO(32) = .FALSE.
            DO i = 32, 2
               LOGICAL :: temp1, temp2, temp3
               
               temp1 = BN1(i) .AND. BN2(i)
               temp2 = (BN1(i) .OR. BN2(i)) .AND. (.NOT. temp1)
               temp3 = CO(i) .AND. temp2
               BN3(i) = (CO(i) .OR. temp2) .AND. (.NOT, temp3)
               CO(i-1) = temp1 .OR. temp3
            End DO
         ELSE
            DO i = 1 , 32
               BN3(i) = .FALSE.
            END DO
      END IF
END IF

WRITE (1,*) "Sum is: ", BN3
WRITE (*,*) "Sum is: ", BN3

RNA(1) = BN3(1)                       !Real number determination for sum
DO i = 2, 32
   IF (BN3(i) == 1)
      THEN
         c = 159 - i
         DO j = 2, 9
            IF (c/2**(9-j) > 0)
               THEN
                  RNA(j) = 1
               ELSE
                  RNA(j) = 0
            END IF
            c = MOD(c,2**(9-j))
         END DO
         DO j = i+1, i+23
            IF (j > 32)
               THEN
                  EXIT
               ELSE
                  RNA(9 + j - i) = BN3(j)
            END IF
         END DO
   END IF
END DO

WRITE (1,*) "Sum as a real: ", RNA
WRITE (*,*) "Sum as a real: ", RNA

IF (BNM1(1) == BNM2(1))                   !Multiplication, sign determination
   THEN
      BNM3(1) = .FALSE.
   ELSE
      BNM3(1) = .TRUE.
END IF
DO i = 32, 2
   BNM3(i) = .FALSE.
END DO

DO i = 32, 2
   DO j = 32, 2
      TM(i) = .FALSE.
   END DO   
   
   IF (BNM2(i) == .TRUE.)
      THEN
         DO j = i, 2
            TM(j) = BNM1(j - i + 32)
         END DO         
   ENDIF
   
   DO i = 32, 2
      LOGICAL :: temp1, temp2, temp3
               
      temp1 = TM(i) .AND. BNM3(i)
      temp2 = (TM(i) .OR. BNM3(i)) .AND. (.NOT. temp1)
      temp3 = CO(i) .AND. temp2
      BNM3(i) = (CO(i) .OR. temp2) .AND. (.NOT, temp3)
      CO(i-1) = temp1 .OR. temp3
   End DO
   IF(CO(1) == .TRUE.) !does not guarntee that all products above integer limit will be obseved by this statment.
      THEN
         WRITE (*,*) "Product excedes integer limit for 1 word representation of integer."
         WRITE (1,*) "Product excedes integer limit for 1 word representation of integer."            
   End IF
END DO

WRITE (1,*) "Product is: ", BNM3
WRITE (*,*) "Product is: ", BNM3

RNM(1) = BNM3(1)                       !Real number determination for product
DO i = 2, 32
   IF (BNM3(i) == 1)
      THEN
         c = 159 - i
         DO j = 2, 9
            IF (c/2**(9-j) > 0)
               THEN
                  RNM(j) = 1
               ELSE
                  RNM(j) = 0
            END IF
            c = MOD(c,2**(9-j))
         END DO
         DO j = i+1, i+23
            IF (j > 32)
               THEN
                  EXIT
               ELSE
                  RNM(9 + j - i) = BNM3(j)
            END IF
         END DO
   END IF
END DO

WRITE (1,*) "Product as a real: ", RNM
WRITE (*,*) "Product as a real: ", RNM

CLOSE (1)
STOP
END PROGRAM Binary_Adder