VBspeed / Bits / HiByte
VBspeed © 2000-10, updated: 02-Dec-2001
HiByte


Function HiByte
Returns the HiByte (the most significant byte) from a 2-byte Word (aka Integer).
See also LoByte.
A trivial task it seems, however, when you search for HiByte in the internet you'll find more wrong than right solutions. Here are two ubiquitous crap codes:
  Public Function X01HiByte(ByVal Word As Integer)
  ' fails eg. at Word = &H8000
    X01HiByte = Word \ &H100
  End Function

  Public Function X02HiByte(ByVal Word As Integer)
  ' fails eg. at Word = &HFFFF
    X02HiByte = Word \ &H100& And &HFF&
  End Function
  
Code
HiByte01
Public Function HiByte01(ByVal Word As Integer) As Byte
' by Donald, donald@xbeat.net, 20011202
  HiByte01 = (Word And &HFF00&) \ &H100
End Function
HiByte02
Public Function HiByte02(ByVal Word As Integer) As Byte
' by Donald, donald@xbeat.net, 20011202
  CopyMemory HiByte02, ByVal VarPtr(Word) + 1, 1
End Function
HiByte03
Public Function HiByte03(ByVal Word As Integer) As Byte
' by Anonymous, found at http://www.vbapi.com/ref/h/hibyte.html, 20011202
  HiByte03 = Val("&H" & Left(Right("0000" & Hex(Word), 4), 2))
End Function
Calls
1bRet = HiByte(&H1234)
Charts
 VB5 Charts
CodeAuthorDopingNotes
HiByte01 Donald  
HiByte02 DonaldAPI 
HiByte03 Anonymous  
Call 1
11.000.049µs
28.530.417µs
3238.3711.651µs
 VB6 Charts
CodeAuthorDopingNotes
HiByte01 Donald  
HiByte02 DonaldAPI 
HiByte03 Anonymous  
Call 1
11.000.049µs
29.370.458µs
3230.0211.236µs
Conclusions
HiByte02: say no to dope!
HiByte03: I didn't include this gem to diss anybody: although it's more than 200 times slower than necessary it sure earns its credits since it packs a record number of performance sins into one line (and still works 100% correct). This is real cool.
Got comments? How to read all those numbers

top




VBspeed © 2000-10 by Donald Lessau