Set individual bit

Can we set individual bit something like this: https://www.quora.com/How-do-you-set-clear-and-toggle-a-single-bit-in-C ?

First keep in mind that all numbers in AmiBroker are IEEE floating point which means only 24 bits (mantissa) are usable. The rest is exponent. Typically bit manipulation is done on integers. Having said that, yes you can turn ‘bits’ like this:

function BitValue( bit )
{
  return  ( 2 ^ bit );
}

function SetBit( value, bit )
{
  return  value | BitValue( bit );
}

function ClearBit( value, bit )
{
  bv = BitValue( bit );
  return  ( value | bv ) - bv;
}


allbits = 0;
clearbits = 16777215;

for( i = 0; i < 24; i++ )
{
   bit = BitValue( i );
   allbits = allbits | bit;
   clearbits = ClearBit( clearbits, i );
   printf("Set Bit %g single result %.9g, ORed result = %.9g, cleared %.9g\n", i, bit, allbits, clearbits );
   
}
1 Like
Function ToggleBit(value,indexbit) 
{
 return ((-1 - value) & (2^indexbit)) | (value & (-1 - (2^indexbit)));
}

My ClearBit:
Function ClearBit(value,indexbit) 
{
 return value & -1-(2^indexbit);
}