HiWords and LoWords from Long Values

 

HiWords and LoWords from Long Values

 When translating C code to VB, you quite often come across the HiWord and LoWord operators, used to pack two integers into a long value. A simple translation of HiWord code will run into difficulties when unsigned integer arithmetic is being 
used in the C code and the highest bit of the long value can be set. Since VB doesn注释:t support unsigned arithmetic we have to strip out the high bit and add it back again later to avoid overflows and misleading results. 
 (当把C代码翻译到VB时,你经常会遇到高字和低字的操作,通常是把两个整型值合成一个长整型。当在C代码中使用了无符号整型,这最高一位是可以被设置的,因此简单的直接转换高字将会遇到困难。由于VB不支持无符号算术符,我们只好剥去这最高一位,在稍后再把它加回来,以避免溢出和错误的结果。)

 Start a new project then add a class module. Add the following code to the module: 
 (开始一个新工程,增添一个类模块。把以下代码写入模块:)

Public Property Get LoWord(ByRef lThis As Long) As Long
  LoWord = (lThis And &HFFFF&)
End Property

Public Property Let LoWord(ByRef lThis As Long, ByVal lLoWord As Long)
  lThis = lThis And Not &HFFFF& Or lLoWord
End Property

Public Property Get HiWord(ByRef lThis As Long) As Long
  If (lThis And &H80000000) = &H80000000 Then
   HiWord = ((lThis And &H7FFF0000) / &H10000) Or &H8000&
  Else
   HiWord = (lThis And &HFFFF0000) / &H10000
  End If
End Property

Public Property Let HiWord(ByRef lThis As Long, ByVal lHiWord As Long)
  If (lHiWord And &H8000&) = &H8000& Then
   lThis = lThis And Not &HFFFF0000 Or ((lHiWord And &H7FFF&) * &H10000) Or &H80000000
  Else
   lThis = lThis And Not &HFFFF0000 Or (lHiWord * &H10000)
  End If
End Property

  调用时,假定类模块取名CHiLo

Dim chl As New CHiLo
Dim a As Long
chl.LoWord(a) = &HFFFF0
chl.HiWord(a) = &HFFFF1
Me.Caption = Hex(a)

  如此便可以正确无误地将高字和低字合成在一起。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值