#1   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 1
Default Binary NOT?

Hi

I have successfully created functions for bitwise operators BInary AND, OR
and XOR using the following format in VBA (2010 beta)

Function Bin<operator(a, b)
Bin<operator = a <operator b
End Function

for example the XOR script is:

Function BinXor(a, b)
BinXor = a Xor b
End Function

when I try the same for NOT, VBA comes back with:

Function BinNot(a)
BinNot = Not a
End Function

which is fine as you can only NOT 1 number

The answers are not what I am expecting however, ie the corret outputs are
0=1 and 1=0 whereas the functions output is: 1= -2 and 0 = -1

What do I need to do to make this work or is there a built in "binary flip"
function in Excel?

Regards

Mark


  #2   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 11,058
Default Binary NOT?

The numerical equivalents to True and False are different in VBA than in the
worksheet:

http://groups.google.com/group/micro...746093e7133?q=
--
Gary''s Student - gsnu200909


"Mark Scott" wrote:

Hi

I have successfully created functions for bitwise operators BInary AND, OR
and XOR using the following format in VBA (2010 beta)

Function Bin<operator(a, b)
Bin<operator = a <operator b
End Function

for example the XOR script is:

Function BinXor(a, b)
BinXor = a Xor b
End Function

when I try the same for NOT, VBA comes back with:

Function BinNot(a)
BinNot = Not a
End Function

which is fine as you can only NOT 1 number

The answers are not what I am expecting however, ie the corret outputs are
0=1 and 1=0 whereas the functions output is: 1= -2 and 0 = -1

What do I need to do to make this work or is there a built in "binary flip"
function in Excel?

Regards

Mark


  #4   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 905
Default Binary NOT?

"Mark Scott" wrote:
Function BinNot(a)
BinNot = Not a
End Function

[....]
The answers are not what I am expecting however,
ie the corret outputs are 0=1 and 1=0 whereas the
functions output is: 1= -2 and 0 = -1


They may not be what you expect, but they __are__ the correct results for
the __bitwise__ operation, which you said you want.

Consider binAND(12,10), binOR(12,10) and binXOR(12,10). The results are 8,
14 and 6 respectively, since 12 is 1100 and 10 is 1010 in binary.

I presume that meets your expectations. If not, what do you expect?(!)

binNOT(12) is -13 because the complement of 1100 is 1...10011, and
binNOT(10) is -11 because the complement of 1010 is 1...10101.

Likewise, binNOT(0) is -1 because the complement of 0 is 1...11, and
binNOT(1) is -2 because the complement of 1 is 1...10.

Hopefully that explains your results, and you acquiesce to accept them.

Otherwise (sigh)....

If you want binNOT(0) to be 1 and binNOT(1) to be 0, perhaps you want the
__boolean__ complement, not the bitwise complement.

In that case, you could write --NOT(0) and --NOT(1) in Excel. Or in VBA,
you could write: binNOT = -1 * (a = 0). I would call that boolNOT, not
binNOT. But why bother?

But what would you want for binNOT(12) and binNOT(10)?

boolNOT would return 0 in both cases. Is that what you expect?(!) If so,
--NOT() or boolNOT is indeed the answer you want. But it is __not__ the
bitwise operation.

Or would you expect 3 (0011) and 5 (0101)?

Off-hand, I do not know a fast way to do that. The best I have come up with
so far is:

Function binNOT(a As Long) As Long
Dim mask As Long
If a < 0 Then
Mask = -1
Else
Mask = 1
While (a And Mask) < a: Mask = 2 * Mask + 1: Wend
End If
binNOT = Mask And (Not a)
End Function


----- original message -----

"Mark Scott" wrote:
Hi

I have successfully created functions for bitwise operators BInary AND, OR
and XOR using the following format in VBA (2010 beta)

Function Bin<operator(a, b)
Bin<operator = a <operator b
End Function

for example the XOR script is:

Function BinXor(a, b)
BinXor = a Xor b
End Function

when I try the same for NOT, VBA comes back with:

Function BinNot(a)
BinNot = Not a
End Function

which is fine as you can only NOT 1 number

The answers are not what I am expecting however, ie the corret outputs are
0=1 and 1=0 whereas the functions output is: 1= -2 and 0 = -1

What do I need to do to make this work or is there a built in "binary flip"
function in Excel?

Regards

Mark


  #5   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 905
Default Binary NOT?

I wrote:
If you want binNOT(0) to be 1 and binNOT(1)
to be 0, perhaps you want the __boolean__
complement, not the bitwise complement.

[....]
Or in VBA, you could write: binNOT = -1 * (a = 0).


Or simply binNOT = - (a = 0). (Doh!)


----- original message -----

"Joe User" wrote:
"Mark Scott" wrote:
Function BinNot(a)
BinNot = Not a
End Function

[....]
The answers are not what I am expecting however,
ie the corret outputs are 0=1 and 1=0 whereas the
functions output is: 1= -2 and 0 = -1


They may not be what you expect, but they __are__ the correct results for
the __bitwise__ operation, which you said you want.

Consider binAND(12,10), binOR(12,10) and binXOR(12,10). The results are 8,
14 and 6 respectively, since 12 is 1100 and 10 is 1010 in binary.

I presume that meets your expectations. If not, what do you expect?(!)

binNOT(12) is -13 because the complement of 1100 is 1...10011, and
binNOT(10) is -11 because the complement of 1010 is 1...10101.

Likewise, binNOT(0) is -1 because the complement of 0 is 1...11, and
binNOT(1) is -2 because the complement of 1 is 1...10.

Hopefully that explains your results, and you acquiesce to accept them.

Otherwise (sigh)....

If you want binNOT(0) to be 1 and binNOT(1) to be 0, perhaps you want the
__boolean__ complement, not the bitwise complement.

In that case, you could write --NOT(0) and --NOT(1) in Excel. Or in VBA,
you could write: binNOT = -1 * (a = 0). I would call that boolNOT, not
binNOT. But why bother?

But what would you want for binNOT(12) and binNOT(10)?

boolNOT would return 0 in both cases. Is that what you expect?(!) If so,
--NOT() or boolNOT is indeed the answer you want. But it is __not__ the
bitwise operation.

Or would you expect 3 (0011) and 5 (0101)?

Off-hand, I do not know a fast way to do that. The best I have come up with
so far is:

Function binNOT(a As Long) As Long
Dim mask As Long
If a < 0 Then
Mask = -1
Else
Mask = 1
While (a And Mask) < a: Mask = 2 * Mask + 1: Wend
End If
binNOT = Mask And (Not a)
End Function


----- original message -----

"Mark Scott" wrote:
Hi

I have successfully created functions for bitwise operators BInary AND, OR
and XOR using the following format in VBA (2010 beta)

Function Bin<operator(a, b)
Bin<operator = a <operator b
End Function

for example the XOR script is:

Function BinXor(a, b)
BinXor = a Xor b
End Function

when I try the same for NOT, VBA comes back with:

Function BinNot(a)
BinNot = Not a
End Function

which is fine as you can only NOT 1 number

The answers are not what I am expecting however, ie the corret outputs are
0=1 and 1=0 whereas the functions output is: 1= -2 and 0 = -1

What do I need to do to make this work or is there a built in "binary flip"
function in Excel?

Regards

Mark


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
binary format John Excel Discussion (Misc queries) 2 April 26th 08 01:09 AM
decimal to binary boardmaker Excel Discussion (Misc queries) 4 June 13th 06 09:00 PM
Hexadecimal to binary [email protected] Excel Discussion (Misc queries) 5 March 4th 06 05:45 PM
Binary bit analysis MadManInABox Excel Discussion (Misc queries) 5 September 9th 05 09:08 PM
Solver returns non binary answer in binary constrained cells Navy Student Excel Worksheet Functions 6 September 1st 05 03:11 PM


All times are GMT +1. The time now is 07:13 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"