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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Converting and Adding Binary Numbers

Status
Not open for further replies.

jainamichelle

Technical User
Oct 6, 2010
1
CA
Hi all,

I need help writing a program, using Fortran 90, to convert numerical values into binary then add them.

I'm able to convert numbers into binary, but I'm having difficulty adding two binary numbers. All I know is that I have to use a loop to add corresponding digits, starting with the lower digit, and the carry digits.

So far I have...

program ItAllAddsUp
implicit none

integer:: x=13, y=23, I, J,
integer:: Array1(8)
integer:: Array2(8)
integer:: Array3(8)

!Array for x value
do I=8, 1, -1
Array1(I)=MOD(x, 2)
x=x/2
end do

!Array for y value
Do J=8, 1, -1
Array2(J)=MOD(y, 2)
y=y/2

end do
end program ItAllAddsUp


----
Any help would be much appreciated.

Thank you very much:)
 
Not very difficult if you program the reverse transformation :
- translating the two "binary" arrays into integers
- adding the integers
- transforming again integer result into a binary array

The corresponding programming is provided at the end. But I have several additional remarks :

- why doing a loop from 8 to 1 ? the array should be dimensioned to 32 because a normal integer value is coded on 32 bits (even on most 64 bits architectures).

- why do you want to add these binary arrays ? I ask for that question because I generally use a similar technique to register up to 32 logical options into a single integer. So I never "add" these binary representations but compute OR or XOR or AND bit wise operations.

- you should examine carefully intrinsic bit manipulation functions of FORTRAN-2003 like BTEST, IAND, IBCLR, IBITS, IBSET, IEOR, IOR, ISHFT, MVBITS, NOT. For instance, BTEST(value,position) returns a logical value indicating if the bit at a given position is equal to 0 (false) or 1 (true). The position starts from 0 to 31 (it corresponds to the exponent of 2). So your loop from 8 to 1 should be replaced by a loop from 0 to 31 to match the FORTRAN integer model and the arrays could be dimensioned (0:31).

Example to solve your initial request with arrays declared DIMENSION(32) :

Code:
module binary

  implicit none
  
  contains
  
  function integer2binary(i) result(b)
    integer,intent(in) :: i
    integer :: b(32)
    integer k,j
    b=0
    j=i
    do k=1,size(b)
      b(k)=mod(j,2)
      j=j/2
    enddo
  end function
  
  function binary2integer(b) result(i)
    integer :: i
    integer,intent(in) :: b(:)
    integer k,j
    i=0
    j=1
    do k=1,size(b)
      i=i+b(k)*j
      j=j+j
    enddo
  end function
  
  function sum(b1,b2) result(b3)
    integer,intent(in) :: b1(:),b2(:)
    integer :: b3(32)
    b3=integer2binary(binary2integer(b1)+binary2integer(b2))
  end function
  
end module

program test
  use binary
  integer :: i=13,j=23,k
  integer,dimension(32) :: bi,bj,bk
  bi=integer2binary(i)
  write(*,'(32i1)') bi
  write(*,*) binary2integer(bi)
  bj=integer2binary(j)
  bk=sum(bi,bj)
  write(*,*) i+j,binary2integer(bk)
end program
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top