Beginning VB 6For coders who are new to Visual Basic, working in VB version 6 (not .NET).
Welcome to the p2p.wrox.com Forums.
You are currently viewing the Beginning VB 6 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
I am trying to modify the bits of an integer (16 bits). What I want to do is to change the 16th bit of that number. So I am doing the following:
MyNumber = MyNumber Or LShift(1, 15) where LShift is the following function:
Public Function LShift(ByVal Value As Integer, _
Count As Integer) As Integer
Dim i As Integer
If Count = 0 Then
LShift = Value
Exit Function
End If
LShift = Value
For i = 1 To Count
LShift = LShift * 2
Next
End Function
The problem is that as soon as the counter gets to 15, the function returns an overflow. So the maximun number I get is 16384. Is there another way to do this?
The usual way to set the nth bit is just to 'Or' the current value with 2^(n-1), but VB's 16-bit integers are signed so the hi-bit (16th bit) denotes the sign. The easiest way to change the sign of an Int is to * -1.