O Marcos Gambeta me enviou este exemplo para exemplicar como trabalhar com "bits" no harbour:
Código: Selecionar todos
/*
SEQ. NOME TIPO OPICIONAL DESCRIÇÃO
01 Tentativas Integer Não Quantidade de tentativas de receber as informações da impressora.
02 status LongInt Não Número que representa as situações da impressora usando bit flags. <- Aqui...
Status:
stNone = 0
stErro = 1 << 0
stNaoSerial = 1 << 1
stPoucoPapel = 1 << 2
stSemPapel = 1 << 3
stGavetaAberta = 1 << 4
stImprimindo = 1 << 5
stOffLine = 1 << 6
stTampaAberta = 1 << 7
stErroLeitura = 1 << 8
stSlip = 1 << 9
stMICR = 1 << 10
stAguardandoSlip = 1 << 11
stTOF = 1 << 12
stBOF = 1 << 13
*/
procedure main()
local nStatus := 12345 // valor aleatorio para teste
? "DECIMAL=", nStatus
? "BINARIO=", dectobin(nStatus)
?
//
? "VALORES"
? "stNone = 0"
? "stErro = 1 << 0", hb_bitshift(1, 0)
? "stNaoSerial = 1 << 1", hb_bitshift(1, 1)
? "stPoucoPapel = 1 << 2", hb_bitshift(1, 2)
? "stSemPapel = 1 << 3", hb_bitshift(1, 3)
? "stGavetaAberta = 1 << 4", hb_bitshift(1, 4)
? "stImprimindo = 1 << 5", hb_bitshift(1, 5)
? "stOffLine = 1 << 6", hb_bitshift(1, 6)
? "stTampaAberta = 1 << 7", hb_bitshift(1, 7)
? "stErroLeitura = 1 << 8", hb_bitshift(1, 8)
? "stSlip = 1 << 9", hb_bitshift(1, 9)
? "stMICR = 1 << 10", hb_bitshift(1, 10)
? "stAguardandoSlip = 1 << 11", hb_bitshift(1, 11)
? "stTOF = 1 << 12", hb_bitshift(1, 12)
? "stBOF = 1 << 13", hb_bitshift(1, 13)
?
//
? "USANDO HB_BITTEST"
if nStatus == 0
? "stNone = 0"
endif
if hb_bittest(nStatus, 0)
? "stErro = 1 << 0"
endif
if hb_bittest(nStatus, 1)
? "stNaoSerial = 1 << 1"
endif
if hb_bittest(nStatus, 2)
? "stPoucoPapel = 1 << 2"
endif
if hb_bittest(nStatus, 3)
? "stSemPapel = 1 << 3"
endif
if hb_bittest(nStatus, 4)
? "stGavetaAberta = 1 << 4"
endif
if hb_bittest(nStatus, 5)
? "stImprimindo = 1 << 5"
endif
if hb_bittest(nStatus, 6)
? "stOffLine = 1 << 6"
endif
if hb_bittest(nStatus, 7)
? "stTampaAberta = 1 << 7"
endif
if hb_bittest(nStatus, 8)
? "stErroLeitura = 1 << 8"
endif
if hb_bittest(nStatus, 9)
? "stSlip = 1 << 9"
endif
if hb_bittest(nStatus, 10)
? "stMICR = 1 << 10"
endif
if hb_bittest(nStatus, 11)
? "stAguardandoSlip = 1 << 11"
endif
if hb_bittest(nStatus, 12)
? "stTOF = 1 << 12"
endif
if hb_bittest(nStatus, 13)
? "stBOF = 1 << 13"
endif
?
//
? "USANDO HB_BITAND"
if nStatus == 0
? "stNone = 0"
endif
if hb_bitand(nStatus, hb_bitshift(1, 0)) == hb_bitshift(1, 0)
? "stErro = 1 << 0"
endif
if hb_bitand(nStatus, hb_bitshift(1, 1)) == hb_bitshift(1, 1)
? "stNaoSerial = 1 << 1"
endif
if hb_bitand(nStatus, hb_bitshift(1, 2)) == hb_bitshift(1, 2)
? "stPoucoPapel = 1 << 2"
endif
if hb_bitand(nStatus, hb_bitshift(1, 3)) == hb_bitshift(1, 3)
? "stSemPapel = 1 << 3"
endif
if hb_bitand(nStatus, hb_bitshift(1, 4)) == hb_bitshift(1, 4)
? "stGavetaAberta = 1 << 4"
endif
if hb_bitand(nStatus, hb_bitshift(1, 5)) == hb_bitshift(1, 5)
? "stImprimindo = 1 << 5"
endif
if hb_bitand(nStatus, hb_bitshift(1, 6)) == hb_bitshift(1, 6)
? "stOffLine = 1 << 6"
endif
if hb_bitand(nStatus, hb_bitshift(1, 7)) == hb_bitshift(1, 7)
? "stTampaAberta = 1 << 7"
endif
if hb_bitand(nStatus, hb_bitshift(1, 8)) == hb_bitshift(1, 8)
? "stErroLeitura = 1 << 8"
endif
if hb_bitand(nStatus, hb_bitshift(1, 9)) == hb_bitshift(1, 9)
? "stSlip = 1 << 9"
endif
if hb_bitand(nStatus, hb_bitshift(1, 10)) == hb_bitshift(1, 10)
? "stMICR = 1 << 10"
endif
if hb_bitand(nStatus, hb_bitshift(1, 11)) == hb_bitshift(1, 11)
? "stAguardandoSlip = 1 << 11"
endif
if hb_bitand(nStatus, hb_bitshift(1, 12)) == hb_bitshift(1, 12)
? "stTOF = 1 << 12"
endif
if hb_bitand(nStatus, hb_bitshift(1, 13)) == hb_bitshift(1, 13)
? "stBOF = 1 << 13"
endif
return