Howard Bullock
(KiX Supporter)
2003-12-11 05:24 PM
Need bitwise negation operator

To perform bitwise operation like | and & where one needs to turn off a bit we need a bitwise negation operator.


Turn on bit: $x = $x | 64
Turn off bit: $x = $x & ~64

In this example "~" performs a bitwise negation so that performing a bitwise "and" of bits only turn off the bit representing 64.


Richard H.Administrator
(KiX Supporter)
2003-12-12 10:12 AM
Re: Need bitwise negation operator

See my comments in the original thread re word size.

While you are waiting, here is a UDF NOT function:
Code:
Function funNot($dValue,Optional $iWordSize)

; Cast result to correct type
$funNot=CDbl(1)

; Calculate the value of all bits set
If Val($iWordSize) $iWordSize=Val($iWordSize) Else $iWordSize=16 EndIf
While $iWordSize $iWordSize=$iWordSize-1 $funNot=$funNot*2 Loop

$funNot=($funNot-1)-$dValue
EndFunction


To use:
Code:
Turn off bit: $x = $x & funNot(64)



If the size of the mask is important, supply the optional word size:
Code:
"The maximum value of an 8 bit word (byte) is: " funNot(0,8)



I've left the default word size set as 16 bits.