| xHarbour Reference Documentation > Function Reference |
![]() |
![]() |
![]() |
Sets a bit in a numeric integer value to 1.
HB_BitSet( <nInteger>, <nPosition> ) --> nResult
The function returns the integer number where the specified bit is set to 1.
Function HB_BitSet() sets a single bit of a numeric integer to 1. If the bit at position <nPosition> is set in <nInteger>, the result has the same value, otherwise its value is different.
| See also: | HB_BitIsSet(), HB_BitReset(), HB_BitShift() |
| Category: | Bitwise functions , xHarbour extensions |
| Source: | rtl\hbBitf.c |
| LIB: | xhb.lib |
| DLL: | xhbdll.dll |
// The example displays the result of HB_BitSet() with the
// help of a user defined function Num2Bit(). The bit sequence
// and the numeric value resulting from the operation is shown.
PROCEDURE Main
LOCAL nInt := 45678
LOCAL n, i
? "Original:", " ", Num2Bit( nInt ), LTrim(Str(nInt))
?
FOR i:= 0 TO 7
n := HB_BitSet( nInt, i )
? "Set Bit: ", LTrim(Str(i)), Num2Bit( n ), LTrim(Str(n))
NEXT
** output
// Original: 01101110 45678
// Set Bit: 0 01101111 45679
// Set Bit: 1 01101110 45678
// Set Bit: 2 01101110 45678
// Set Bit: 3 01101110 45678
// Set Bit: 4 01111110 45694
// Set Bit: 5 01101110 45678
// Set Bit: 6 01101110 45678
// Set Bit: 7 11101110 45806
RETURN
FUNCTION Num2Bit( nNumber )
LOCAL nInt := Int( nNumber )
LOCAL nLen := 7
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