High Byte

I need to split and integer in to bytes.

I have suceded getting the low byte doing a bitwise AND with 255.

How can i get the high byte of an integer??
LVL 2
PedroMVGomesAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
mrmickConnect With a Mentor Commented:
Function HiByte(Pass As Integer) As Byte

   If (Pass And -2 ^ 15) = 0 Then
      HiByte = Int(Pass / 255)
   Else
      HiByte = ((Pass And 32512) \ 256) Or 128
   End If

End Function

0
 
mrmickCommented:
Simplified a bit...

Function HiByte(i As Integer) As Byte

   If i And -2 ^ 15 Then
      HiByte = ((i And 32512) \ 256) Or 128
   Else
      HiByte = i \ 255
   End If

End Function

0
 
mrmickCommented:
or how about this...

Function HiByte(i As Integer) As Byte
Dim Sign As Boolean

   Sign = i And -2 ^ 15
   HiByte = ((i And 32512) \ 256) Or (128 And Sign)

End Function

Note: All three work!

0
 
alamoCommented:
yes, yet another way, and without stripping the sign bit first

In case you'd like to see it in decimal:

Function HiByte(i As Integer) As Byte

   HiByte = (i And 65280) / 256 And 255

End Function



0
 
mrmickCommented:
How can i do the reverse, this is i have to bytes the low one and the higher one how can a calculate the integer??
0
All Courses

From novice to tech pro — start learning today.