| xHarbour Reference Documentation > Function Reference |
![]() |
![]() |
![]() |
Checks if a bit is set in a numeric integer value.
HB_BitIsSet( <nInteger>, <nPosition> ) --> lBitIsSet
The function returns .T. (true) when the bit is set at the specified position, otherwise .F. (false).
Function HB_BitIsSet() tests individual bits of a numeric integer value. This allows, for example, to encode multiple status variables in a single numeric value and detect different values from a single memory variable, or to build the binary character representation for an integer number.
| See also: | HB_BitReset(), HB_BitSet(), HB_BitShift() |
| Category: | Bitwise functions , xHarbour extensions |
| Source: | rtl\hbBitf.c |
| LIB: | xhb.lib |
| DLL: | xhbdll.dll |
// The example implements function Num2Bit() which creates
// a characters string consisting of "0" and "1"
PROCEDURE Main()
? Num2Bit( 1 ) // result: 0000000000000001
? Num2Bit( 64 ) // result: 0000000001000000
? Num2Bit( 1234 ) // result: 0000010011010010
? Num2Bit( 12345678 ) // result: 00000000101111000110000101001110
RETURN
FUNCTION Num2Bit( nNumber )
LOCAL nInt := Int( nNumber )
LOCAL nLen := IIf( Abs(nInt) > 2ˆ15, 31, 15 )
LOCAL cBin := Replicate( "0", nLen+1 )
LOCAL nPos
FOR nPos := 0 TO nLen
IF HB_BitIsSet( nInt, nPos )
cBin[ nLen-nPos+1 ] := "1"
ENDIF
NEXT
RETURN cBin
http://www.xHarbour.com