ビットレベルの問い合わせと操作#

bge#

名前#

bge(3) - [BIT:COMPARE] ビット単位で大きいか等しい

概要#

    result = bge(i,j)
      elemental logical function bge(i, j)

       integer(kind=**),intent(in) :: i
       integer(kind=**),intent(in) :: j

特性#

  • **と指定された種類は、その型でサポートされている任意の種類です。**

  • ij整数種類は必ずしも同じである必要はありません。さらに、値は、現在のプラットフォームで最も多くのビットを持つ整数種類で有効な値を持つBOZ定数にすることができます。

  • 戻り値は、デフォルトの論理型です。

説明#

bge(3) は、ある整数が別の整数よりビット単位で大きいか等しいかどうかを判定します。

値のビットレベル表現はプラットフォームに依存します。システムのエンディアンネスや、システムが符号の「2の補数」表現を使用するかどうかなどによって、結果が影響を受ける可能性があります。

BOZ定数(2進数、8進数、16進数)には独自の種類またはがないため、整数型に変換されるときに切り捨てられる可能性があることに注意してください。定数が含むことができる最大ビット数は、コンパイルでサポートされている任意の整数種類で表現できる最大ビット数によって制限されます。

ビットシーケンス比較#

長さが異なるビットシーケンスを比較する場合、短いシーケンスは、利用可能な整数の種類がサポートする最大ビット数まで、左側にゼロビットでパディングされます。

ビットシーケンスは、左から右に、一度に1ビットずつ比較されます。不等なビットが見つかるか、すべてのビットが比較されて等しいとわかった場合に終了します。

ビットは常にこの順序で評価されます。必ずしもMSBからLSB(最上位ビットから最下位ビット)とは限りません。

不等なビットが見つかった場合、不等な位置にゼロがあるシーケンスは、不等な位置に1があるシーケンスよりも小さいと見なされます。

オプション#

  • i

    値のビット表現に基づいて、j 以上かどうかをテストする値。

  • j

    i と比較する値。

結果#

ij よりビット単位で大きい場合は.true.を、そうでない場合は.false.を返します。

#

サンプルプログラム

program demo_bge
use,intrinsic :: iso_fortran_env, only : int8, int16, int32, int64
implicit none
integer            :: i
integer(kind=int8) :: byte
integer(kind=int8),allocatable :: arr1(:), arr2(:)

  ! BASIC USAGE
   write(*,*)'bge(-127,127)=',bge( -127, 127 )
   ! on (very common) "two's complement" machines that are
   ! little-endian -127 will be greater than 127

   ! BOZ constants
   ! BOZ constants are subject to truncation, so make sure
   ! your values are valid for the integer kind being compared to
   write(*,*)'bge(b"0001",2)=',bge( b"1", 2)

  ! ELEMENTAL
   ! an array and scalar
   write(*, *)'compare array of values [-128, -0, +0, 127] to 127'
   write(*, *)bge(int([-128, -0, +0, 127], kind=int8), 127_int8)

   ! two arrays
   write(*, *)'compare two arrays'
   arr1=int( [ -127, -0, +0,  127], kind=int8 )
   arr2=int( [  127,  0,  0, -127], kind=int8 )
   write(*,*)'arr1=',arr1
   write(*,*)'arr2=',arr2
   write(*, *)'bge(arr1,arr2)=',bge( arr1, arr2 )

  ! SHOW TESTS AND BITS
   ! actually looking at the bit patterns should clarify what affect
   ! signs have ...
   write(*,*)'Compare some one-byte values to 64.'
   write(*,*)'Notice that the values are tested as bits not as integers'
   write(*,*)'so the results are as if values are unsigned integers.'
   do i=-128,127,32
      byte=i
      write(*,'(sp,i0.4,*(1x,1l,1x,b0.8))')i,bge(byte,64_int8),byte
   enddo

  ! SIGNED ZERO
   ! are +0 and -0 the same on your platform? When comparing at the
   ! bit level this is important
   write(*,'("plus zero=",b0)')  +0
   write(*,'("minus zero=",b0)') -0

end program demo_bge

結果

整数の値がビットレベルでどのように表現されるかは異なる場合があります。これらは、今日の最も一般的なプラットフォームで予想される値にすぎません…

    > bge(-127,127)= T
    > bge(b"0001",2)= F
    > compare array of values [-128, -0, +0, 127] to 127
    > T F F T
    > compare two arrays
    > arr1= -127    0    0  127
    > arr2=  127    0    0 -127
    > bge(arr1,arr2)= T T T F
    > Compare some one-byte values to 64.
    > Notice that the values are tested as bits not as integers
    > so the results are as if values are unsigned integers.
    > -0128  T 10000000
    > -0096  T 10100000
    > -0064  T 11000000
    > -0032  T 11100000
    > +0000  F 00000000
    > +0032  F 00100000
    > +0064  T 01000000
    > +0096  T 01100000
    > plus zero=0
    > minus zero=0

標準#

Fortran 2008

関連項目#

bgt(3)ble(3)blt(3)

fortran-lang 固有関数の説明 (ライセンス: MIT) @urbanjost

bgt#

名前#

bgt(3) - [BIT:COMPARE] ビット単位で大きい

概要#

    result = bgt(i, j)
      elemental logical function bgt(i, j)

       integer(kind=**),intent(in) :: i
       integer(kind=**),intent(in) :: j

特性#

  • i整数またはbozリテラル定数です。

  • j整数またはbozリテラル定数です。

  • **と指定された種類は、その型でサポートされている任意の種類です。**i と**j** の整数種類は必ずしも同じである必要はありません。さらに、値は、現在のプラットフォームで最も多くのビットを持つ整数種類で有効な値を持つBOZ定数にすることができます。

  • 戻り値は、デフォルトの種類の論理型です。

説明#

bgt は、ある整数が別の整数よりビット単位で大きいかどうかを判定します。値のビットレベル表現はプラットフォームに依存します。

オプション#

  • i

    比較対象の参照値

  • j

    i と比較する値

結果#

戻り値は、デフォルトの種類の論理型です。iで表されるビットのシーケンスがjで表されるビットのシーケンスより大きい場合、結果はtrueになり、そうでない場合はfalseになります。

ビットは右から左に比較されます。

#

サンプルプログラム

program demo_bgt
use,intrinsic :: iso_fortran_env, only : int8, int16, int32, int64
implicit none
integer            :: i
integer(kind=int8) :: byte
  ! Compare some one-byte values to 64.
   ! Notice that the values are tested as bits not as integers
   ! so sign bits in the integer are treated just like any other
   write(*,'(a)') 'we will compare other values to 64'
   i=64
   byte=i
   write(*,'(sp,i0.4,*(1x,1l,1x,b0.8))')i,bgt(byte,64_int8),byte

   write(*,'(a)') "comparing at the bit level, not as whole numbers."
   write(*,'(a)') "so pay particular attention to the negative"
   write(*,'(a)') "values on this two's complement platform ..."
   do i=-128,127,32
      byte=i
      write(*,'(sp,i0.4,*(1x,1l,1x,b0.8))')i,bgt(byte,64_int8),byte
   enddo

   ! see the BGE() description for an extended description
   ! of related information

end program demo_bgt

結果

 > we will compare other values to 64
 > +0064  F 01000000
 > comparing at the bit level, not as whole numbers.
 > so pay particular attention to the negative
 > values on this two's complement platform ...
 > -0128  T 10000000
 > -0096  T 10100000
 > -0064  T 11000000
 > -0032  T 11100000
 > +0000  F 00000000
 > +0032  F 00100000
 > +0064  F 01000000
 > +0096  T 01100000

標準#

Fortran 2008

関連項目#

bge(3)ble(3)blt(3)

fortran-lang 固有関数の説明 (ライセンス: MIT) @urbanjost

ble#

名前#

ble(3) - [BIT:COMPARE] ビット単位で小さいか等しい

概要#

    result = ble(i,j)
     elemental logical function ble(i, j)

      integer(kind=**),intent(in) :: i
      integer(kind=**),intent(in) :: j

特性#

  • ij は、サポートされている任意の整数種類にすることができ、必ずしも同じである必要はありません。ただし、値は、現在のプラットフォームで最も多くのビットを持つ整数種類で有効な値を持つBOZ定数にすることができます。

  • 戻り値は、デフォルトの種類の論理スカラーです。

説明#

ble(3) は、ある整数が別の整数よりビット単位で小さいか等しいかどうかを判定します。短い値は、長い値の長さまで左側にゼロでパディングされると仮定します。

オプション#

  • i

    j と比較する値

  • j

    i 以下であるかどうかをテストする値

結果#

j の任意のビットがi の任意のビットより小さい場合、右端のビットから始め、左方向にテストを継続して、戻り値は.true.になります。

#

サンプルプログラム

program demo_ble
use,intrinsic :: iso_fortran_env, only : int8, int16, int32, int64
implicit none
integer            :: i
integer(kind=int8) :: byte
  ! Compare some one-byte values to 64.
   ! Notice that the values are tested as bits not as integers
   ! so sign bits in the integer are treated just like any other
   do i=-128,127,32
      byte=i
      write(*,'(sp,i0.4,*(1x,1l,1x,b0.8))')i,ble(byte,64_int8),byte
      write(*,'(sp,i0.4,*(4x,b0.8))')64_int8,64_int8
   enddo

   ! see the BGE() description for an extended description
   ! of related information

end program demo_ble

結果

   -0128  F 10000000
   +0064    01000000
   -0096  F 10100000
   +0064    01000000
   -0064  F 11000000
   +0064    01000000
   -0032  F 11100000
   +0064    01000000
   +0000  T 00000000
   +0064    01000000
   +0032  T 00100000
   +0064    01000000
   +0064  T 01000000
   +0064    01000000
   +0096  F 01100000
   +0064    01000000

標準#

Fortran 2008

関連項目#

bge(3)bgt(3)blt(3)

fortran-lang 固有関数の説明 (ライセンス: MIT) @urbanjost

blt#

名前#

blt(3) - [BIT:COMPARE] ビット単位で小さい

概要#

    result = blt(i,j)
     elemental logical function blt(i, j)

      integer(kind=**),intent(in) :: i
      integer(kind=**),intent(in) :: j

特性#

  • i は、任意の種類の整数または BOZ リテラル定数です。

  • j は、任意の種類の整数または BOZ リテラル定数です。i と同じである必要はありません。

  • 結果は、デフォルトの論理型です。

BOZ 定数は、現在のプラットフォームで最も多くのビットを持つ整数型で使用できる有効な値を持つ必要があります。

説明#

blt(3) は、ある整数が別の整数よりもビット単位で小さいかどうかを判定します。

オプション#

  • i

    整数型または BOZ リテラル定数である必要があります。

  • j

    整数型または BOZ 定数である必要があります。

結果#

戻り値は、デフォルトの種類の論理型です。

#

サンプルプログラム

program demo_blt
use,intrinsic :: iso_fortran_env, only : int8, int16, int32, int64
implicit none
integer            :: i
integer(kind=int8) :: byte
  ! Compare some one-byte values to 64.
   ! Notice that the values are tested as bits not as integers
   ! so sign bits in the integer are treated just like any other
   do i=-128,127,32
      byte=i
      write(*,'(sp,i0.4,*(1x,1l,1x,b0.8))')i,blt(byte,64_int8),byte
   enddo
  ! BOZ literals
   write(*,*)blt(z'1000', z'101011010')
   ! see the BGE() description for an extended description
   ! of related information

end program demo_blt

結果

   > -0128  F 10000000
   > -0096  F 10100000
   > -0064  F 11000000
   > -0032  F 11100000
   > +0000  T 00000000
   > +0032  T 00100000
   > +0064  F 01000000
   > +0096  F 01100000
   > T

標準#

Fortran 2008

関連情報#

bge(3)bgt(3)ble(3)

fortran-lang 固有関数の説明 (ライセンス: MIT) @urbanjost

bit_size#

名前#

bit_size(3) - [ビット:照会] ビットサイズ照会関数

概要#

    result = bit_size(i)
     integer(kind=KIND) function bit_size(i)

      integer(kind=KIND),intent(in) :: i(..)

特性#

  • i は整数型である必要があります。スカラーまたは配列です。

  • KIND の値は、プロセッサ上の整数型パラメーターの有効な値です。

  • 戻り値は、入力値と同じ型のスカラーです。

説明#

bit_size(3) は、整数 i の型によって表されるビット数(整数精度と符号ビット)を返します。

オプション#

  • i

    ビット数を求める任意の種類の整数値。引数の型だけが検査されるため、引数を定義する必要はありません。i はスカラーまたは配列にすることができますが、単一の要素を表すスカラーが常に返されます。

結果#

結果は、i の型と種類 の値を表すために使用されるビット数です。結果は、i と同じ種類の整数スカラーです。

#

サンプルプログラム

program demo_bit_size
use,intrinsic :: iso_fortran_env, only : int8, int16, int32, int64
use,intrinsic :: iso_fortran_env, only : integer_kinds
implicit none
character(len=*),parameter   :: fmt=&
& '(a,": bit size is ",i3," which is kind=",i3," on this platform")'

    ! default integer bit size on this platform
    write(*,fmt) "default", bit_size(0), kind(0)

    write(*,fmt) "int8   ", bit_size(0_int8),   kind(0_int8)
    write(*,fmt) "int16  ", bit_size(0_int16),  kind(0_int16)
    write(*,fmt) "int32  ", bit_size(0_int32),  kind(0_int32)
    write(*,fmt) "int64  ", bit_size(0_int64),  kind(0_int64)

    write(*,'(a,*(i0:,", "))') "The available kinds are ",integer_kinds

end program demo_bit_size

代表的な結果

    default: bit size is  32 which is kind=  4 on this platform
    int8   : bit size is   8 which is kind=  1 on this platform
    int16  : bit size is  16 which is kind=  2 on this platform
    int32  : bit size is  32 which is kind=  4 on this platform
    int64  : bit size is  64 which is kind=  8 on this platform
    The available kinds are 1, 2, 4, 8, 16

標準#

Fortran 95

関連情報#

****(3)

fortran-lang 固有関数の説明 (ライセンス: MIT) @urbanjost

btest#

名前#

btest(3) - [ビット:照会] 整数値のビットをテストします。

概要#

    result = btest(i,pos)
     elemental logical function btest(i,pos)

      integer(kind=**),intent(in)  :: i
      integer(kind=**),intent(in)  :: pos

特性#

  • i は任意の種類の整数です。

  • pos は任意の種類の整数です。

  • 結果はデフォルトの論理型です。

説明#

btest(3) は、ipos ビット目が 1 に設定されている場合、論理値 .true. を返します。位置 0 は最下位ビットです。ビット位置は、右から左に bitsize(i)-1 まで増加します。

オプション#

  • i

    テストするビットを含む整数

  • pos

    照会するビットの位置。i の有効な位置である必要があります。つまり、0 <= pos <= bit_size(i) です。

結果#

結果は、i のビット位置 pos の値が 1 の場合は論理値 .true.0 の場合は .false. となる論理型です。

シーケンス内のビットの位置は、右から左に番号が付けられ、最右端のビットの位置は 0 です。

#

サンプルプログラム

program demo_btest
implicit none
integer :: i, j, pos, a(2,2)
logical :: bool
character(len=*),parameter :: g='(*(g0))'

     i = 32768 + 1024 + 64
    write(*,'(a,i0,"=>",b32.32,/)')'Looking at the integer: ',i

    ! looking one bit at a time from LOW BIT TO HIGH BIT
    write(*,g)'from bit 0 to bit ',bit_size(i),'==>'
    do pos=0,bit_size(i)-1
        bool = btest(i, pos)
        write(*,'(l1)',advance='no')bool
    enddo
    write(*,*)

    ! a binary format the hard way.
    ! Note going from bit_size(i) to zero.
    write(*,*)
    write(*,g)'so for ',i,' with a bit size of ',bit_size(i)
    write(*,'(b32.32)')i
    write(*,g)merge('^','_',[(btest(i,j),j=bit_size(i)-1,0,-1)])
    write(*,*)
    write(*,g)'and for ',-i,' with a bit size of ',bit_size(i)
    write(*,'(b32.32)')-i
    write(*,g)merge('^','_',[(btest(-i,j),j=bit_size(i)-1,0,-1)])

    ! elemental:
    !
    a(1,:)=[ 1, 2 ]
    a(2,:)=[ 3, 4 ]
    write(*,*)
    write(*,'(a,/,*(i2,1x,i2,/))')'given the array a ...',a
    ! the second bit of all the values in a
    write(*,'(a,/,*(l2,1x,l2,/))')'the value of btest (a, 2)',btest(a,2)
    ! bits 1,2,3,4 of the value 2
    write(*,'(a,/,*(l2,1x,l2,/))')'the value of btest (2, a)',btest(2,a)
end program demo_btest

結果

  > Looking at the integer: 33856=>11111111111111110111101111000000
  >
  > 00000000000000001000010001000000
  > 11111111111111110111101111000000
  > 1000010001000000
  > 11111111111111110111101111000000
  > from bit 0 to bit 32==>
  > FFFFFFTFFFTFFFFTFFFFFFFFFFFFFFFF
  >
  > so for 33856 with a bit size of 32
  > 00000000000000001000010001000000
  > ________________^____^___^______
  >
  > and for -33856 with a bit size of 32
  > 11111111111111110111101111000000
  > ^^^^^^^^^^^^^^^^_^^^^_^^^^______
  >
  > given the array a ...
  >  1  3
  >  2  4
  >
  > the value of btest (a, 2)
  >  F  F
  >  F  T
  >
  > the value of btest (2, a)
  >  T  F
  >  F  F

標準#

Fortran 95

関連情報#

ieor(3)ibclr(3)not(3)ibclr(3)ibits(3)ibset(3)iand(3)ior(3)ieor(3)mvbits(3)

fortran-lang 固有関数の説明 (ライセンス: MIT) @urbanjost

storage_size#

名前#

storage_size(3) - [ビット:照会] ビット単位のストレージサイズ

概要#

    result = storage_size(a [,KIND] )
     integer(kind=KIND) storage_size(a,KIND)

      type(TYPE(kind=**)) :: a
      integer,intent(in),optional :: KIND

特性#

  • **と指定された種類は、その型でサポートされている任意の種類です。**

  • a は任意の型と種類にすることができます。多相型の場合は、未定義のポインターであってはなりません。無制限多相型であるか、または遅延型パラメーターを持つ場合は、未割り当ての割り当て可能変数または関連付け解除された、または未定義のポインターであってはなりません。

  • 返される値の種類型パラメーターは、kind の値で指定されたものです。それ以外の場合は、デフォルトの整数型の型パラメーターです。

  • kind が指定されていない限り、結果はデフォルトの種類の整数スカラーです。kind が指定されている場合は、kind で指定された種類になります。

説明#

storage_size(3) は、引数 a のストレージサイズをビット単位で返します。

オプション#

  • a

    ストレージサイズを決定するエンティティ

  • kind

    出力値の種類を定義するスカラー整数定数式。

結果#

結果値は、動的な型と型パラメーターが a と同じである配列の要素のサイズ(ビット単位)です。

型と型パラメーターがストレージ関連付けに適用される場合は、結果は、組込みモジュール ISO_FORTRAN_ENV で定義されている名前付き定数と一貫しています。

注記1

配列要素の格納には、単純なスカラー変数には適用されない可能性のあるハードウェアによって課せられた配列要素のアライメント要件のために、「型」がさらに多くのビットが必要になる場合があります。

注記2

これは、オブジェクトが格納されるときにメモリに占めるサイズを意図したものです。これは、式処理中に占めるサイズ(ネイティブレジスタサイズである可能性があります)やファイルに格納されるときのサイズとは異なる場合があります。オブジェクトがメモリに格納されることがなく、レジスタのみに格納される場合でも、この関数はメモリに格納された場合のサイズを返します。

#

サンプルプログラム

program demo_storage_size
implicit none

   ! a default real, integer, and logical are the same storage size
   write(*,*)'size of integer       ',storage_size(0)
   write(*,*)'size of real          ',storage_size(0.0)
   write(*,*)'size of logical       ',storage_size(.true.)
   write(*,*)'size of complex       ',storage_size((0.0,0.0))

   ! note the size of an element of the array, not the storage size of
   ! the entire array is returned for array arguments
   write(*,*)'size of integer array ',storage_size([0,1,2,3,4,5,6,7,8,9])

end program demo_storage_size

結果

    size of integer                 32
    size of real                    32
    size of logical                 32
    size of complex                 64
    size of integer array           32

標準#

Fortran 2008

関連情報#

c_sizeof(3)

fortran-lang 組込み記述

leadz#

名前#

leadz(3) - [ビット:カウント] 整数の先頭のゼロビットの数

概要#

    result = leadz(i)
     elemental integer function leadz(i)

      integer(kind=**),intent(in) :: i

特性#

  • i は任意の種類の整数です。

  • 戻り値は、デフォルトの整数型です。

説明#

leadz(3) は、整数の先頭のゼロビット数を返します。

オプション#

  • i

    先頭のゼロビットをカウントする整数

結果#

入力値の種類を考慮した先頭のゼロビットの数。i のすべてのビットが 0 の場合、結果値は bit_size(i) になります。

結果は、k が入力 i の最左端の 1 ビットの位置である場合、bit_size(i)-1-k と考えることもできます。位置は 0 から bit-size() までで、最右端のビットが 0 です。

#

サンプルプログラム

program demo_leadz
implicit none
integer :: value, i
character(len=80) :: f

  ! make a format statement for writing a value as a bit string
  write(f,'("(b",i0,".",i0,")")')bit_size(value),bit_size(value)

  ! show output for various integer values
  value=0
  do i=-150, 150, 50
     value=i
     write (*,'("LEADING ZERO BITS=",i3)',advance='no') leadz(value)
     write (*,'(" OF VALUE ")',advance='no')
     write(*,f,advance='no') value
     write(*,'(*(1x,g0))') "AKA",value
  enddo
  ! Notes:
  ! for two's-complements programming environments a negative non-zero
  ! integer value will always start with a 1 and a positive value with 0
  ! as the first bit is the sign bit. Such platforms are very common.
end program demo_leadz

結果

  LEADING ZERO BITS=  0 OF VALUE 11111111111111111111111101101010 AKA -150
  LEADING ZERO BITS=  0 OF VALUE 11111111111111111111111110011100 AKA -100
  LEADING ZERO BITS=  0 OF VALUE 11111111111111111111111111001110 AKA -50
  LEADING ZERO BITS= 32 OF VALUE 00000000000000000000000000000000 AKA 0
  LEADING ZERO BITS= 26 OF VALUE 00000000000000000000000000110010 AKA 50
  LEADING ZERO BITS= 25 OF VALUE 00000000000000000000000001100100 AKA 100
  LEADING ZERO BITS= 24 OF VALUE 00000000000000000000000010010110 AKA 150

標準#

Fortran 2008

関連情報#

bit_size(3)popcnt(3)poppar(3)trailz(3)

fortran-lang 固有関数の説明 (ライセンス: MIT) @urbanjost

popcnt#

名前#

popcnt(3) - [ビット:カウント] 設定されたビットの数

概要#

    result = popcnt(i)
     elemental integer function popcnt(i)

      integer(kind=KIND), intent(in) :: i

特性#

  • i は任意の種類の整数です。

  • 戻り値は、デフォルトの整数型の整数です。

説明#

popcnt(3) は、整数の2進表現で 1 に設定されているビット数を返します。

オプション#

  • i

    設定されたビットをカウントする値

結果#

i で 1 に設定されているビットの数。

#

サンプルプログラム

program demo_popcnt
use, intrinsic :: iso_fortran_env, only : integer_kinds, &
   & int8, int16, int32, int64
implicit none
character(len=*),parameter :: pretty='(b64,1x,i0)'
  ! basic usage
   print pretty, 127,     popcnt(127)
   print pretty, int(b"01010"), popcnt(int(b"01010"))

  ! any kind of an integer can be used
   print pretty, huge(0_int8),  popcnt(huge(0_int8))
   print pretty, huge(0_int16), popcnt(huge(0_int16))
   print pretty, huge(0_int32), popcnt(huge(0_int32))
   print pretty, huge(0_int64), popcnt(huge(0_int64))
end program demo_popcnt

結果

ほとんどのマシンでは、最上位ビットが符号ビットであり、正の値には0が使用されますが、これはシステムに依存することに注意してください。 これらは、huge(3f)関数が最上位ビット以外をすべて1に設定した典型的な値です。

 >                                                         1111111 7
 >                                                            1010 2
 >                                                         1111111 7
 >                                                 111111111111111 15
 >                                 1111111111111111111111111111111 31
 > 111111111111111111111111111111111111111111111111111111111111111 63

標準#

Fortran 2008

参照#

ビットレベルでオペランドまたはクエリ値を操作する手順は多数あります。

poppar(3)leadz(3)trailz(3) atomic_and(3)atomic_fetch_and(3)atomic_fetch_or(3)atomic_fetch_xor(3)atomic_or(3)atomic_xor(3)bge(3)bgt(3)bit_size(3)ble(3)blt(3)btest(3)dshiftl(3)dshiftr(3)iall(3)iand(3)iany(3)ibclr(3)ibits(3)ibset(3)ieor(3)ior(3)iparity(3)ishftc(3)ishft(3)maskl(3)maskr(3)merge_bits(3)mvbits(3)not(3)shifta(3)shiftl(3)shiftr(3)storage_size(3)

fortran-lang 固有関数の説明 (ライセンス: MIT) @urbanjost

poppar#

名前#

poppar(3) - [BIT:COUNT] 設定されたビット数のパリティ

概要#

    result = poppar(i)
     elemental integer function poppar(i)

      integer(kind=KIND), intent(in) :: i

特性#

  • i は任意の種類の整数です。

  • 戻り値はデフォルトの種類の整数です。

説明#

poppar(3) は、整数のバイナリ表現のパリティ(つまり、設定されたビット数のパリティ)を返します。

パリティは次のように表現されます。

  • i のビットが偶数個設定されている場合、0(ゼロ)。

  • 1に設定されたビット数が奇数の場合、1(1)。

オプション#

  • i

    ビットパリティを照会する値

結果#

i に設定されているビット数が偶数の場合、戻り値は0、奇数の場合1になります。

#

サンプルプログラム

program demo_poppar
use, intrinsic :: iso_fortran_env, only : integer_kinds, &
   & int8, int16, int32, int64
implicit none
character(len=*),parameter :: pretty='(b64,1x,i0)'
   ! basic usage
   print pretty, 127,     poppar(127)
   print pretty, 128,     poppar(128)
   print pretty, int(b"01010"), poppar(int(b"01010"))

   ! any kind of an integer can be used
   print pretty, huge(0_int8),  poppar(huge(0_int8))
   print pretty, huge(0_int16), poppar(huge(0_int16))
   print pretty, huge(0_int32), poppar(huge(0_int32))
   print pretty, huge(0_int64), poppar(huge(0_int64))
end program demo_poppar

結果

 >                                                          1111111 1
 >                                                         10000000 1
 >                                                             1010 0
 >                                  1111111111111111111111111111111 1
 >                                                          1111111 1
 >                                                  111111111111111 1
 >                                  1111111111111111111111111111111 1
 >  111111111111111111111111111111111111111111111111111111111111111 1

標準#

Fortran 2008

参照#

ビットレベルでオペランドまたはクエリ値を操作する手順は多数あります。

popcnt(3)leadz(3)trailz(3) atomic_and(3)atomic_fetch_and(3)atomic_fetch_or(3)atomic_fetch_xor(3)atomic_or(3)atomic_xor(3)bge(3)bgt(3)bit_size(3)ble(3)blt(3)btest(3)dshiftl(3)dshiftr(3)iall(3)iand(3)iany(3)ibclr(3)ibits(3)ibset(3)ieor(3)ior(3)iparity(3)ishftc(3)ishft(3)maskl(3)maskr(3)merge_bits(3)mvbits(3)not(3)shifta(3)shiftl(3)shiftr(3)storage_size(3)

fortran-lang 固有関数の説明 (ライセンス: MIT) @urbanjost

trailz#

名前#

trailz(3) - [BIT:COUNT] 整数の末尾のゼロビット数

概要#

 result = trailz(i)
  elemental integer function trailz(i)

   integer(kind=**),intent(in) :: i

特性#

  • i は任意の種類の整数です。

  • 結果はデフォルトの種類の整数です。

説明#

trailz(3) は、整数値の末尾のゼロビット数を返します。

オプション#

  • i

    末尾のゼロビットを数える値

結果#

最後の非ゼロビットの後の整数値における、右端からの末尾のゼロビット数。

       >      right-most non-zero bit
       >                 V
       >  |0|0|0|1|1|1|0|1|0|0|0|0|0|0|
       >  ^               |___________| trailing zero bits
       >   bit_size(i)

i のすべてのビットがゼロの場合、結果は入力値のビットサイズ(つまり、bit_size(i))になります。

結果は、右端のビットを0として左から数えた場合のiにおける最右端の1ビットの位置と見なすこともできます。

#

サンプルプログラム

program demo_trailz

! some common integer kinds
use, intrinsic :: iso_fortran_env, only : &
 & integer_kinds, int8, int16, int32, int64

implicit none

! a handy format
character(len=*),parameter :: &
 & show = '(1x,"value=",i4,", value(bits)=",b32.32,1x,", trailz=",i3)'

integer(kind=int64) :: bigi
  ! basics
   write(*,*)'Note default integer is',bit_size(0),'bits'
   print  show,  -1, -1,  trailz(-1)
   print  show,   0,  0,  trailz(0)
   print  show,   1,  1,  trailz(1)
   print  show,  96, 96,  trailz(96)
  ! elemental
   print *, 'elemental and any integer kind:'
   bigi=2**5
   write(*,*) trailz( [ bigi, bigi*256, bigi/2 ] )
   write(*,'(1x,b64.64)')[ bigi, bigi*256, bigi/2 ]

end program demo_trailz

結果

    Note default integer is          32 bits
    value=  -1, value(bits)=11111111111111111111111111111111 , trailz=  0
    value=   0, value(bits)=00000000000000000000000000000000 , trailz= 32
    value=   1, value(bits)=00000000000000000000000000000001 , trailz=  0
    value=  96, value(bits)=00000000000000000000000001100000 , trailz=  5
    elemental and any integer kind:
              5          13           4
    0000000000000000000000000000000000000000000000000000000000100000
    0000000000000000000000000000000000000000000000000010000000000000
    0000000000000000000000000000000000000000000000000000000000010000

標準#

Fortran 2008

参照#

bit_size(3)popcnt(3)poppar(3)leadz(3)

fortran-lang 固有関数の説明 (ライセンス: MIT) @urbanjost

dshiftl#

名前#

dshiftl(3) - [BIT:COPY] 2つの整数のビットの組み合わせ左シフト

概要#

    result = dshiftl(i, j, shift)
     elemental integer(kind=KIND) function dshiftl(i, j, shift)

      integer(kind=KIND),intent(in) :: i
      integer(kind=KIND),intent(in) :: j
      integer(kind=**),intent(in) :: shift

特性#

  • ij、および戻り値の種類は同じです。ただし、ijのいずれかがBOZリテラル定数の場合を除きます(BOZリテラル定数とは、2進数、8進数、または16進数の定数です)。

  • iまたはjのいずれかがBOZリテラル定数である場合(ただし両方ではない場合)、組込み関数int(3)によって、もう一方のkind型パラメータを持つ整数型に変換されます。

  • **と指定された種類は、その型でサポートされている任意の種類です。**

説明#

dshiftl(3) は、ijのビットを組み合わせます。結果の最下位shiftビットはjの最上位shiftビットであり、残りのビットはiの最下位bitsize(i)-shiftビットです。

したがって、dshiftlは「組み合わせ左シフト」と呼ばれます。これは、ijを連結し、shiftビットだけ左にシフトし、iまたはjと同じビット数を保持するようなものです。

たとえば、2つの16ビット値の場合、shift=6であれば

      SHIFT=6
      I =             1111111111111111
      J =             0000000000000000
      COMBINED        11111111111111110000000000000000
      DROP LEFT BITS  11111111110000000000000000
      KEEP LEFT 16    1111111111000000

注記#

これは次と同等です。

     ior( shiftl(i, shift), shiftr(j, bit_size(j) - shift) )

また、この演算の最後の表現を使用すると、ijの両方が同じ値を持つ場合、つまり

      dshiftl(i, i, shift)

結果は円形シフトと同じ値になります。

      ishftc(i, shift)

オプション#

  • i

    組み合わせパターン内の左側のビットパターンを定義するために使用されます。

  • j

    組み合わせパターン内の右側のビットパターンに使用されます。

  • shift

    非負であり、整数入力値のビット数(つまり、BOZリテラル定数ではない方のビットサイズ)以下である必要があります。

結果#

jの最上位shiftビットが結果の最下位ビットにコピーされ、残りのビットはiの最下位ビットになります。

#

サンプルプログラム

program demo_dshiftl
use,intrinsic :: iso_fortran_env, only : int8, int16, int32, int64
implicit none
integer(kind=int32) :: i, j
integer             :: shift

  ! basic usage
   write(*,*) dshiftl (1, 2**30, 2) ! int32 values on little-endian => 5

  ! print some simple calls as binary to better visual the results
   i=-1
   j=0
   shift=5
   call printit()

   ! the leftmost SHIFT bits of J are copied to the rightmost result bits
   j=int(b"11111000000000000000000000000000")
   ! and the other bits are the rightmost bits of I
   i=int(b"00000000000000000000000000000000")
   call printit()

   j=int(b"11111000000000000000000000000000")
   i=int(b"00000111111111111111111111111111")
   ! result should be all 1s
   call printit()

contains
subroutine printit()
   ! print i,j,shift and then i,j, and the result as binary values
    write(*,'(*(g0))')'I=',i,' J=',j,' SHIFT=',shift
    write(*,'(b32.32)') i,j, dshiftl (i, j, shift)
end subroutine printit

end program demo_dshiftl

結果

   > I=-1 J=0 SHIFT=5
   > 11111111111111111111111111111111
   > 00000000000000000000000000000000
   > 11111111111111111111111111100000
   > I=0 J=-134217728 SHIFT=5
   > 00000000000000000000000000000000
   > 11111000000000000000000000000000
   > 00000000000000000000000000011111
   > I=134217727 J=-134217728 SHIFT=5
   > 00000111111111111111111111111111
   > 11111000000000000000000000000000
   > 11111111111111111111111111111111

標準#

Fortran 2008

参照#

dshiftr(3)

fortran-lang 固有関数の説明 (ライセンス: MIT) @urbanjost

dshiftr#

名前#

dshiftr(3) - [BIT:COPY] 2つの整数のビットの組み合わせ右シフト

概要#

    result = dshiftr(i, j, shift)
     elemental integer(kind=KIND) function dshiftr(i, j, shift)

      integer(kind=KIND),intent(in) :: i
      integer(kind=KIND),intent(in) :: j
      integer(kind=**),intent(in) :: shift

特性#

  • **と指定されたkindは、整数型の任意のkind値にすることができます。

  • ij、および戻り値の種類は同じです。ただし、ijのいずれかがBOZリテラル定数の場合を除きます(BOZリテラル定数とは、2進数、8進数、または16進数の定数です)。

  • iまたはjのいずれかがBOZリテラル定数である場合、組込み関数int(3)によって、もう一方のkind型パラメータを持つ整数型に変換されます。

説明#

dshiftr(3) は、ijのビットを組み合わせます。結果の最上位shiftビットはiの最下位shiftビットであり、残りのビットはjの最上位ビットです。

これは、ijのビットを連結し、右端のshiftビットを削除し、入力値と同じ数の右端のビットを保持することとして考えることができます。そのため、「結合右シフト」という名前が付けられています。

アルファベット順にラベル付けされた2つの16ビット値が与えられた場合…

   i=ABCDEFGHIJKLMNOP
   j=abcdefghijklmnop

それらを連結する

   ABCDEFGHIJKLMNOPabcdefghijklmnop

N=6ビットだけ右にシフトし、ビットを削除する

         ABCDEFGHIJKLMNOPabcdefghij

右端の16ビットを保持する

                   KLMNOPabcdefghij

注記#

dshifr(i,j,shift) は、次のと同等です。

     ior(shiftl (i, bit_size(i) - shift), shiftr(j, shift) )

ijが同じ値を持つ場合も、

     dshiftr( i, i, shift )

これは負の巡回シフトと同じ結果になります。

     ishftc( i,   -shift ).

オプション#

  • i

    結合右シフトされる値のペアの左側の値

  • j

    結合右シフトされる値のペアの右側の値

  • shift

    シフト値は負ではなく、bit_size(3)で計算できる入力値のビット数以下です。

結果#

結果は、ijの結合右シフトであり、入力のビットパターンが左から右に結合され、右端のshiftビットが削除され、その後、右端のビットから入力値と同じ数のビットが保持されるものと同じです。

#

サンプルプログラム

program demo_dshiftr
use,intrinsic :: iso_fortran_env, only : int8, int16, int32, int64
implicit none
integer(kind=int32) :: i, j
integer             :: shift

  ! basic usage
   write(*,*) dshiftr (1, 2**30, 2)

  ! print some calls as binary to better visualize the results
   i=-1
   j=0
   shift=5

   ! print values
    write(*,'(*(g0))')'I=',i,' J=',j,' SHIFT=',shift
    write(*,'(b32.32)') i,j, dshiftr (i, j, shift)

  ! visualizing a "combined right shift" ...
   i=int(b"00000000000000000000000000011111")
   j=int(b"11111111111111111111111111100000")
   ! appended together ( i//j )
   ! 0000000000000000000000000001111111111111111111111111111111100000
   ! shifted right SHIFT values dropping off shifted values
   !      00000000000000000000000000011111111111111111111111111111111
   ! keep enough rightmost bits to fill the kind
   !                                 11111111111111111111111111111111
   ! so the result should be all 1s bits ...

    write(*,'(*(g0))')'I=',i,' J=',j,' SHIFT=',shift
    write(*,'(b32.32)') i,j, dshiftr (i, j, shift)

end program demo_dshiftr

結果

 >    1342177280
 >  I=-1 J=0 SHIFT=5
 >  11111111111111111111111111111111
 >  00000000000000000000000000000000
 >  11111000000000000000000000000000
 >  I=31 J=-32 SHIFT=5
 >  00000000000000000000000000011111
 >  11111111111111111111111111100000
 >  11111111111111111111111111111111

標準#

Fortran 2008

参照#

dshiftl(3)

fortran-lang 固有関数の説明 (ライセンス: MIT) @urbanjost

merge_bits#

名前#

merge_bits(3) - [BIT:COPY] マスクを使用してビットをマージする

概要#

    result = merge_bits(i, j, mask)
     elemental integer(kind=KIND) function merge_bits(i,j,mask)

      integer(kind=KIND), intent(in) :: i, j, mask

特性#

  • 結果とすべての入力値は、マスクとiまたはjのいずれかがBOZ定数である場合を除き、同じ整数型とKINDを持ちます。

説明#

三項ラスター演算における一般的なグラフィック操作は、2つの異なるソースからのビットを結合することであり、一般的にビットブレンドと呼ばれます。merge_bits(3)は、mask値のビットを使用して、どの入力値からビットをコピーするかを決定することにより、ijのマスク付きビットブレンドを実行します。

具体的には、結果のk番目のビットは、maskのk番目のビットが1の場合、iのk番目のビットと等しくなります。そうでない場合(したがって、3つの入力値すべてが同じ数のビットを持つ必要があります)、jのk番目のビットと等しくなります。

結果は、次の結果と同じです。

    ior (iand (i, mask),iand (j, not (mask)))

すべての値が同じ整数型であるという例外は、iまたはjと/またはマスクがBOZ定数である可能性があることです(BOZ定数とは、バイナリ、オクタ、または16進数のリテラル定数のいずれかであることを意味します)。BOZ値は、非BOZ値のKINDを使用して固有関数int()によって呼び出されたかのように、非BOZ値の整数型に変換されるため、BOZ値は結果の型の範囲内にある必要があります。

オプション#

  • i

    マスク内の関連ビットが1の場合にビットを選択する値。

  • j

    マスク内の関連ビットが0の場合にビットを選択する値。

  • マスク

    ijからビットを選択するためにマスクとして使用される値

結果#

マスクmaskを使用してijからブレンドされたビット。

#

サンプルプログラム

program demo_merge_bits
use,intrinsic :: iso_fortran_env,  only : int8, int16, int32, int64
implicit none
integer(kind=int16) :: if_one,if_zero,msk
character(len=*),parameter :: fmt='(*(g0, 1X))'

   ! basic usage
   print *,'MERGE_BITS( 5,10,41) should be 3.=>',merge_bits(5,10,41)
   print *,'MERGE_BITS(13,18,22) should be 4.=>',merge_bits(13,18,22)

   ! use some values in base2 illustratively:
   if_one =int(b'1010101010101010',kind=int16)
   if_zero=int(b'0101010101010101',kind=int16)

   msk=int(b'0101010101010101',kind=int16)
   print '("should get all zero bits =>",b16.16)', &
   & merge_bits(if_one,if_zero,msk)

   msk=int(b'1010101010101010',kind=int16)
   print '("should get all ones bits =>",b16.16)', &
   & merge_bits(if_one,if_zero,msk)

   ! using BOZ values
   print fmt, &
   & merge_bits(32767_int16,    o'12345',         32767_int16), &
   & merge_bits(o'12345', 32767_int16, b'0000000000010101'), &
   & merge_bits(32767_int16,    o'12345',             z'1234')

   ! a do-it-yourself equivalent for comparison and validation
   print fmt, &
   & ior(iand(32767_int16, 32767_int16),                   &
   &   iand(o'12345', not(32767_int16))),                  &

   & ior(iand(o'12345', int(o'12345', kind=int16)),        &
   &   iand(32767_int16, not(int(o'12345', kind=int16)))), &

   & ior(iand(32767_int16, z'1234'),                       &
   &   iand(o'12345', not(int( z'1234', kind=int16))))

end program demo_merge_bits

結果

    MERGE_BITS( 5,10,41) should be 3.=>           3
    MERGE_BITS(13,18,22) should be 4.=>           4
   should get all zero bits =>0000000000000000
   should get all ones bits =>1111111111111111
   32767 32751 5877
   32767 32767 5877

標準#

Fortran 2008

参照#

****(3)

fortran-lang 固有関数の説明 (ライセンス: MIT) @urbanjost

mvbits#

名前#

mvbits(3) - [BIT:COPY] 整数で見つかったビットパターンを別の整数に再現する

概要#

   call mvbits(from, frompos, len, to, topos)
    elemental subroutine mvbits( from, frompos, len, to, topos )

     integer(kind=KIND),intent(in)    :: from
     integer(kind=**),intent(in)      :: frompos
     integer(kind=**),intent(in)      :: len
     integer(kind=KIND),intent(inout) :: to
     integer(kind=**),intent(in)      :: topos

特性#

  • from整数です。

  • fromposは整数です。

  • lenは整数です。

  • toは、fromと同じKINDの整数です。

  • toposは整数です。

説明#

mvbits(3)は、整数fromの隣接するビットの範囲に見つかったビットパターンを、別の整数tofromと同じKIND)の指定された位置にコピーします。それ以外のビットはtoのままです。

コピーされるビット位置は、fromの値内に存在する必要があります。つまり、frompos+len-1topos+len-1の値は非負で、bit_size(from)未満である必要があります。

ビットには、右から左に0からbit_size(i)-1の番号が付けられています。

オプション#

  • from

    ビットを読み取る整数

  • frompos

    fromposは、コピーする最初のビットの位置です。これは、bit_size(from)未満の非負の整数値です。

  • len

    fromからコピーするビット数を示す非負の整数値。fromの終端を超えてビットをコピーすることは指定できません。つまり、frompos + lenbit_size(from)以下である必要があります。

  • to

    コピーされたビットを配置する整数変数。fromと同じKINDである必要があり、fromと同じ変数、またはそれに関連付けられた変数にすることもできます。

    toは、fromfromposの位置から始まる長さlenのビットシーケンスを、totoposの位置にコピーすることで設定されます。toの他のビットは変更されません。戻り値では、toposから始まるtoの長さlenのビットは、入力時にfromposから始まるfromの長さlenのビットが持っていた値と等しくなります。

  • topos

    toで、fromからコピーされたビットの開始位置を示す非負の整数値。topos + lenbit_size(to)以下である必要があります。

#

入力値からバイトを逆順で新しい32ビット整数に設定するサンプルプログラム(つまり、整数のエンディアンを変更する)。

program demo_mvbits
use,intrinsic :: iso_fortran_env,  only : int8, int16, int32, int64
implicit none
integer(kind=int32) :: intfrom, intto, abcd_int
character(len=*),parameter :: bits= '(g0,t30,b32.32)'
character(len=*),parameter :: fmt= '(g0,t30,a,t40,b32.32)'

    intfrom=huge(0)  ! all bits are 1 accept the sign bit
    intto=0          ! all bits are 0

    !! CHANGE BIT 0
    ! show the value and bit pattern
    write(*,bits)intfrom,intfrom
    write(*,bits)intto,intto

    ! copy bit 0 from intfrom to intto to show the rightmost bit changes
    !          (from,    frompos, len,    to, topos)
    call mvbits(intfrom,       0,   1, intto,     0) ! change bit 0
    write(*,bits)intto,intto

    !! COPY PART OF A VALUE TO ITSELF
    ! can copy bit from a value to itself
    call mvbits(intfrom,0,1,intfrom,31)
    write(*,bits)intfrom,intfrom

    !! MOVING BYTES AT A TIME
    ! make native integer value with bit patterns
    ! that happen to be the same as the beginning of the alphabet
    ! to make it easy to see the bytes are reversed
    abcd_int=transfer('abcd',0)
    ! show the value and bit pattern
    write(*,*)'native'
    write(*,fmt)abcd_int,abcd_int,abcd_int

    ! change endian of the value
    abcd_int=int_swap32(abcd_int)
    ! show the values and their bit pattern
    write(*,*)'non-native'
    write(*,fmt)abcd_int,abcd_int,abcd_int

 contains

 pure elemental function int_swap32(intin) result(intout)
 ! Convert a 32 bit integer from big Endian to little Endian,
 ! or conversely from little Endian to big Endian.
 !
 integer(kind=int32), intent(in)  :: intin
 integer(kind=int32) :: intout
    ! copy bytes from input value to new position in output value
    !          (from,  frompos, len,     to, topos)
    call mvbits(intin,       0,   8, intout,    24) ! byte1 to byte4
    call mvbits(intin,       8,   8, intout,    16) ! byte2 to byte3
    call mvbits(intin,      16,   8, intout,     8) ! byte3 to byte2
    call mvbits(intin,      24,   8, intout,     0) ! byte4 to byte1
 end function int_swap32

 end program demo_mvbits

結果

   2147483647                   01111111111111111111111111111111
   0                            00000000000000000000000000000000
   1                            00000000000000000000000000000001
   -1                           11111111111111111111111111111111
    native
   1684234849                   abcd      01100100011000110110001001100001
    non-native
   1633837924                   dcba      01100001011000100110001101100100

標準#

Fortran 95

参照#

ieor(3)ibclr(3)not(3)btest(3)ibclr(3)ibits(3)ibset(3)iand(3)ior(3)ieor(3)

fortran-lang 固有関数の説明 (ライセンス: MIT) @urbanjost

ibits#

名前#

ibits(3) - [BIT:COPY] ビットのサブセットを抽出する

概要#

    result = ibits(i, pos, len)
     elemental integer(kind=KIND) function ibits(i,pos,len)

      integer(kind=KIND),intent(in) :: i
      integer(kind=**),intent(in) :: pos
      integer(kind=**),intent(in) :: len

特性#

  • **として指定されたKINDは、サポートされている任意の整数KINDです。

  • iも、サポートされている任意の整数KINDです。

  • 戻り値は、iと同じKINDになります。

説明#

ibits(3)は、ビット位置posから始まり、合計lenビットにわたって左に拡張することで、iからビットのフィールドを抽出します。

次に、結果は右寄せされ、結果の残りの左端のビットはゼロになります。

位置posは、右端のビットがゼロであり、位置が左に増加すると仮定して計算されます。

オプション#

  • i

    ビットを抽出する値

  • pos

    コピーを開始するビットの位置。posは非負です。

  • len

    iからコピーするビット数。非負である必要があります。

pos + lenbit_size(i)以下である必要があります。

結果#

戻り値は、選択されたビットを右寄せし、左側にゼロで埋め込んだものです。

#

サンプルプログラム

program demo_ibits
use,intrinsic :: iso_fortran_env,  only : int8, int16, int32, int64
implicit none
integer(kind=int16) :: i,j
  ! basic usage
   print *,ibits (14, 1, 3) ! should be seven
   print *,ibits(-1,10,3)   ! and so is this
   ! it is easier to see using binary representation
   i=int(b'0101010101011101',kind=int16)
   write(*,'(b16.16,1x,i0)') ibits(i,3,3), ibits(i,3,3)

  ! we can illustrate this as
   !        #-- position 15
   !        |              #-- position 0
   !        |   <-- +len   |
   !        V              V
   !        5432109876543210
   i =int(b'1111111111111111',kind=int16)
   !          ^^^^
   j=ibits(i,10,4) ! start at 10th from left and proceed
                   ! left for a total of 4 characters
   write(*,'(a,b16.16)')'j=',j
  ! lets do something less ambiguous
   i =int(b'0010011000000000',kind=int16)
   j=ibits(i,9,5)
   write(*,'(a,b16.16)')'j=',j
end program demo_ibits

結果

 > 7
 > 7
 > 0000000000000011 3
 > j=0000000000001111
 > j=0000000000010011

標準#

Fortran 95

参照#

ieor(3)ibclr(3)not(3)btest(3)ibclr(3)ibset(3)iand(3)ior(3)ieor(3)mvbits(3)

fortran-lang 固有関数の説明 (ライセンス: MIT) @urbanjost

ibclr#

名前#

ibclr(3) - [BIT:SET] ビットをクリアする

概要#

    result = ibclr(i, pos)
     elemental integer(kind=KIND) function ibclr(i,pos)

      integer(kind=KIND),intent(in) :: i
      integer(kind=**),intent(in) :: pos

特性#

  • i整数型である必要があります。

  • pos整数型である必要があります。

  • 戻り値は、**i** と同じ型です。

  • **と指定された種類は、その型でサポートされている任意の種類です。**

説明#

ibclr(3) は、**pos** ビットの位置を0に設定した**i** の値を返します。

オプション#

  • i

    変更される初期値

  • pos

    入力値において変更するビットの位置。0は最右ビットを表します。**pos** の値は非負で、**bit_size(i)** より小さくなければなりません。

結果#

戻り値は、指定されたビットが条件なしで**0** に設定されている点を除き、**i** と同じビットシーケンスを持ちます。

#

サンプルプログラム

program demo_ibclr
use,intrinsic :: iso_fortran_env,  only : int8, int16, int32, int64
implicit none
integer(kind=int16) :: i
  ! basic usage
   print *,ibclr (16, 1), ' ==> ibclr(16,1) has the value 15'

   ! it is easier to see using binary representation
   i=int(b'0000000000111111',kind=int16)
   write(*,'(b16.16,1x,i0)') ibclr(i,3), ibclr(i,3)

  ! elemental
   print *,'an array of initial values may be given as well'
   print *,ibclr(i=[7,4096,9], pos=2)
   print *
   print *,'a list of positions results in multiple returned values'
   print *,'not multiple bits set in one value, as the routine is  '
   print *,'a scalar function; calling it elementally essentially  '
   print *,'calls it multiple times.                               '
   write(*,'(b16.16)') ibclr(i=-1_int16, pos=[1,2,3,4])

   ! both may be arrays if of the same size

end program demo_ibclr

結果

 >           16  ==> ibclr(16,1) has the value 15
 > 0000000000110111 55
 >  an array of initial values may be given as well
 >            3        4096           9
 >
 >  a list of positions results in multiple returned values
 >  not multiple bits set in one value, as the routine is
 >  a scalar function; calling it elementally essentially
 >  calls it multiple times.
 > 1111111111111101
 > 1111111111111011
 > 1111111111110111
 > 1111111111101111

標準#

Fortran 95

参照#

ieor(3)not(3)btest(3)ibset(3)ibits(3)iand(3)ior(3)ieor(3)mvbits(3)

fortran-lang 固有関数の説明 (ライセンス: MIT) @urbanjost

ibset#

名前#

ibset(3) - [BIT:SET] 整数値のビットを1に設定する

概要#

    result = ibset(i, pos)
     elemental integer(kind=KIND) function ibset(i,pos)

      integer(kind=KIND),intent(in) :: i
      integer(kind=**),intent(in) :: pos

特性#

  • **と指定された種類は、その型でサポートされている任意の種類です。**

  • 戻り値は**i** と同じ型です。それ以外では、任意の*整数*型が許可されます。

説明#

ibset(3) は、**pos** ビットの位置を1に設定した**i** の値を返します。

オプション#

  • i

    変更される初期値

  • pos

    入力値において変更するビットの位置。0は最右ビットを表します。**pos** の値は非負で、**bit_size(i)** より小さくなければなりません。

結果#

戻り値は、指定されたビットが条件なしで**1** に設定されている点を除き、**i** と同じビットシーケンスを持ちます。

#

サンプルプログラム

program demo_ibset
use,intrinsic :: iso_fortran_env,  only : int8, int16, int32, int64
implicit none
integer(kind=int16) :: i
  ! basic usage
   print *,ibset (12, 1), 'ibset(12,1) has the value 14'

   ! it is easier to see using binary representation
   i=int(b'0000000000000110',kind=int16)
   write(*,'(b16.16,1x,i0,1x,i0)') ibset(i,12), ibset(i,12), i

  ! elemental
   print *,'an array of initial values may be given as well'
   print *,ibset(i=[0,4096], pos=2)
   print *
   print *,'a list of positions results in multiple returned values'
   print *,'not multiple bits set in one value, as the routine is  '
   print *,'a scalar function; calling it elementally essentially  '
   print *,'calls it multiple times.                               '
   write(*,'(b16.16)') ibset(i=0, pos=[1,2,3,4])

   ! both may be arrays if of the same size

end program demo_ibset

結果

 >           14 ibset(12,1) has the value 14
 > 0001000000000110 4102 6
 >  an array of initial values may be given as well
 >            4        4100
 >
 >  a list of positions results in multiple returned values
 >  not multiple bits set in one value, as the routine is
 >  a scalar function; calling it elementally essentially
 >  calls it multiple times.
 > 0000000000000010
 > 0000000000000100
 > 0000000000001000
 > 0000000000010000

標準#

Fortran 95

参照#

ibclr(3)

ieor(3)not(3)btest(3)ibits(3)iand(3)ior(3)ieor(3)mvbits(3)

fortran-lang 固有関数の説明 (ライセンス: MIT) @urbanjost

maskl#

名前#

maskl(3) - [BIT:SET] 左詰めのマスクを生成する

概要#

    result = maskl( i [,kind] )
     elemental integer(kind=KIND) function maskl(i,KIND)

      integer(kind=**),intent(in) :: i
      integer(kind=**),intent(in),optional :: KIND

特性#

  • **と指定された種類は、その型でサポートされている任意の種類です。**

  • **i** は整数です。

  • **kind** は、値がサポートされている*整数*型である、*整数*型のスカラー定数式でなければなりません。

  • 結果は、**kind** が存在しない限り、**i** と同じ*kind* の*整数*です。**kind** が存在する場合は、結果のkindを指定するために使用されます。

説明#

maskl(3) は、最上位**i** ビットを**1** に、残りのビットを**0** に設定します。

オプション#

  • i

    *整数*結果で設定する最上位ビットの数。結果は0から結果のkindのビット数まででなければなりません。結果のデフォルトのkindは、結果のサイズが**kind**で指定されていない限り、**i** と同じです。つまり、これらのFortranステートメントは*.true.* でなければなりません。

   i >= 0 .and. i < bitsize(i) ! if KIND is not specified
   i >= 0 .and. i < bitsize(0_KIND) ! if KIND is specified
  • kind

    *整数*結果のkindを指定します。

結果#

出力*整数*の最上位**i** ビットは1に設定され、他のビットは0に設定されます。

#

サンプルプログラム

program demo_maskl
implicit none
integer :: i
  ! basics
   i=3
   write(*,'(i0,1x,b0)') i, maskl(i)

  ! elemental
   write(*,'(*(i11,1x,b0.32,1x,/))') maskl([(i,i,i=0,bit_size(0),4)])
end program demo_maskl

結果

 > 3 11100000000000000000000000000000
 >           0 00000000000000000000000000000000
 >  -268435456 11110000000000000000000000000000
 >   -16777216 11111111000000000000000000000000
 >    -1048576 11111111111100000000000000000000
 >      -65536 11111111111111110000000000000000
 >       -4096 11111111111111111111000000000000
 >        -256 11111111111111111111111100000000
 >         -16 11111111111111111111111111110000
 >          -1 11111111111111111111111111111111

標準#

Fortran 2008

参照#

maskr(3)

fortran-lang 固有関数の説明 (ライセンス: MIT) @urbanjost

maskr#

名前#

maskr(3) - [BIT:SET] 右詰めのマスクを生成する

概要#

    result = maskr( i [,kind] )
     elemental integer(kind=KIND) function maskr(i,KIND)

      integer(kind=**),intent(in) :: i
      integer(kind=**),intent(in),optional :: KIND

特性#

  • **と指定された種類は、その型でサポートされている任意の種類です。**

  • **i** は整数です。

  • **kind** は、値がサポートされている*整数*型である、*整数*型のスカラー定数式でなければなりません。

  • 結果は、**kind** が存在しない限り、**i** と同じ*kind* の*整数*です。**kind** が存在する場合は、結果のkindを指定するために使用されます。

説明#

maskr(3) は、最下位**i** ビットを1に、残りのビットを0に設定した*整数*を生成します。

オプション#

  • i

    *整数*結果で設定する最下位ビットの数。結果は0から結果のkindのビット数まででなければなりません。結果のデフォルトのkindは、結果のサイズが**kind**で指定されていない限り、**i** と同じです。つまり、これらのFortranステートメントは*.true.* でなければなりません。

   i >= 0 .and. i < bitsize(i) ! if KIND is not specified
   i >= 0 .and. i < bitsize(0_KIND) ! if KIND is specified
  • kind

    *整数*結果のkindを指定します。

結果#

出力*整数*の最下位**i** ビットは1に設定され、他のビットは0に設定されます。

#

サンプルプログラム

program demo_maskr
implicit none
integer :: i

  ! basics
   print *,'basics'
   write(*,'(i0,t5,b32.32)') 1, maskr(1)
   write(*,'(i0,t5,b32.32)') 5,  maskr(5)
   write(*,'(i0,t5,b32.32)') 11, maskr(11)
   print *,"should be equivalent on two's-complement processors"
   write(*,'(i0,t5,b32.32)') 1,  shiftr(-1,bit_size(0)-1)
   write(*,'(i0,t5,b32.32)') 5,  shiftr(-1,bit_size(0)-5)
   write(*,'(i0,t5,b32.32)') 11, shiftr(-1,bit_size(0)-11)

  ! elemental
   print *,'elemental '
   print *,'(array argument accepted like called with each element)'
   write(*,'(*(i11,1x,b0.32,1x,/))') maskr([(i,i,i=0,bit_size(0),4)])

end program demo_maskr

結果

 >   basics
 >  1   00000000000000000000000000000001
 >  5   00000000000000000000000000011111
 >  11  00000000000000000000011111111111
 >   should be equivalent on two's-complement processors
 >  1   00000000000000000000000000000001
 >  5   00000000000000000000000000011111
 >  11  00000000000000000000011111111111
 >   elemental
 >   (array argument accepted like called with each element)
 >            0 00000000000000000000000000000000
 >           15 00000000000000000000000000001111
 >          255 00000000000000000000000011111111
 >         4095 00000000000000000000111111111111
 >        65535 00000000000000001111111111111111
 >      1048575 00000000000011111111111111111111
 >     16777215 00000000111111111111111111111111
 >    268435455 00001111111111111111111111111111
 >           -1 11111111111111111111111111111111

標準#

Fortran 2008

参照#

maskl(3)

fortran-lang 固有関数の説明 (ライセンス: MIT) @urbanjost

iparity#

名前#

iparity(3) - [BIT:LOGICAL] 配列要素のビットごとの排他的OR

概要#

    result = iparity( array [,mask] ) | iparity( array, dim [,mask] )
     integer(kind=KIND) function iparity(array, dim, mask )

      integer(kind=KIND),intent(in) :: array(..)
      logical(kind=**),intent(in),optional :: dim
      logical(kind=**),intent(in),optional :: mask(..)
  • array - *整数*配列。

  • dim - **array** のランクから1までの*整数*スカラー。

  • mask - **array** と適合する*論理*型。

説明#

iparity(3) は、対応する**mask** の要素が*.true.* の場合、次元**dim** に沿って**array** の要素をビットごとの*xor*(排他的*or*)で縮約します。

オプション#

  • array

    *整数*値の配列

  • **dim** **array** のランクから1までの値。

  • マスク

    **array** と同じ形状のスカラーまたは配列の*論理*マスク。

結果#

結果は、**array** と同じ型です。

**dim** がない場合、**array** のすべての要素のビットごとの*xor* を持つスカラーが返されます。それ以外の場合は、**array** のランクを**n** とすると、ランク**n-1** の配列と、次元**dim** が削除された**array** と同様の形状を持つ配列が返されます。

ケース(i): IPARITY(ARRAY)の結果は、ARRAYのすべての要素のビットごとの排他的ORと等しい値を持ちます。ARRAYのサイズがゼロの場合、結果はゼロになります。

ケース(ii): IPARITY(ARRAY, MASK=MASK)の結果は、以下の値と等しくなります。

               IPARITY (PACK (ARRAY, MASK)).

ケース(iii): IPARITY(ARRAY, DIM=DIM [, MASK=MASK])の結果は、ARRAYのランクが1の場合、IPARITY(ARRAY [, MASK=MASK])の結果と等しくなります。

           Otherwise, an array of values reduced along the dimension
           **dim** is returned.

#

サンプルプログラム

program demo_iparity
implicit none
integer, dimension(2) :: a
  a(1) = int(b'00100100')
  a(2) = int(b'01101010')
  print '(b8.8)', iparity(a)
end program demo_iparity

結果

   01001110

標準#

Fortran 2008

参照#

iany(3)iall(3)ieor(3)parity(3)

fortran-lang 組込み記述

iall#

名前#

iall(3) - [BIT:LOGICAL] 配列要素のビットごとのAND

概要#

    result = iall(array [,mask]) | iall(array ,dim [,mask])
     integer(kind=KIND) function iall(array,dim,mask)

      integer(kind=KIND),intent(in)        :: array(*)
      integer(kind=**),intent(in),optional :: dim
      logical(kind=**),intent(in),optional :: mask(*)

特性#

  • **と指定された種類は、その型でサポートされている任意の種類です。**

  • **array** は*整数*配列でなければなりません。

  • **mask** は、任意の*論理*kindの**array** と適合する*論理*配列です。

  • **dim** は任意の*整数*kindです。

  • 結果は、**array** と同じ型とkindになります。

説明#

iall(3) は、対応する**mask** の要素が*.true.* の場合、次元**dim** に沿って**array** の要素をビットごとの*and* で縮約します。

オプション#

  • array

    *整数*型の配列でなければなりません。

  • dim

    (オプション) **n** を**array** のランクとすると、**1 から n** の範囲の値を持つ*整数*型のスカラーでなければなりません。

  • マスク

    (オプション) *論理*型で、スカラーまたは**array** と同じ形状の配列でなければなりません。

結果#

結果は、**array** と同じ型です。

**dim** がない場合、**array** のすべての要素のビットごとの*all* を持つスカラーが返されます。それ以外の場合は、**array** のランクを**n** とすると、ランク**n-1** の配列と、次元**dim** が削除された**array** と同様の形状を持つ配列が返されます。

#

サンプルプログラム

program demo_iall
use, intrinsic :: iso_fortran_env, only : integer_kinds, &
 & int8, int16, int32, int64
implicit none
integer(kind=int8) :: a(2)

   a(1) = int(b'00100100')
   a(2) = int(b'01101010')

   print '(b8.8)', iall(a)

end program demo_iall

結果

 > 00100000

標準#

Fortran 2008

参照#

iany(3)iparity(3)iand(3)

fortran-lang 固有関数の説明 (ライセンス: MIT) @urbanjost

iand#

名前#

iand(3) - [BIT:LOGICAL] ビットごとの論理AND

概要#

    result = iand(i, j)
     elemental integer(kind=KIND) function iand(i,j)

      integer(kind=KIND),intent(in) :: i
      integer(kind=KIND),intent(in) :: j

特性#

  • **i**、**j**、および結果は、同じ*整数*型とkindを持ちます。ただし、**i** または**j** のいずれかがBOZ定数である場合を除きます。

説明#

iand(3) は、2つの値のビットごとの論理**and** を返します。

オプション#

  • i

    ビットを比較する値のペアの1つ

  • j

    ビットを比較する値のペアの1つ

**i** または**j** のいずれかがBOZリテラル定数の場合、まず、他のもののkind型パラメータを持つ*整数*型に、組込み関数**int**(3)のように変換されます。

結果#

結果は、次の表に従って**i** と**j** をビットごとに組み合わせた値を持ちます。

    I  |  J  |  IAND (I, J)
  ----------------------------
    1  |  1  |    1
    1  |  0  |    0
    0  |  1  |    0
    0  |  0  |    0

したがって、**i** と**j** の両方のビットがオンの場合、結果のビットはオン(1)になります。そうでない場合、結果のビットはオフ(0)になります。

これは一般的に、2つの値の「ビット単位論理AND」と呼ばれます。

#

サンプルプログラム

program demo_iand
implicit none
integer :: a, b
 data a / z'f' /, b / z'3' /
 write (*,*) 'a=',a,' b=',b,'iand(a,b)=',iand(a, b)
 write (*,'(b32.32)') a,b,iand(a,b)
end program demo_iand

結果

    a= 15  b= 3 iand(a,b)= 3
   00000000000000000000000000001111
   00000000000000000000000000000011
   00000000000000000000000000000011

標準#

Fortran 95

関連項目#

ieor(3)ibclr(3)not(3)btest(3)ibclr(3)ibits(3)ibset(3)ior(3)ieor(3)mvbits(3)

fortran-lang 固有関数の説明 (ライセンス: MIT) @urbanjost

iany#

名前#

iany(3) - [BIT:LOGICAL] 配列要素のビット単位OR

概要#

    result = iany(array [,mask]) | iany(array ,dim [,mask])
     integer(kind=KIND) function iany(array,dim,mask)

      integer(kind=KIND),intent(in)        :: array(..)
      integer(kind=**),intent(in),optional :: dim
      logical(kind=**),intent(in),optional :: mask(..)

特性#

  • array整数配列です。

  • **dim** は任意の*整数*kindです。

  • maskarrayに適合する論理配列です。

  • 結果は、arrayと同じ型とkindになります。dimが存在しないか1の場合、スカラーになります。それ以外の場合は、次元dimによって削減された配列の形状と階数になります。

**で指定されたkindは、型の任意のサポートされているkindです。

説明#

iany(3)は、maskの対応する要素が.true.の場合、次元dimに沿ってarrayの要素をビット単位OR(包含的OR)で削減します。

オプション#

  • array

    マスクに基づいて選択的にORする要素の配列。

  • dim

    1からnまでの範囲の値(narrayの階数に等しい)。

  • マスク

    論理スカラー。またはarrayと同じ形状の配列。

結果#

結果は、**array** と同じ型です。

dimが存在しない場合、array内のすべての要素のビット単位orを持つスカラーが返されます。それ以外の場合は、階数がn-1narrayの階数に等しい)で、次元dimが削除されたarrayと同様の形状を持つ配列が返されます。

#

サンプルプログラム

program demo_iany
use, intrinsic :: iso_fortran_env, only : integer_kinds, &
 & int8, int16, int32, int64
implicit none
logical,parameter :: T=.true., F=.false.
integer(kind=int8) :: a(3)
   a(1) = int(b'00100100',int8)
   a(2) = int(b'01101010',int8)
   a(3) = int(b'10101010',int8)
   write(*,*)'A='
   print '(1x,b8.8)', a
   print *
   write(*,*)'IANY(A)='
   print '(1x,b8.8)', iany(a)
   print *
   write(*,*)'IANY(A) with a mask'
   print '(1x,b8.8)', iany(a,mask=[T,F,T])
   print *
   write(*,*)'should match '
   print '(1x,b8.8)', iany([a(1),a(3)])
   print *
   write(*,*)'does it?'
   write(*,*)iany(a,[T,F,T]) == iany([a(1),a(3)])
end program demo_iany

結果

    A=
    00100100
    01101010
    10101010

    IANY(A)=
    11101110

    IANY(A) with a mask
    10101110

    should match
    10101110

    does it?
    T

標準#

Fortran 2008

関連項目#

iparity(3)iall(3)ior(3)

fortran-lang 固有関数の説明 (ライセンス: MIT) @urbanjost

ieor#

名前#

ieor(3) - [BIT:LOGICAL] ビット単位排他的OR

概要#

    result = ieor(i, j)
     elemental integer(kind=**) function ieor(i,j)

      integer(kind=**),intent(in) :: i
      integer(kind=**),intent(in) :: j

特性#

  • ij、および結果は、同じ整数kindである必要があります。

  • 例外として、ijのいずれかがBOZリテラル定数である場合があります。

説明#

ieor(3)は、ijのビット単位排他的-orを返します。

排他的ORまたは「排他的論理和」は、その引数が異なる場合にのみ真となる論理演算です。この場合、1ビットと0ビットが真と偽の代わりに使用されます。

これは、多くの場合、「eXclusive OR」の略である「XOR」という表記で表されます。

プロセスを別の方法で考える方法は、結果に次の表に従ってijをビット単位で組み合わせた値が得られることです。

  >  I | J |IEOR (I, J)
  >  --#---#-----------
  >  1 | 1 |  0
  >  1 | 0 |  1
  >  0 | 1 |  1
  >  0 | 0 |  0

オプション#

  • i

    XORする2つの値の最初の値。

  • j

    XORする2つの値の2番目の値。

IまたはJのいずれかがboz-literal-constantの場合、最初に、内在関数INTによって、他の型のkind型パラメータを持つ整数型に変換されます。

結果#

ijの同じ位置でビットが異なる場合、結果の対応するビットは1になり、そうでない場合は0になります。

#

サンプルプログラム

program demo_ieor
use,intrinsic :: iso_fortran_env,  only : int8, int16, int32, int64
implicit none
integer(kind=int16) :: i,j
  ! basic usage
   print *,ieor (16, 1), ' ==> ieor(16,1) has the value 17'

   ! it is easier to see using binary representation
   i=int(b'0000000000111111',kind=int16)
   j=int(b'0000001111110000',kind=int16)
   write(*,'(a,b16.16,1x,i0)')'i=     ',i, i
   write(*,'(a,b16.16,1x,i0)')'j=     ',j, j
   write(*,'(a,b16.16,1x,i0)')'result=',ieor(i,j), ieor(i,j)

  ! elemental
   print *,'arguments may be arrays. If both are arrays they '
   print *,'must have the same shape.                        '
   print *,ieor(i=[7,4096,9], j=2)

   ! both may be arrays if of the same size

end program demo_ieor

結果

 >           17  ==> ieor(16,1) has the value 17
 > i=     0000000000111111 63
 > j=     0000001111110000 1008
 > result=0000001111001111 975
 >  arguments may be arrays. If both are arrays they
 >  must have the same shape.
 >            5        4098          11

標準#

Fortran 95

関連項目#

ieor(3)ibclr(3)not(3)btest(3)ibclr(3)ibits(3)ibset(3)iand(3)ior(3)mvbits(3)

fortran-lang 固有関数の説明 (ライセンス: MIT) @urbanjost

ior#

名前#

ior(3) - [BIT:LOGICAL] ビット単位論理包含的OR

概要#

    result = ior(i, j)
     elemental integer(kind=KIND) function ior(i,j)

      integer(kind=KIND ,intent(in) :: i
      integer(kind=KIND ,intent(in) :: j

特性#

  • **i**、**j**、および結果は、同じ*整数*型とkindを持ちます。ただし、**i** または**j** のいずれかがBOZ定数である場合を除きます。

説明#

ior(3)は、ijのビット単位ブール包含的-orを返します。

オプション#

  • i

    ビットを比較する値のペアの1つ

  • j

    ビットを比較する値のペアの1つ

**i** または**j** のいずれかがBOZリテラル定数の場合、まず、他のもののkind型パラメータを持つ*整数*型に、組込み関数**int**(3)のように変換されます。

結果#

結果は、次の表に従ってIとJをビット単位で組み合わせた値を持ちます。

          I   J   IOR (I, J)
          1   1        1
          1   0        1
          0   1        1
          0   0        0

いずれかの入力値でビットが設定されている場合、結果は設定されます。それ以外の場合は、結果のビットはゼロです。

これは一般的に、2つの値の「ビット単位論理包含的OR」と呼ばれます。

#

サンプルプログラム

program demo_ior
implicit none
integer :: i, j, k
   i=53       ! i=00110101 binary (lowest order byte)
   j=45       ! j=00101101 binary (lowest order byte)
   k=ior(i,j) ! k=00111101 binary (lowest order byte), k=61 decimal
   write(*,'(i8,1x,b8.8)')i,i,j,j,k,k
end program demo_ior

結果

         53 00110101
         45 00101101
         61 00111101

標準#

Fortran 95

関連項目#

ieor(3)ibclr(3)not(3)btest(3)ibclr(3)ibits(3)ibset(3)iand(3)ieor(3)mvbits(3)

fortran-lang 固有関数の説明 (ライセンス: MIT) @urbanjost

not#

名前#

not(3) - [BIT:LOGICAL] 論理否定;整数のすべてのビットを反転

概要#

    result = not(i)
    elemental integer(kind=KIND) function not(i)

     integer(kind=KIND), intent(in) :: i

特性#

  • iは、任意の有効なkindの整数です。

  • 返される整数は、引数iと同じkindです。

説明#

not(3)は、iのビット単位ブール逆を返します。これは、「ビット単位補数」または値の「論理否定」としても知られています。

入力ビットが1の場合、出力ではその位置が0になります。逆に、入力ビットが0の場合は、出力では1になります。

オプション#

  • i

    ビットを反転する値。

結果#

結果は、次の真理値表に従ってiをビット単位で補うことによって得られた値を持ちます。

   >    I   |  NOT(I)
   >    ----#----------
   >    1   |   0
   >    0   |   1

つまり、すべての入力ビットが反転されます。

#

サンプルプログラム

program demo_not
implicit none
integer :: i
  ! basics
   i=-13741
   print *,'the input value',i,'represented in bits is'
   write(*,'(1x,b32.32,1x,i0)') i, i
   i=not(i)
   print *,'on output it is',i
   write(*,'(1x,b32.32,1x,i0)') i, i
   print *, " on a two's complement machine flip the bits and add 1"
   print *, " to get the value with the sign changed, for example."
   print *, 1234, not(1234)+1
   print *, -1234, not(-1234)+1
   print *, " of course 'x=-x' works just fine and more generally."
end program demo_not

結果

    the input value      -13741 represented in bits is
    11111111111111111100101001010011 -13741
    on output it is       13740
    00000000000000000011010110101100 13740
     on a two's complement machine flip the bits and add 1
     to get the value with the sign changed, for example.
           1234       -1234
          -1234        1234
     of course 'x=-x' works just fine and more generally.

標準#

Fortran 95

関連項目#

iand(3)ior(3)ieor(3)ibits(3)ibset(3)

ibclr(3)

fortran-lang 固有関数の説明 (ライセンス: MIT) @urbanjost

ishftc#

名前#

ishftc(3) - [BIT:SHIFT] 最右端のビットを循環的にシフトする、別名論理シフト

概要#

    result = ishftc( i, shift [,size] )
     elemental integer(kind=KIND) function ishftc(i, shift, size)

      integer(kind=KIND),intent(in)        :: i
      integer(kind=**),intent(in)          :: shift
      integer(kind=**),intent(in),optional :: size

特性#

  • **と指定された種類は、その型でサポートされている任意の種類です。**

  • iは任意のkindの整数です。

  • shiftsizeは任意のkindの整数です。

  • iのkindによって、返される値のkindが決まります。

説明#

ishftc(3)は、整数の指定された最右端のビットのみを循環的にシフトします。

ishftc(3)は、最右端のsizeビットがshift位置だけ循環的にシフトされたiに対応する値を返します。つまり、一方の端からシフトアウトされたビットは、セクションの反対側の端からシフトインされます。

shiftの値が0より大きい場合は左シフト、0の場合はシフトなし、0より小さい場合は右シフトに対応します。

オプション#

  • i

    シフトするビットのパターンを指定する値。

  • shift

    shiftが正の場合、左シフトされます。shiftが負の場合、右シフトされます。shiftが0の場合、シフトは実行されません。

    shiftの絶対値はsizeより小さくなければなりません(簡単に言うと、シフトする位置の数は、シフトするように指定されたビット数以下でなければなりません)。

  • size

    この値は0より大きく、bit_size(i)以下でなければなりません。

    bit_size(i)が存在しない場合のデフォルトは、値i全体を循環的にシフトすることです。

結果#

結果の特性(kind、形状、サイズ、階数など)はiと同じです。

結果は、isize個の最右端のビットをshift位置だけ循環的にシフトすることによって得られた値を持ちます。

ビットは失われません。

シフトされていないビットは変更されません。

#

サンプルプログラム

program demo_ishftc
use,intrinsic :: iso_fortran_env, only : int8, int16, int32, int64
implicit none
integer             :: i
character(len=*),parameter :: g='(b32.32,1x,i0)'
  ! basics
   write(*,*) ishftc(3, 1),' <== typically should have the value 6'

   print *, 'lets start with this:'
   write(*,'(b32.32)')huge(0)
   print *, 'shift the value by various amounts, negative and positive'
   do i= -bit_size(0), bit_size(0), 8
      write(*,g) ishftc(huge(0),i), i
   enddo
  print *,'elemental'
  i=huge(0)
  write(*,*)ishftc(i,[2,3,4,5])
  write(*,*)ishftc([2**1,2**3,-2**7],3)
  print *,'note the arrays have to conform when elemental'
  write(*,*)ishftc([2**1,2**3,-2**7],[5,20,0])

end program demo_ishftc

結果

 >            6  <== typically should have the value 6
 >  lets start with this:
 > 01111111111111111111111111111111
 >  shift the value by various amounts, negative and positive
 > 01111111111111111111111111111111 -32
 > 11111111111111111111111101111111 -24
 > 11111111111111110111111111111111 -16
 > 11111111011111111111111111111111 -8
 > 01111111111111111111111111111111 0
 > 11111111111111111111111101111111 8
 > 11111111111111110111111111111111 16
 > 11111111011111111111111111111111 24
 > 01111111111111111111111111111111 32
 >  elemental
 >           -3          -5          -9         -17
 >           16          64       -1017
 >  note the arrays have to conform when elemental
 >           64     8388608        -128

標準#

Fortran 95

関連項目#

  • ishft(3) - 整数のビットの論理シフト

  • shifta(3) - 右シフト(埋め込みあり)

  • shiftl(3) - ビットを左にシフト

  • shiftr(3) - 2つのint…のビットの組み合わせによる右シフト

  • dshiftl(3) - 2つのinte…のビットの組み合わせによる左シフト

  • dshiftr(3) - 2つのintのビットを組み合わせて右シフトします。

  • cshift(3) - 配列の要素を循環シフトします。

  • eoshift(3) - 配列の要素をエンドオフシフトします。

fortran-lang 固有関数の説明 (ライセンス: MIT) @urbanjost

ishft#

名前#

ishft(3) - [BIT:SHIFT] 整数のビットの論理シフト

概要#

    result = ishftc( i, shift )
     elemental integer(kind=KIND) function ishft(i, shift )

      integer(kind=KIND),intent(in) :: i
      integer(kind=**),intent(in) :: shift

特性#

  • **と指定された種類は、その型でサポートされている任意の種類です。**

  • i は任意の種類の整数です。i の種類によって戻り値の種類が決まります。

  • shift は任意の種類の整数です。

説明#

ishft(3) は、shift の符号と大きさで指定されたとおりに、すべてのビットをshift ビット分左または右にシフトしたiに対応する値を返します。

左端または右端からシフトされたビットは失われます。反対の端からはゼロがシフトインされます。

オプション#

  • i

    シフトするビットのパターンを指定する値。

  • shift

    shiftの値が0より大きい場合は左シフト、0の場合はシフトなし、0より小さい場合は右シフトに対応します。

    shift の絶対値がbit_size(i)より大きい場合、値は未定義です。

結果#

結果は、i のビットをshift ポジションだけシフトすることによって得られる値です。

  1. shift が正の場合、左シフトされます。

  2. shift が負の場合、右シフトされます。

  3. shift がゼロの場合、シフトは実行されません。

適切なように、左端または右端からシフトアウトされたビットは失われます。反対の端からゼロがシフトインされます。

#

サンプルプログラム

program demo_ishft
use,intrinsic :: iso_fortran_env, only : int8, int16, int32, int64
implicit none
integer             :: shift
character(len=*),parameter :: g='(b32.32,1x,i0)'

   write(*,*) ishft(3, 1),' <== typically should have the value 6'

   shift=4
   write(*,g) ishft(huge(0),shift), shift
   shift=0
   write(*,g) ishft(huge(0),shift), shift
   shift=-4
   write(*,g) ishft(huge(0),shift), shift
end program demo_ishft

結果

>              6  <== typically should have the value 6
>   11111111111111111111111111110000 4
>   01111111111111111111111111111111 0
>   00000111111111111111111111111111 -4

標準#

Fortran 95

参照#

ishftc(3)

fortran-lang 固有関数の説明 (ライセンス: MIT) @urbanjost

shifta#

名前#

shifta(3) - [BIT:SHIFT] 埋め込み付き右シフト

概要#

    result = shifta(i, shift )
     elemental integer(kind=KIND) function shifta(i, shift)

      integer(kind=KIND),intent(in) :: i
      integer(kind=**),intent(in) :: shift

特性#

  • **と指定された種類は、その型でサポートされている任意の種類です。**

  • i は任意の種類の整数です。

  • shift は任意の種類の整数です。

  • 結果は、自動的にiと同じ型、種類、ランクになります。

説明#

shifta(3) は、すべてのビットをshift ビット分右にシフトし、左側の空いたビットを元の左端のビットの値で埋めたiに対応する値を返します。

オプション#

  • i

    シフトしてゼロで埋める初期値

  • shift

    右にシフトするビット数。非負で、bit_size(i)以下である必要があります。そうでない場合、値は未定義です。shiftがゼロの場合、結果はiです。

結果#

結果は、iのビットを右にshiftビットシフトし、iの最左ビットを左のshiftビットに複製することによって得られる値です(「2の補数」表現における最左ビットは符号ビットであることに注意してください)。

右端からシフトアウトされたビットは失われます。

shiftがゼロの場合、結果はiです。

#

サンプルプログラム

program demo_shifta
use,intrinsic :: iso_fortran_env, only : int8, int16, int32, int64
implicit none
integer(kind=int32) :: ival
integer             :: shift
integer(kind=int32) :: oval
integer(kind=int32),allocatable :: ivals(:)
integer             :: i
integer(kind=int8)  :: arr(2,2)=reshape([2,4,8,16],[2,2])

  ! basic usage
  write(*,*)shifta(100,3)

  ! loop through some interesting values
   shift=5

   ivals=[ -1, -0, +0, +1, &
   & int(b"01010101010101010101010101010101"), &
   & int(b"10101010101010101010101010101010"), &
   & int(b"00000000000000000000000000011111") ]

   ! does your platform distinguish between +0 and -0?
   ! note the original leftmost bit is used to fill in the vacated bits

   write(*,'(/,"SHIFT =  ",i0)') shift
   do i=1,size(ivals)
      ival=ivals(i)
      write(*,'(  "I =      ",b32.32," == ",i0)') ival,ival
      oval=shifta(ival,shift)
      write(*,'(  "RESULT = ",b32.32," == ",i0)') oval,oval
   enddo
   ! elemental
   write(*,*)"characteristics of the result are the same as input"
   write(*,'(*(g0,1x))') &
     & "kind=",kind(shifta(arr,3)), "shape=",shape(shifta(arr,3)), &
     & "size=",size(shifta(arr,3)) !, "rank=",rank(shifta(arr,3))

end program demo_shifta

結果

 >           12
 >
 > SHIFT =  5
 > I =      11111111111111111111111111111111 == -1
 > RESULT = 11111111111111111111111111111111 == -1
 > I =      00000000000000000000000000000000 == 0
 > RESULT = 00000000000000000000000000000000 == 0
 > I =      00000000000000000000000000000000 == 0
 > RESULT = 00000000000000000000000000000000 == 0
 > I =      00000000000000000000000000000001 == 1
 > RESULT = 00000000000000000000000000000000 == 0
 > I =      01010101010101010101010101010101 == 1431655765
 > RESULT = 00000010101010101010101010101010 == 44739242
 > I =      10101010101010101010101010101010 == -1431655766
 > RESULT = 11111101010101010101010101010101 == -44739243
 > I =      00000000000000000000000000011111 == 31
 > RESULT = 00000000000000000000000000000000 == 0
 >  characteristics of the result are the same as input
 > kind= 1 shape= 2 2 size= 4

標準#

Fortran 2008

参照#

shiftl(3)shiftr(3)ishft(3)ishftc(3)

fortran-lang 固有関数の説明 (ライセンス: MIT) @urbanjost

shiftl#

名前#

shiftl(3) - [BIT:SHIFT] ビットを左シフト

概要#

    result = shiftl( i, shift )
     elemental integer(kind=KIND) function shiftl(i, shift)

      integer(kind=KIND),intent(in) :: i
      integer(kind=**),intent(in) :: shift

特性#

  • **と指定された種類は、その型でサポートされている任意の種類です。**

  • i は任意の種類の整数です。

  • shift は任意の種類の整数です。

  • 結果は、自動的にiと同じ型、種類、ランクになります。

説明#

shiftl(3) は、すべてのビットをshift ビット分左にシフトしたiに対応する値を返します。

左端からシフトアウトされたビットは失われ、右端からシフトインされたビットは0に設定されます。

shift の絶対値がbit_size(i)より大きい場合、値は未定義です。

たとえば、16ビットの整数を5ビット左シフトすると…

    >  |a|b|c|d|e|f|g|h|i|j|k|l|m|n|o|p| <- original 16-bit example
    >  |f|g|h|i|j|k|l|m|n|o|p|           <- left-shifted five
    >  |f|g|h|i|j|k|l|m|n|o|p|0|0|0|0|0| <- right-padded with zeros

結果の値はishft (i, shift)と同じです。

オプション#

  • i

    シフトしてゼロで埋める初期値

  • shift

    左にシフトするビット数。非負で、bit_size(i)以下である必要があります。

結果#

戻り値は整数型で、iと同じ種類です。

#

サンプルプログラム

program demo_shiftl
use,intrinsic :: iso_fortran_env, only : int8, int16, int32, int64
implicit none
integer             :: shift
integer(kind=int32) :: oval
integer(kind=int32) :: ival
integer(kind=int32),allocatable :: ivals(:)
integer             :: i

  print *, ' basic usage'
  ival=100
  write(*,*)ival, shiftl(ival,3)

 ! elemental (input values may be conformant arrays)
  print *, ' elemental'

 ! loop through some ivalues
   shift=9
   ivals=[ &
   & int(b"01010101010101010101010101010101"), &
   & int(b"10101010101010101010101010101010"), &
   & int(b"11111111111111111111111111111111") ]

   write(*,'(/,"SHIFT =  ",i0)') shift
   do i=1,size(ivals)
      ! print initial value as binary and decimal
      write(*,'(  "I =      ",b32.32," == ",i0)') ivals(i),ivals(i)
      ! print shifted value as binary and decimal
      oval=shiftl(ivals(i),shift)
      write(*,'(  "RESULT = ",b32.32," == ",i0)') oval,oval
   enddo

  ! more about elemental
   ELEM : block
   integer(kind=int8)  :: arr(2,2)=reshape([2,4,8,16],[2,2])
   write(*,*)"characteristics of the result are the same as input"
   write(*,'(*(g0,1x))') &
     & "kind=",kind(shiftl(arr,3)), "shape=",shape(shiftl(arr,3)), &
     & "size=",size(shiftl(arr,3)) !, "rank=",rank(shiftl(arr,3))
   endblock ELEM

end program demo_shiftl

結果

 >    basic usage
 >           100         800
 >    elemental
 >
 >  SHIFT =  9
 >  I =      01010101010101010101010101010101 == 1431655765
 >  RESULT = 10101010101010101010101000000000 == -1431655936
 >  I =      10101010101010101010101010101010 == -1431655766
 >  RESULT = 01010101010101010101010000000000 == 1431655424
 >  I =      11111111111111111111111111111111 == -1
 >  RESULT = 11111111111111111111111000000000 == -512
 >   characteristics of the result are the same as input
 >  kind= 1 shape= 2 2 size= 4

標準#

Fortran 2008

参照#

shifta(3)shiftr(3)ishft(3)ishftc(3)

fortran-lang 固有関数の説明 (ライセンス: MIT) @urbanjost

shiftr#

名前#

shiftr(3) - [BIT:SHIFT] ビットを右シフト

概要#

    result = shiftr( i, shift )
     elemental integer(kind=KIND) function shiftr(i, shift)

      integer(kind=KIND),intent(in) :: i
      integer(kind=**),intent(in) :: shift

特性#

  • **と指定された種類は、その型でサポートされている任意の種類です。**

  • i は任意の種類の整数です。

  • shift は任意の種類の整数です。

  • 結果は、自動的にiと同じ型、種類、ランクになります。

説明#

shiftr(3) は、すべてのビットをshift ビット分右にシフトしたiに対応する値を返します。

shift の絶対値がbit_size(i)より大きい場合、値は未定義です。

右端からシフトアウトされたビットは失われ、左端からシフトインされたビットは0に設定されます。

たとえば、16ビットの整数を5ビット右シフトすると…

    >  |a|b|c|d|e|f|g|h|i|j|k|l|m|n|o|p| <- original 16-bit example
    >            |a|b|c|d|e|f|g|h|i|j|k| <- right-shifted five
    >  |0|0|0|0|0|f|g|h|i|j|k|l|m|n|o|p| <- left-padded with zeros

結果の値はishft (i, -shift)と同じです。

オプション#

  • i

    シフトする値

  • shift

    右にシフトするビット数。非負で、bit_size(i)以下である必要があります。

結果#

右にshiftポジションだけシフトされた残りのビット。左側の空いた位置はゼロで埋められます。

#

サンプルプログラム

program demo_shiftr
use,intrinsic :: iso_fortran_env, only : int8, int16, int32, int64
implicit none
integer             :: shift
integer(kind=int32) :: oval
integer(kind=int32) :: ival
integer(kind=int32),allocatable :: ivals(:)
integer             :: i

  print *,' basic usage'
  ival=100
  write(*,*)ival, shiftr(100,3)

  ! elemental (input values may be conformant arrays)
  print *,' elemental'
   shift=9
   ivals=[ &
   & int(b"01010101010101010101010101010101"), &
   & int(b"10101010101010101010101010101010"), &
   & int(b"11111111111111111111111111111111") ]

   write(*,'(/,"SHIFT =  ",i0)') shift
   do i=1,size(ivals)
      ! print initial value as binary and decimal
      write(*,'(  "I =      ",b32.32," == ",i0)') ivals(i),ivals(i)
      ! print shifted value as binary and decimal
      oval=shiftr(ivals(i),shift)
      write(*,'(  "RESULT = ",b32.32," == ",i0,/)') oval,oval
   enddo

   ! more on elemental
   ELEM : block
   integer(kind=int8)  :: arr(2,2)=reshape([2,4,8,16],[2,2])
   write(*,*)"characteristics of the result are the same as input"
   write(*,'(*(g0,1x))') &
     & "kind=",kind(shiftr(arr,3)), "shape=",shape(shiftr(arr,3)), &
     & "size=",size(shiftr(arr,3)) !, "rank=",rank(shiftr(arr,3))
   endblock ELEM

end program demo_shiftr

結果

  >    basic usage
  >           100          12
  >    elemental
  >
  >  SHIFT =  9
  >  I =      01010101010101010101010101010101 == 1431655765
  >  RESULT = 00000000001010101010101010101010 == 2796202
  >
  >  I =      10101010101010101010101010101010 == -1431655766
  >  RESULT = 00000000010101010101010101010101 == 5592405
  >
  >  I =      11111111111111111111111111111111 == -1
  >  RESULT = 00000000011111111111111111111111 == 8388607
  >
  >   characteristics of the result are the same as input
  >  kind= 1 shape= 2 2 size= 4

標準#

Fortran 2008

参照#

shifta(3)shiftl(3)ishft(3)ishftc(3)

fortran-lang 固有関数の説明 (ライセンス: MIT) @urbanjost