The btest operation checks whether a specific bit in a binary representation of an integer is set or not.
When we say that a “number is set” in the context of the btest operation, it means that a specific bit in the binary representation of the number has a value of 1 (or “on”).
Conversely, if the bit is not set, it means that the bit has a value of 0 (or “off”).
Syntax
RESULT = BTEST(I, POS)
The btest function takes two arguments:
i(integer, intent(in)): This is the number in which you want to test a specific bit. It is declared as an integer and marked asintent(in)to indicate that it is an input argument.pos(integer, intent(in)): This is the position of the bit you want to test within the binary representation of the numberi. It is declared as an integer and marked asintent(in)to indicate that it is an input argument.
The btest function returns a logical value, indicating whether the specified bit is set or not. If the bit is set, the function returns .true. (logical true), and if the bit is not set, it returns .false. (logical false).
Example Program that Demonstrates the btest operation in Fortran
program BTestExample
implicit none
integer :: num, bit
logical :: result
! Prompt the user for input
write(*,*) "Enter a number:"
read(*,*) num
write(*,*) "Enter the bit position (0-31):"
read(*,*) bit
! Perform the btest operation
result = btest(num, bit)
! Display the result
if (result) then
write(*,*) "Bit", bit, "is set in", num
else
write(*,*) "Bit", bit, "is not set in", num
end if
end program BTestExample