Howard Bullock
(KiX Supporter)
2003-12-11 03:51 PM
Bitwise operation help please

I wish to turn off the bit for the value 128 but the and operation is not working like I would expect it to function. Any assistance woulf be appreciated.


$x = 163
? $x
$x = $x | 8
? $x
$x = $x & not 128
? $x

exit 1

I want the value to be 41 after processing. I know IU can subtract 128 in this one case, but why doesn't the bit operator work? I will need it work to have a valid function moving forward.


Richard H.Administrator
(KiX Supporter)
2003-12-11 04:53 PM
Re: Bitwise operation help please

Your problem is that Not is not not.

Not in KiXtart is a logical operator, not a bitwise operator.

There is no boolean not, so you will need emulate it.

Nuff nots.


ShawnAdministrator
(KiX Supporter)
2003-12-11 04:57 PM
Re: Bitwise operation help please

Richard, remember quite some time ago, you and I had a really great discussion about this, and we actually produced some UDF's for bit setting ... I remember searching for it a while ago and damned if I could find it, hoping you would have a better memory and can find it.

Sealeopard
(KiX Master)
2003-12-11 05:01 PM
Re: Bitwise operation help please

For starters: XOR() - Exclusive OR operation using bitwise AND

Richard H.Administrator
(KiX Supporter)
2003-12-11 05:02 PM
Re: Bitwise operation help please

Here's where my memory really lets me down.

Off the top of my head, can't you emulate a NOT by simply subtracting the value from the value of "all-bits".

In other words, if the word size is 8 bits, NOT 128 would be the same as 255-128=127?

If its a DWORD, it would be 65535-128 and so-on.

You can calculate "all-bits" as (2^(WordSize+1))-1


Sealeopard
(KiX Master)
2003-12-11 05:10 PM
Re: Bitwise operation help please

Or maybe "-1 + 128" ?

Howard Bullock
(KiX Supporter)
2003-12-11 05:20 PM
Re: Bitwise operation help please

I can build the function to check if the bit is turned on then if it is subtract the value. But in Perl I can negate a value and hoped that KiXtart supported that as well.

Work around:

;if $SPval & 128
; $SPval = $SPval - 128
;endif

In Perl I can:

$SPval = $SPval & ~128

where "~" is a bitwise negation operator. Off to the "Suggestions" forum.


Richard H.Administrator
(KiX Supporter)
2003-12-12 09:51 AM
Re: Bitwise operation help please

Oops. Thats (2^WordSize)-1.

Quote:

Or maybe "-1 + 128" ?




Not really. KiXtart is loosely typed, so you will get a different result depending on the word size.

In strongly typed languages like 'C', the size of the datum is fixed to (commonly) 8, 16, 32 or 64 bits. This means that you can guarantee the result when you use Not, casting the datum to be absolutely sure.

In this specific case it would work however, as the value is simply used in a bitwise AND operation. It won't work in all cases.


Richard H.Administrator
(KiX Supporter)
2003-12-12 10:17 AM
Re: Bitwise operation help please

Here's the NOT udf I posted in suggestions repeated in full colour (no HTML in suggestions)


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



Ruud van Velsen
(Hey THIS is FUN)
2005-08-23 09:38 PM
Re: Bitwise operation help please

This is in 4.50, so it can be removed from the suggestion list, right?

LonkeroAdministrator
(KiX Master Guru)
2005-08-23 11:10 PM
Re: Bitwise operation help please

indeed, sorry for missing this one.
fixed.