xHarbour Reference Documentation > Function Reference xHarbour Developers Network  

HB_BitIsSet()

Checks if a bit is set in a numeric integer value.

Syntax

HB_BitIsSet( <nInteger>, <nPosition> ) --> lBitIsSet

Arguments

<nInteger>
This is a numeric integer value to check for set bits.
<nPosition>
This numeric parameter specifies the position of the bit to test. The least significant bit has position zero, so that the range for <nPosition> is 0 to 31 on a 32-bit operating system.

Return

The function returns .T. (true) when the bit is set at the specified position, otherwise .F. (false).

Description

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.

Info

See also:HB_BitReset(), HB_BitSet(), HB_BitShift()
Category: Bitwise functions , xHarbour extensions
Source:rtl\hbBitf.c
LIB:xhb.lib
DLL:xhbdll.dll

Example

// 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

Copyright © 2006-2007 xHarbour.com Inc. All rights reserved.
http://www.xHarbour.com
Created by docmaker.exe