Wrox Programmer Forums integer overflow
 | Search | Today's Posts | Mark Forums Read
 Beginning VB 6 For 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
March 17th, 2004, 07:39 AM
 Registered User Join Date: Feb 2004 Location: , , . Posts: 9 Thanks: 0 Thanked 0 Times in 0 Posts
integer overflow

Hello!

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?

Thanks,

Paul
March 17th, 2004, 08:39 AM
 Friend of Wrox Join Date: Jun 2003 Location: , , United Kingdom. Posts: 1,212 Thanks: 0 Thanked 1 Time in 1 Post

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.
March 17th, 2004, 09:01 AM
 Registered User Join Date: Feb 2004 Location: , , . Posts: 9 Thanks: 0 Thanked 0 Times in 0 Posts

Thanks mate!

 Similar Threads Thread Thread Starter Forum Replies Last Post Overflow grstad Classic ASP Databases 18 December 14th, 2006 10:06 AM regarding Overflow of variable hiruni Access VBA 4 February 27th, 2006 02:17 PM Overflow Error mateenmohd Classic ASP Basics 4 July 5th, 2005 02:12 AM Buffer Overflow rekha_jsr Oracle 3 February 3rd, 2005 08:11 PM can anybody tell why i am getting overflow error tulsi15 SQL Server 2000 1 December 15th, 2004 07:58 AM