stdlib_bitsets#
See also the specification page: stdlib_bitsets.
stdlib_bitsets (module)#
Implements zero based bitsets of size up to huge(0_int32). The current code uses 64 bit integers to store the bits and uses all 64 bits. The code assumes two’s complement integers, and treats negative integers as having the sign bit set. (Specification: stdlib_bitsets)
Public procedures
Variables
- max_digitsinteger, parameter, Default = 10
- overflow_bitsinteger(kind=bits_kind), parameter, Default = 2_bits_kind**30/5
- successinteger, parameter, Default = 0
Error flag indicating no errors
- alloc_faultinteger, parameter, Default = 1
Error flag indicating a memory allocation failure
- array_size_invalid_errorinteger, parameter, Default = 2
Error flag indicating an invalid bits value
- char_string_invalid_errorinteger, parameter, Default = 3
Error flag indicating an invalid character string
- char_string_too_large_errorinteger, parameter, Default = 4
Error flag indicating a too large character string
- char_string_too_small_errorinteger, parameter, Default = 5
Error flag indicating a too small character string
- eof_failureinteger, parameter, Default = 6
Error flag indicating unexpected End-of-File on a READ
- index_invalid_errorinteger, parameter, Default = 7
Error flag indicating an invalid index
- integer_overflow_errorinteger, parameter, Default = 8
Error flag indicating integer overflow
- read_failureinteger, parameter, Default = 9
Error flag indicating failure of a READ statement
- write_failureinteger, parameter, Default = 10
Error flag indicating a failure on a WRITE statement
Types
bitset_type (type)#
-
bitset_type (type) Parent type for bitset_64 and bitset_large (Specification: stdlib_bitsets)
- Procedures:
allanybit_countclear_bitclear_rangeclear=>clear_bit,clear_rangeflip_bitflip_rangeflip=>flip_bit,flip_rangefrom_stringinit_zeroinit=>init_zeroinputnonenotoutputread_bitset_stringread_bitset_unitread_bitset=>read_bitset_string,read_bitset_unitset_bitset_rangeset=>set_bit,set_rangetestto_stringvaluewrite_bitset_stringwrite_bitset_unitwrite_bitset=>write_bitset_string,write_bitset_unit
bitset_large (type)#
-
bitset_large (type) Type for bitsets with more than 64 bits (Specification: stdlib_bitsets)
- Procedures:
all=>all_largeany=>any_largebit_count=>bit_count_largeclear_bit=>clear_bit_largeclear_range=>clear_range_largeflip_bit=>flip_bit_largeflip_range=>flip_range_largefrom_string=>from_string_largeinit_zero=>init_zero_largeinput=>input_largenone=>none_largenot=>not_largeoutput=>output_largeread_bitset_string=>read_bitset_string_largeread_bitset_unit=>read_bitset_unit_largeset_bit=>set_bit_largeset_range=>set_range_largetest=>test_largeto_string=>to_string_largevalue=>value_largewrite_bitset_string=>write_bitset_string_largewrite_bitset_unit=>write_bitset_unit_large
bitset_64 (type)#
-
bitset_64 (type) Type for bitsets with no more than 64 bits (Specification: stdlib_bitsets)
- Procedures:
all=>all_64any=>any_64bit_count=>bit_count_64clear_bit=>clear_bit_64clear_range=>clear_range_64flip_bit=>flip_bit_64flip_range=>flip_range_64from_string=>from_string_64init_zero=>init_zero_64input=>input_64none=>none_64not=>not_64output=>output_64read_bitset_string=>read_bitset_string_64read_bitset_unit=>read_bitset_unit_64set_bit=>set_bit_64set_range=>set_range_64test=>test_64to_string=>to_string_64value=>value_64write_bitset_string=>write_bitset_string_64write_bitset_unit=>write_bitset_unit_64
Interfaces
assignment(=) (interface)#
-
assignment(=) (interface) Used to define assignment for bitset_large. (Specification: stdlib_bitsets)
- Procedures:
-
pure module subroutine assign_logint8_large(self, logical_vector)# Used to define assignment from an array of type logical(int8) to a bitset_large.
- Arguments:
- selftype(bitset_large), intent(out)
- logical_vectorlogical(kind=int8), intent(in), dimension(:)
-
pure module subroutine logint8_assign_large(logical_vector, set)# Used to define assignment to an array of type logical(int8) from a bitset_large.
- Arguments:
- logical_vectorlogical(kind=int8), allocatable, intent(out), dimension(:)
- settype(bitset_large), intent(in)
-
pure module subroutine assign_logint16_large(self, logical_vector)# Used to define assignment from an array of type logical(int16) to a bitset_large.
- Arguments:
- selftype(bitset_large), intent(out)
- logical_vectorlogical(kind=int16), intent(in), dimension(:)
-
pure module subroutine logint16_assign_large(logical_vector, set)# Used to define assignment to an array of type logical(int16) from a bitset_large.
- Arguments:
- logical_vectorlogical(kind=int16), allocatable, intent(out), dimension(:)
- settype(bitset_large), intent(in)
-
pure module subroutine assign_logint32_large(self, logical_vector)# Used to define assignment from an array of type logical(int32) to a bitset_large.
- Arguments:
- selftype(bitset_large), intent(out)
- logical_vectorlogical(kind=int32), intent(in), dimension(:)
-
pure module subroutine logint32_assign_large(logical_vector, set)# Used to define assignment to an array of type logical(int32) from a bitset_large.
- Arguments:
- logical_vectorlogical(kind=int32), allocatable, intent(out), dimension(:)
- settype(bitset_large), intent(in)
-
pure module subroutine assign_logint64_large(self, logical_vector)# Used to define assignment from an array of type logical(int64) to a bitset_large.
- Arguments:
- selftype(bitset_large), intent(out)
- logical_vectorlogical(kind=int64), intent(in), dimension(:)
-
pure module subroutine logint64_assign_large(logical_vector, set)# Used to define assignment to an array of type logical(int64) from a bitset_large.
- Arguments:
- logical_vectorlogical(kind=int64), allocatable, intent(out), dimension(:)
- settype(bitset_large), intent(in)
Example
program example_assignment use stdlib_bitsets logical(int8) :: logical1(64) = .true. logical(int32), allocatable :: logical2(:) type(bitset_64) :: set0, set1 set0 = logical1 if ( set0 % bits() /= 64 ) then error stop procedure // & ' initialization with logical(int8) failed to set' // & ' the right size.' else if ( .not. set0 % all() ) then error stop procedure // ' initialization with' // & ' logical(int8) failed to set the right values.' else write(*,*) 'Initialization with logical(int8) succeeded.' end if set1 = set0 if ( set1 == set0 ) & write(*,*) 'Initialization by assignment succeeded' logical2 = set1 if ( all( logical2 ) ) then write(*,*) 'Initialization of logical(int32) succeeded.' end if end program example_assignment
and (interface)#
-
and (interface) Sets the bits in set1 to the bitwise and of the original bits in set1 and set2. The sets must have the same number of bits otherwise the result is undefined. (Specification: stdlib_bitsets)
-
elemental module subroutine and_large(set1, set2)# - Arguments:
- set1type(bitset_large), intent(inout)
- set2type(bitset_large), intent(in)
-
elemental module subroutine and_64(set1, set2)# - Arguments:
- set1type(bitset_64), intent(inout)
- set2type(bitset_64), intent(in)
Example
program example_and use stdlib_bitsets type(bitset_large) :: set0, set1 call set0 % init(166) call set1 % init(166) call and( set0, set1 ) ! none none if ( none(set0) ) write(*,*) 'First test of AND worked.' call set0 % not() call and( set0, set1 ) ! all none if ( none(set0) ) write(*,*) 'Second test of AND worked.' call set1 % not() call and( set0, set1 ) ! none all if ( none(set0) ) write(*,*) 'Third test of AND worked.' call set0 % not() call and( set0, set1 ) ! all all if ( all(set0) ) write(*,*) 'Fourth test of AND worked.' end program example_and
-
and_not (interface)#
-
and_not (interface) Sets the bits in set1 to the bitwise and of the original bits in set1 with the bitwise negation of set2. The sets must have the same number of bits otherwise the result is undefined.
(Specification: stdlib_bitsets)
- Procedures:
-
elemental module subroutine and_not_large(set1, set2)# - Arguments:
- set1type(bitset_large), intent(inout)
- set2type(bitset_large), intent(in)
-
elemental module subroutine and_not_64(set1, set2)# - Arguments:
- set1type(bitset_64), intent(inout)
- set2type(bitset_64), intent(in)
Example
program example_and_not use stdlib_bitsets type(bitset_large) :: set0, set1 call set0 % init(166) call set1 % init(166) call and_not( set0, set1 ) ! none none if ( none(set0) ) write(*,*) 'First test of AND_NOT worked.' call set0 % not() call and_not( set0, set1 ) ! all none if ( all(set0) ) write(*,*) 'Second test of AND_NOT worked.' call set0 % not() call set1 % not() call and_not( set0, set1 ) ! none all if ( none(set0) ) write(*,*) 'Third test of AND_NOT worked.' call set0 % not() call and_not( set0, set1 ) ! all all if ( none(set0) ) write(*,*) 'Fourth test of AND_NOT worked.' end program example_and_not
extract (interface)#
-
extract (interface) Creates a new bitset, new, from a range, start_pos to stop_pos, in bitset old. If start_pos is greater than stop_pos the new bitset is empty. If start_pos is less than zero or stop_pos is greater than bits(old)-1 then if status is present it has the value index_invalid_error and new is undefined, otherwise processing stops with an informative message. (Specification: stdlib_bitsets)
- Procedures:
-
module subroutine extract_large(new, old, start_pos, stop_pos, status)# - Arguments:
- newtype(bitset_large), intent(out)
- oldtype(bitset_large), intent(in)
- start_posinteger(kind=bits_kind), intent(in)
- stop_posinteger(kind=bits_kind), intent(in)
- statusinteger, intent(out), optional
-
module subroutine extract_64(new, old, start_pos, stop_pos, status)# - Arguments:
- newtype(bitset_64), intent(out)
- oldtype(bitset_64), intent(in)
- start_posinteger(kind=bits_kind), intent(in)
- stop_posinteger(kind=bits_kind), intent(in)
- statusinteger, intent(out), optional
Example
program example_extract use stdlib_bitsets type(bitset_large) :: set0, set1 call set0 % init(166) call set0 % set(100,150) call extract( set1, set0, 100, 150) if ( set1 % bits() == 51 ) & write(*,*) 'SET1 has the proper size.' if ( set1 % all() ) write(*,*) 'SET1 has the proper values.' end program example_extract
or (interface)#
-
or (interface) Sets the bits in set1 to the bitwise or of the original bits in set1 and set2. The sets must have the same number of bits otherwise the result is undefined. (Specification: stdlib_bitsets)
-
elemental module subroutine or_large(set1, set2)# - Arguments:
- set1type(bitset_large), intent(inout)
- set2type(bitset_large), intent(in)
-
elemental module subroutine or_64(set1, set2)# - Arguments:
- set1type(bitset_64), intent(inout)
- set2type(bitset_64), intent(in)
Example
program example_or use stdlib_bitsets type(bitset_large) :: set0, set1 call set0 % init(166) call set1 % init(166) call or( set0, set1 ) ! none none if ( none(set0) ) write(*,*) 'First test of OR worked.' call set0 % not() call or( set0, set1 ) ! all none if ( all(set0) ) write(*,*) 'Second test of OR worked.' call set0 % not() call set1 % not() call or( set0, set1 ) ! none all if ( all(set0) ) write(*,*) 'Third test of OR worked.' call set0 % not() call or( set0, set1 ) ! all all if ( all(set0) ) write(*,*) 'Fourth test of OR worked.' end program example_or
-
xor (interface)#
-
xor (interface) Sets the bits in set1 to the bitwise xor of the original bits in set1 and set2. The sets must have the same number of bits otherwise the result is undefined. (Specification: stdlib_bitsets)
-
elemental module subroutine xor_large(set1, set2)# - Arguments:
- set1type(bitset_large), intent(inout)
- set2type(bitset_large), intent(in)
-
elemental module subroutine xor_64(set1, set2)# - Arguments:
- set1type(bitset_64), intent(inout)
- set2type(bitset_64), intent(in)
Example
program example_xor use stdlib_bitsets type(bitset_large) :: set0, set1 call set0 % init(166) call set1 % init(166) call xor( set0, set1 ) ! none none if ( none(set0) ) write(*,*) 'First test of XOR worked.' call set0 % not() call xor( set0, set1 ) ! all none if ( all(set0) ) write(*,*) 'Second test of XOR worked.' call set0 % not() call set1 % not() call xor( set0, set1 ) ! none all if ( all(set0) ) write(*,*) 'Third test of XOR worked.' call set0 % not() call xor( set0, set1 ) ! all all if ( none(set0) ) write(*,*) 'Fourth test of XOR worked.' end program example_xor
-
operator(==) (interface)#
-
operator(==) (interface) Returns .true. if all bits in set1 and set2 have the same value, .false. otherwise. The sets must have the same number of bits otherwise the result is undefined. (Specification: stdlib_bitsets)
-
elemental module function eqv_large(set1, set2) -> eqv# - Arguments:
- set1type(bitset_large), intent(in)
- set2type(bitset_large), intent(in)
- Returns:
- eqvlogical
-
elemental module function eqv_64(set1, set2) -> eqv# - Arguments:
- set1type(bitset_64), intent(in)
- set2type(bitset_64), intent(in)
- Returns:
- eqvlogical
Example
program example_equality use stdlib_bitsets type(bitset_64) :: set0, set1, set2 call set0 % init( 33 ) call set1 % init( 33 ) call set2 % init( 33 ) call set1 % set( 0 ) call set2 % set( 32 ) if ( set0 == set0 .and. set1 == set1 .and. set2 == set2 .and. & .not. set0 == set1 .and. .not. set0 == set2 .and. .not. & set1 == set2 ) then write(*,*) 'Passed 64 bit equality tests.' else error stop 'Failed 64 bit equality tests.' end if end program example_equality
-
operator(/=) (interface)#
-
operator(/=) (interface) Returns .true. if not all bits in set1 and set2 have the same value, .false. otherwise. The sets must have the same number of bits otherwise the result is undefined. (Specification: stdlib_bitsets)
- Procedures:
-
elemental module function neqv_large(set1, set2) -> neqv# - Arguments:
- set1type(bitset_large), intent(in)
- set2type(bitset_large), intent(in)
- Returns:
- neqvlogical
-
elemental module function neqv_64(set1, set2) -> neqv# - Arguments:
- set1type(bitset_64), intent(in)
- set2type(bitset_64), intent(in)
- Returns:
- neqvlogical
Example
program example_inequality use stdlib_bitsets type(bitset_64) :: set0, set1, set2 call set0 % init( 33 ) call set1 % init( 33 ) call set2 % init( 33 ) call set1 % set( 0 ) call set2 % set( 32 ) if ( set0 /= set1 .and. set0 /= set2 .and. set1 /= set2 .and. & .not. set0 /= set0 .and. .not. set1 /= set1 .and. .not. & set2 /= set2 ) then write(*,*) 'Passed 64 bit inequality tests.' else error stop 'Failed 64 bit inequality tests.' end if end program example_inequality
operator(>) (interface)#
-
operator(>) (interface) Returns .true. if the bits in set1 and set2 differ and the highest order different bit is set to 1 in set1 and to 0 in set2, .false. otherwise. The sets must have the same number of bits otherwise the result is undefined. (Specification: stdlib_bitsets)
-
elemental module function gt_large(set1, set2) -> gt# - Arguments:
- set1type(bitset_large), intent(in)
- set2type(bitset_large), intent(in)
- Returns:
- gtlogical
-
elemental module function gt_64(set1, set2) -> gt# - Arguments:
- set1type(bitset_64), intent(in)
- set2type(bitset_64), intent(in)
- Returns:
- gtlogical
Example
program example_gt use stdlib_bitsets type(bitset_64) :: set0, set1, set2 call set0 % init( 33 ) call set1 % init( 33 ) call set2 % init( 33 ) call set1 % set( 0 ) call set2 % set( 32 ) if ( set1 > set0 .and. set2 > set1 .and. set2 > set0 .and. & .not. set0 > set0 .and. .not. set0 > set1 .and. .not. & set1 > set2 ) then write(*,*) 'Passed 64 bit greater than tests.' else error stop 'Failed 64 bit greater than tests.' end if end program example_gt
-
operator(>=) (interface)#
-
operator(>=) (interface) Returns .true. if the bits in set1 and set2 are the same or the highest order different bit is set to 1 in set1 and to 0 in set2, .false. otherwise. The sets must have the same number of bits otherwise the result is undefined. (Specification: stdlib_bitsets)
-
elemental module function ge_large(set1, set2) -> ge# - Arguments:
- set1type(bitset_large), intent(in)
- set2type(bitset_large), intent(in)
- Returns:
- gelogical
-
elemental module function ge_64(set1, set2) -> ge# - Arguments:
- set1type(bitset_64), intent(in)
- set2type(bitset_64), intent(in)
- Returns:
- gelogical
Example
program example_ge use stdlib_bitsets type(bitset_64) :: set0, set1, set2 call set0 % init( 33 ) call set1 % init( 33 ) call set2 % init( 33 ) call set1 % set( 0 ) call set2 % set( 32 ) if ( set1 >= set0 .and. set2 >= set1 .and. set2 >= set0 .and. & set0 >= set0 .and. set1 >= set1 .and. set2 >= set2 .and. & .not. set0 >= set1 .and. .not. set0 >= set2 .and. .not. & set1 >= set2 ) then write(*,*) 'Passed 64 bit greater than or equals tests.' else error stop 'Failed 64 bit greater than or equals tests.' end if end program example_ge
-
operator(<) (interface)#
-
operator(<) (interface) Returns .true. if the bits in set1 and set2 differ and the highest order different bit is set to 0 in set1 and to 1 in set2, .false. otherwise. The sets must have the same number of bits otherwise the result is undefined. (Specification: stdlib_bitsets)
-
elemental module function lt_large(set1, set2) -> lt# - Arguments:
- set1type(bitset_large), intent(in)
- set2type(bitset_large), intent(in)
- Returns:
- ltlogical
-
elemental module function lt_64(set1, set2) -> lt# - Arguments:
- set1type(bitset_64), intent(in)
- set2type(bitset_64), intent(in)
- Returns:
- ltlogical
Example
program example_lt use stdlib_bitsets type(bitset_64) :: set0, set1, set2 call set0 % init( 33 ) call set1 % init( 33 ) call set2 % init( 33 ) call set1 % set( 0 ) call set2 % set( 32 ) if ( set0 < set1 .and. set1 < set2 .and. set0 < set2 .and. & .not. set0 < set0 .and. .not. set2 < set0 .and. .not. & set2 < set1 ) then write(*,*) 'Passed 64 bit less than tests.' else error stop 'Failed 64 bit less than tests.' end if end program example_lt
-
operator(<=) (interface)#
-
operator(<=) (interface) Returns .true. if the bits in set1 and set2 are the same or the highest order different bit is set to 0 in set1 and to 1 in set2, .false. otherwise. The sets must have the same number of bits otherwise the result is undefined. (Specification: stdlib_bitsets)
-
elemental module function le_large(set1, set2) -> le# - Arguments:
- set1type(bitset_large), intent(in)
- set2type(bitset_large), intent(in)
- Returns:
- lelogical
-
elemental module function le_64(set1, set2) -> le# - Arguments:
- set1type(bitset_64), intent(in)
- set2type(bitset_64), intent(in)
- Returns:
- lelogical
Example
program example_le use stdlib_bitsets type(bitset_64) :: set0, set1, set2 call set0 % init( 33 ) call set1 % init( 33 ) call set2 % init( 33 ) call set1 % set( 0 ) call set2 % set( 32 ) if ( set0 <= set1 .and. set1 <= set2 .and. set0 <= set2 .and. & set0 <= set0 .and. set1 <= set1 .and. set2 <= set2 .and. & .not. set1 <= set0 .and. .not. set2 <= set0 .and. .not. & set2 <= set1 ) then write(*,*) 'Passed 64 bit less than or equal tests.' else error stop 'Failed 64 bit less than or equal tests.' end if end program example_le
-
Procedures
bits (function)#
-
elemental function bits(self) -> bits Returns the number of bit positions in self.
- Arguments:
- selfclass(bitset_type), intent(in)
- Returns:
- bitsinteger(kind=bits_kind)
error_handler (subroutine)#
-
module subroutine error_handler(message, error, status, module, procedure) - Arguments:
- messagecharacter(len=*), intent(in)
- errorinteger, intent(in)
- statusinteger, intent(out), optional
- modulecharacter(len=*), intent(in), optional
- procedurecharacter(len=*), intent(in), optional