Page 1 of 1 1
Topic Options
#110107 - 2003-12-11 03:51 PM Bitwise operation help please
Howard Bullock Offline
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)
_________________________
Home page: http://www.kixhelp.com/hb/

Top
#110108 - 2003-12-11 04:53 PM Re: Bitwise operation help please
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
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.

Top
#110109 - 2003-12-11 04:57 PM Re: Bitwise operation help please
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
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.
Top
#110110 - 2003-12-11 05:01 PM Re: Bitwise operation help please
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11164
Loc: Boston, MA, USA
For starters: XOR() - Exclusive OR operation using bitwise AND
_________________________
There are two types of vessels, submarines and targets.

Top
#110111 - 2003-12-11 05:02 PM Re: Bitwise operation help please
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
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

Top
#110112 - 2003-12-11 05:10 PM Re: Bitwise operation help please
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11164
Loc: Boston, MA, USA
Or maybe "-1 + 128" ?
_________________________
There are two types of vessels, submarines and targets.

Top
#110113 - 2003-12-11 05:20 PM Re: Bitwise operation help please
Howard Bullock Offline
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.
_________________________
Home page: http://www.kixhelp.com/hb/

Top
#110114 - 2003-12-12 09:51 AM Re: Bitwise operation help please
Richard H. Administrator Offline
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 Offline
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
#110116 - 2005-08-23 09:38 PM Re: Bitwise operation help please
Ruud van Velsen Moderator Offline
Developer
*****

Registered: 1999-05-06
Posts: 391
Loc: Amsterdam, The Netherlands
This is in 4.50, so it can be removed from the suggestion list, right?
Top
#110117 - 2005-08-23 11:10 PM Re: Bitwise operation help please
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
indeed, sorry for missing this one.
fixed.
_________________________
!

download KiXnet

Top
Page 1 of 1 1


Moderator:  Glenn Barnas, NTDOC, Arend_, Jochen, Radimus, Allen, ShaneEP, Ruud van Velsen, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 557 anonymous users online.
Newest Members
gespanntleuchten, DaveatAdvanced, Paulo_Alves, UsTaaa, xxJJxx
17864 Registered Users

Generated in 0.062 seconds in which 0.025 seconds were spent on a total of 12 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org