xHarbour Reference Documentation > Function Reference xHarbour Developers Network  

HB_BitSet()

Sets a bit in a numeric integer value to 1.

Syntax

HB_BitSet( <nInteger>, <nPosition> ) --> nResult

Arguments

<nInteger>
This is a numeric integer value.
<nPosition>
This numeric parameter specifies the position of the bit to set to 1. 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 the integer number where the specified bit is set to 1.

Description

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.

Info

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

Example

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

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