Page 1 of 1 1
Topic Options
#110118 - 2003-12-11 05:24 PM Need bitwise negation operator
Howard Bullock Offline
KiX Supporter
*****

Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
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.
_________________________
Home page: http://www.kixhelp.com/hb/

Top
#110119 - 2003-12-12 10:12 AM Re: Need bitwise negation operator
Richard H. Administrator Offline
Administrator
*****

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

Top
Page 1 of 1 1


Moderator:  Lonkero, ShaneEP, Jochen, Radimus, Glenn Barnas, Allen, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 142 anonymous users online.
Newest Members
KatieQuinn, Brennnnn, MKubicsek, xguixa, mdress
17681 Registered Users

Generated in 0.029 seconds in which 0.012 seconds were spent on a total of 13 queries. Zlib compression enabled.

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