#110107 - 2003-12-11 03:51 PM
Bitwise operation help please
|
Howard Bullock
KiX Supporter
Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
|
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.
Edited by Howard Bullock (2003-12-11 03:55 PM)
|
Top
|
|
|
|
#110113 - 2003-12-11 05:20 PM
Re: Bitwise operation help please
|
Howard Bullock
KiX Supporter
Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
|
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.
|
Top
|
|
|
|
#110114 - 2003-12-12 09:51 AM
Re: Bitwise operation help please
|
Richard H.
Administrator
Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
|
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.
|
Top
|
|
|
|
#110115 - 2003-12-12 10:17 AM
Re: Bitwise operation help please
|
Richard H.
Administrator
Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
|
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
|
|
Top
|
|
|
|
Moderator: Glenn Barnas, NTDOC, Arend_, Jochen, Radimus, Allen, ShaneEP, Ruud van Velsen, Mart
|
0 registered
and 718 anonymous users online.
|
|
|