Hi,
Looking at Harbour SetBit(), it looks like it requires either a number of a hex string.
Is there an existing function that can take a binary string like "001110110", and set one of the 0 bits to 1?
cBits := "001110110"
? Chr(AscPos( 3 ) ) // --> "1" : Character at 3rd position
// Now set character at 2nd position to "1"
cBits := PosChar( cBits, "1", 2 )
? cBits // "011110110"
/*******************************************************************************
* Program Id: bit.c
* Version: 1.00
********************************************************************************
*
* Purpose: Sets the given bit in a passed bit string. Returns the previous
* value. Be sure to pass the string by reference. NOTE. In order
* to stay as fast as possible, minimal parameter checking is
* performed. It is up to the user to not be too stupid.
*
* Syntax: bit( <OptC String>, <OptN (1...n) Offset> [, <OptL Set/Clear>] )
*
********************************************************************************
#include <extend.h>
CLIPPER bit( void )
{
unsigned char mask,
*ptr;
unsigned int loc,
offset = _parni( 2 ) - 1,
res = 0;
loc = offset / 8;
if ( loc < _parclen( 1 ) )
{
ptr = _parc( 1 ) + loc;
loc = offset % 8;
res = *ptr << loc & 0x80;
if ( PCOUNT > 2 )
{
mask = (unsigned char ) 0x80 >> loc;
if ( _parl( 3 ) )
*ptr = *ptr | mask;
else
*ptr = *ptr & ~mask;
}
}
_retl( res );
}
nageswaragunupudi wrote:
- Code: Select all Expand view RUN
cBits := "001110110"
? Chr(AscPos( 3 ) ) // --> "1" : Character at 3rd position
// Now set character at 2nd position to "1"
cBits := PosChar( cBits, "1", 2 )
? cBits // "011110110"
#include <hbapi.h>
HB_FUNC( BIT )
{
unsigned char mask,
*ptr;
unsigned int loc,
offset = hb_parni( 2 ) - 1,
res = 0;
loc = offset / 8;
if ( loc < hb_parclen( 1 ) )
{
ptr = hb_parc( 1 ) + loc;
loc = offset % 8;
res = *ptr << loc & 0x80;
if ( hb_pcount() > 2 )
{
mask = (unsigned char ) 0x80 >> loc;
if ( hb_parl( 3 ) )
*ptr = *ptr | mask;
else
*ptr = *ptr & ~mask;
}
}
hb_retl( res );
}
#include "fivewin.ch"
//----------------------------------------------------------------------------//
function Main()
local num1 := 100
local num2 := 50
local result
result := MY_ADD( num1, num2 )
? num1, num2, result
return nil
//----------------------------------------------------------------------------//
#pragma BEGINDUMP
#include <hbapi.h>
HB_FUNC( MY_ADD ) // ( num1, num2 ) --> result
{
int n1 = hb_parni( 1 );
int n2 = hb_parni( 2 );
hb_retni( n1 + n2 );
}
#pragma ENDDUMP
//----------------------------------------------------------------------------//
Return to FiveWin for Harbour/xHarbour
Users browsing this forum: No registered users and 60 guests