What's the best way to set individual bits, in a bit string

What's the best way to set individual bits, in a bit string

Postby FWExplorer » Fri Feb 26, 2021 8:00 pm

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?
FWExplorer
 
Posts: 100
Joined: Fri Aug 09, 2013 12:43 am

Re: What's the best way to set individual bits, in a bit string

Postby nageswaragunupudi » Sat Feb 27, 2021 10:11 am

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"
 
Regards

G. N. Rao.
Hyderabad, India
User avatar
nageswaragunupudi
 
Posts: 10646
Joined: Sun Nov 19, 2006 5:22 am
Location: India

Re: What's the best way to set individual bits, in a bit string

Postby FWExplorer » Sat Feb 27, 2021 3:11 pm

Nages,

Thanks, of course we can do it that way. Sorry, I left out that I was looking for a C alternative. I guess my real question is how to convert Joe Booth's Bit.c to Harbour:


Code: Select all  Expand view  RUN
/*******************************************************************************
* 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"
 
FWExplorer
 
Posts: 100
Joined: Fri Aug 09, 2013 12:43 am

Re: What's the best way to set individual bits, in a bit string

Postby nageswaragunupudi » Sun Feb 28, 2021 4:31 am

Code: Select all  Expand view  RUN
#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 );
}
 
Regards

G. N. Rao.
Hyderabad, India
User avatar
nageswaragunupudi
 
Posts: 10646
Joined: Sun Nov 19, 2006 5:22 am
Location: India

Re: What's the best way to set individual bits, in a bit string

Postby FWExplorer » Sun Feb 28, 2021 3:46 pm

Thanks, nages

I badly want to understand the Harbour C api. There's certainly plenty of documentation in terms of reference material, and a few examples from the Clipper days and the Harbour implementation.

But I'm so far from understanding it, and there's no clear path to being an expert. Months back, Antonio put together a full-working sample, within a couple of days, that interfaced to a Prolog dll library. I could NEVER have figured out how to construct Antonio's interface.

There doesn't seem to be any step-by-step tutorial and exercise guide for coding in the Harbour C api, along with any Fivewin extensions.


Anyway, thanks for taking the time to code this, it'll be useful.
FWExplorer
 
Posts: 100
Joined: Fri Aug 09, 2013 12:43 am

Re: What's the best way to set individual bits, in a bit string

Postby nageswaragunupudi » Sun Feb 28, 2021 4:04 pm

Please study "c" programs in fwh\samples\function folder.
Regards

G. N. Rao.
Hyderabad, India
User avatar
nageswaragunupudi
 
Posts: 10646
Joined: Sun Nov 19, 2006 5:22 am
Location: India

Re: What's the best way to set individual bits, in a bit string

Postby FWExplorer » Sun Feb 28, 2021 6:01 pm

Thanks nage,

There's no function folder in samples.

if you mean the .c code in the ..\samples and subfolders, yes that's helpful.

But nobody - as far as I know - has put together any kind structured tutorial document with short samples and maybe exercises. All we have is reference documentation and samples, which leaves it up a random process, and there's always a chance that the developer might miss something fundamental.

Let me think about this a bit. Maybe I can start the docs, from the vantage point of someone who used the api and experimented a little, but needs to develop a foundation.
FWExplorer
 
Posts: 100
Joined: Fri Aug 09, 2013 12:43 am

Re: What's the best way to set individual bits, in a bit string

Postby nageswaragunupudi » Sun Feb 28, 2021 6:21 pm

Very sorry:
fwh\source\function
Regards

G. N. Rao.
Hyderabad, India
User avatar
nageswaragunupudi
 
Posts: 10646
Joined: Sun Nov 19, 2006 5:22 am
Location: India

Re: What's the best way to set individual bits, in a bit string

Postby nageswaragunupudi » Sun Feb 28, 2021 6:33 pm

If you are interested, you can make a beginning with this sample:
Code: Select all  Expand view  RUN
#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

//----------------------------------------------------------------------------//
 


Save this to fwh\samples folder and build with buildh.bat.

After this, you can try adding functions like MY_MULT(), MY_DIVIDE(), etc.
Then you can make more complex functions.
Regards

G. N. Rao.
Hyderabad, India
User avatar
nageswaragunupudi
 
Posts: 10646
Joined: Sun Nov 19, 2006 5:22 am
Location: India

Re: What's the best way to set individual bits, in a bit string

Postby FWExplorer » Sun Feb 28, 2021 7:30 pm

Yes, that's a nice sample to start with, thanks.
FWExplorer
 
Posts: 100
Joined: Fri Aug 09, 2013 12:43 am


Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: No registered users and 60 guests