
; Set or clear iBitPos bit in iValue according to fState.
; Return the previous state of iBitPos bit (0-31) in iValue.

; Function ModifyBit(ByRef iValue As Long,
;                    ByVal iBitPos As Long,
;                    ByVal fState As Boolean) As Boolean

; If the specified bit position is less then zero,
; or greater than 31, the result is undefined.

 .486          ; 32-bit instruction set
 .model flat   ; Protected memory, 4GB limit
 .code         ; Start of code

ModifyBit proc near

    push ebx
    mov ebx,[esp+12] ; extract address of value
    mov ecx,[esp+16] ; position of bit to set
    mov eax,[ebx]    ; extract value to modify
    mov dx, [esp+20] ; new state of bit to set
    test dx, dx
    jz _clear
    bts eax, ecx     ; set the specified bit
    jmp _return
   _clear:
    btr eax, ecx     ; clear the specified bit
   _return:
    mov [ebx],eax    ; assign modified value
    mov edx,[esp+24] ; function return pointer
    sbb eax, eax     ; assign carry flag value
    mov[edx],ax      ; assign the function result
    pop ebx
    xor eax, eax     ; clear eax tell vb all okay
    ret 20           ; cleanup (lpObj, P1, P2, P3, rc)

ModifyBit endp ; end of procedure
end ModifyBit  ; end of code, indicate entry point

; BTS   set operand after copy bit to carry flag
; BTR   reset (clear) operand after " " " " "
