mcocdawc
mcocdawc

Reputation: 1867

Bit sizes of Fortran integers

I have the following program to give a minimal example I don't understand either about the bit representation in general or in Fortran. If I compile with gfortran 7.5 or ifort 18.0 it passes all assertions and I don't understand why.

The function popcnt returns the number of bits set (’1’ bits) in the binary representation of I.

In my understanding the sign of a signed integer is encoded in one bit so if I go from popcnt(n) to popcnt(-n) it should change by one. If I can express n as a power of 2 it should have a popcnt of 1 or 2 (depending on the sign.) What is my error in thinking?

program bit_sizes
    use iso_fortran_env, only: int64
    implicit none
    integer(int64), parameter :: n = -4294967296_int64

    call assert(2_int64 ** 32_int64 == -n)
    call assert(popcnt(-n) == 1)
    call assert(popcnt(n) == 32)

contains

    subroutine assert(cond)
        logical, intent(in) :: cond
        if (.not. cond) error stop
    end subroutine
end program


Upvotes: 2

Views: 571

Answers (2)

Pierre de Buyl
Pierre de Buyl

Reputation: 7293

The error is assuming that the processor will follow that logic :-)

Positive integers follow a simple progression, negative ones have no guarantee with respect to their representation.

I wrote a routine to display bitstrings from Fortran values that takes user input at the command-line. Check the result for yours:

program bit_sizes
  use iso_fortran_env, only: int64
  implicit none
  integer(int64) :: n

  do while (.true.)
     write(*,*) 'enter value'
     read(*,*) n
     write(*,*) 'n', n
     write(*,*) 'popcnt(n)', popcnt(n)
     call print_bitstring(n)
     write(*,*) '-n', -n
     write(*,*) 'popcnt(-n)', popcnt(-n)
     call print_bitstring(-n)
  end do

contains

  subroutine assert(cond)
    logical, intent(in) :: cond
    if (.not. cond) error stop
  end subroutine assert

subroutine print_bitstring(i)
  integer(kind=int64), intent(in) :: i

  integer :: j, n
  character(len=:), allocatable :: bitstring
  n = bit_size(i)
  allocate(character(len=n) :: bitstring)

  do j = 1, n
     if (btest(i,j-1)) then
        bitstring(j:j) = '1'
     else
        bitstring(j:j) = '0'
     end if
  end do

  write(*,*) bitstring

end subroutine print_bitstring

end program bit_sizes

With gfortran on linux 64bit, I have

$ ./bit_sizes 
 enter value
1
 n                    1
 popcnt(n)           1
 1000000000000000000000000000000000000000000000000000000000000000
 -n                   -1
 popcnt(-n)          64
 1111111111111111111111111111111111111111111111111111111111111111
 enter value
2
 n                    2
 popcnt(n)           1
 0100000000000000000000000000000000000000000000000000000000000000
 -n                   -2
 popcnt(-n)          63
 0111111111111111111111111111111111111111111111111111111111111111
 enter value
4294967296
 n           4294967296
 popcnt(n)           1
 0000000000000000000000000000000010000000000000000000000000000000
 -n          -4294967296
 popcnt(-n)          32
 0000000000000000000000000000000011111111111111111111111111111111
 enter value

Upvotes: 3

mcocdawc
mcocdawc

Reputation: 1867

A good friend from electric engineering could help me.

In my understanding the sign of a signed integer is encoded in one bit

This is not generally true.

Alternative representations are:

https://en.wikipedia.org/wiki/Two%27s_complement

Upvotes: 2

Related Questions