Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default Binary operations (left/right shift, binary and/or, etc.)

How do you do binary operations like left shift, right shift, binary
and, binary or, exclusive or, etc. I'd like to essentially do this
(basic C calculations) in Excel:

x = a 32; // (left shift by 32 bits)
y = a & 0xFFFFFFFF; // (binary AND; i.e. bit mask)

I'm basically trying to get the high-order & low-order DWORDs from a
64-bit int.

--
*mike hodgson* |/ database administrator/ | mallesons stephen jaques
*T* +61 (2) 9296 3668 |* F* +61 (2) 9296 3885 |* M* +61 (408) 675 907
*E* |* W* http://www.mallesons.com


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default Binary operations (left/right shift, binary and/or, etc.)

A small clarification - I mean in a cell formula rather than VBA.

--
*mike hodgson* |/ database administrator/ | mallesons stephen jaques
*T* +61 (2) 9296 3668 |* F* +61 (2) 9296 3885 |* M* +61 (408) 675 907
*E* |* W* http://www.mallesons.com



Mike Hodgson wrote:

How do you do binary operations like left shift, right shift, binary
and, binary or, exclusive or, etc. I'd like to essentially do this
(basic C calculations) in Excel:

x = a 32; // (left shift by 32 bits)
y = a & 0xFFFFFFFF; // (binary AND; i.e. bit mask)

I'm basically trying to get the high-order & low-order DWORDs from a
64-bit int.

--
*mike hodgson* |/ database administrator/ | mallesons stephen jaques
*T* +61 (2) 9296 3668 |* F* +61 (2) 9296 3885 |* M* +61 (408) 675 907
*E* |* W*
http://www.mallesons.com


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Binary operations (left/right shift, binary and/or, etc.)


Hello Mike,

Here are some routines I use when working with DWords in API calls.
think you will find these useful. Place this code in a standard VB
Module and you can use these macros in your code.


Code
-------------------
Public Function HiByte(ByVal w As Integer) As Byte

If w And &H8000 Then
HiByte = &H80 Or ((w And &H7FFF) \ &HFF)
Else
HiByte = w \ 256
End If

End Function

Public Function HiWord(dw As Long) As Integer

If dw And &H80000000 Then
HiWord = (dw \ 65535) - 1
Else
HiWord = dw \ 65535
End If

End Function

Public Function LoByte(w As Integer) As Byte

LoByte = w And &HFF

End Function

Public Function LoWord(dw As Long) As Integer

If dw And &H8000& Then
LoWord = &H8000 Or (dw And &H7FFF&)
Else
LoWord = dw And &HFFFF&
End If

End Function

Public Function LShiftWord(ByVal w As Integer, ByVal C As Integer) As Integer

Dim dw As Long
dw = w * (2 ^ C)
If dw And &H8000& Then
LShiftWord = CInt(dw And &H7FFF&) Or &H8000
Else
LShiftWord = dw And &HFFFF&
End If

End Function

Public Function RShiftWord(ByVal w As Integer, ByVal C As Integer) As Integer

Dim dw As Long
If C = 0 Then
RShiftWord = w
Else
dw = w And &HFFFF&
dw = dw \ (2 ^ C)
RShiftWord = dw And &HFFFF&
End If

End Function

Public Function MakeWord(ByVal bHi As Byte, ByVal bLo As Byte) As Integer

If bHi And &H80 Then
MakeWord = (((bHi And &H7F) * 256) + bLo) Or &H8000
Else
MakeWord = (bHi * 256) + bLo
End If

End Function

Public Function MakeDWord(wHi As Integer, wLo As Integer) As Long

If wHi And &H8000& Then
MakeDWord = (((wHi And &H7FFF&) * 65536) _
Or (wLo And &HFFFF&)) _
Or &H80000000
Else
MakeDWord = (wHi * 65535) + wLo
End If

End Functio
-------------------

--
Leith Ros

-----------------------------------------------------------------------
Leith Ross's Profile: http://www.excelforum.com/member.php...fo&userid=1846
View this thread: http://www.excelforum.com/showthread.php?threadid=37999

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 102
Default Binary operations (left/right shift, binary and/or, etc.)

Mike Hodgson shared this with us in microsoft.public.excel.programming:

A small clarification - I mean in a cell formula rather than VBA.

--
*mike hodgson* |/ database administrator/ | mallesons stephen jaques
*T* +61 (2) 9296 3668 |* F* +61 (2) 9296 3885 |* M* +61 (408) 675 907
*E* |* W*
http://www.mallesons.com



Mike Hodgson wrote:

How do you do binary operations like left shift, right shift,
binary and, binary or, exclusive or, etc. I'd like to essentially
do this (basic C calculations) in Excel:

x = a 32; // (left shift by 32 bits)
y = a & 0xFFFFFFFF; // (binary AND; i.e. bit mask)

I'm basically trying to get the high-order & low-order DWORDs from
a 64-bit int.

--
*mike hodgson* |/ database administrator/ | mallesons stephen jaques
T +61 (2) 9296 3668 |* F* +61 (2) 9296 3885 |* M* +61 (408) 675 907
E |* W*
http://www.mallesons.com


Not directly, but you could use the Dec2Bin and Bin2Dec functions, and
write your own formulae around that. I think you need to install the
Analysis Toolpack.


PS: add a space after your "--" sig separator. It should be "-- ", not
"--" ;-)

--
Amedee Van Gasse
  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,253
Default Binary operations (left/right shift, binary and/or, etc.)



there are no excel function for binary arithmatic.
you have to create VBA user defined functions.

Instead of using VBA math and binary operators
I'd go the API way...

Note: Be carefull with creating your own derivatives
with copymemory, certainly with "As Any" syntax.



Private Declare Sub CopyMemory Lib "kernel32.dll" Alias "RtlMoveMemory"
( _
ByRef Destination As Any, ByRef Source As Any, ByVal Length As Long)

Function HiWord(ByVal dw As Long) As Integer
CopyMemory HiWord, ByVal VarPtr(dw) + 2, 2
End Function
Function LoWord(ByVal dw As Long) As Integer
CopyMemory LoWord, ByVal VarPtr(dw), 2
End Function
Function MakeWord(ByVal HiWord As Integer, _
ByVal LoWord As Integer) As Long
CopyMemory ByVal VarPtr(MakeWord) + 2, HiWord, 2
CopyMemory ByVal VarPtr(MakeWord), LoWord, 2
End Function


--
keepITcool
| www.XLsupport.com | keepITcool chello nl | amsterdam


Mike Hodgson wrote :

A small clarification - I mean in a cell formula rather than VBA.

--
*mike hodgson* |/ database administrator/ | mallesons stephen jaques
*T* +61 (2) 9296 3668 |* F* +61 (2) 9296 3885 |* M* +61 (408) 675 907
*E* |* W*
http://www.mallesons.com



Mike Hodgson wrote:

How do you do binary operations like left shift, right shift,
binary and, binary or, exclusive or, etc. I'd like to essentially
do this (basic C calculations) in Excel:

x = a 32; // (left shift by 32 bits)
y = a & 0xFFFFFFFF; // (binary AND; i.e. bit mask)

I'm basically trying to get the high-order & low-order DWORDs from
a 64-bit int.

--
*mike hodgson* |/ database administrator/ | mallesons stephen jaques
T +61 (2) 9296 3668 |* F* +61 (2) 9296 3885 |* M* +61 (408) 675 907
E |* W*
http://www.mallesons.com

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
Saving as Binary Kevryl Excel Discussion (Misc queries) 1 May 21st 10 06:50 PM
Binary NOT? Mark Scott[_3_] Excel Worksheet Functions 4 January 24th 10 09:21 PM
Hexadecimal to binary [email protected] Excel Discussion (Misc queries) 5 March 4th 06 05:45 PM
Solver returns non binary answer in binary constrained cells Navy Student Excel Worksheet Functions 6 September 1st 05 03:11 PM
Decimal to Binary Himu Excel Worksheet Functions 15 November 22nd 04 07:13 PM


All times are GMT +1. The time now is 10:17 AM.

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

About Us

"It's about Microsoft Excel"