Overflow exceptions when converting from Long to Integer? 
Author Message
 Overflow exceptions when converting from Long to Integer?

Hello,

This test gives an 'OverflowException':

Dim myLong As Long = Int64.MaxValue
Dim myInteger As Integer = CInt(myLong)
Console.WriteLine("Initeger of {0} is {1}", myLong, myInteger)

I want to be able to convert a Long to Integer without checks for overflow.
In other words, I want only the lower 32-bits copied to the Integer. Is
there a neat way to do this?

Thanks for any help.



Wed, 18 Jun 2003 10:25:52 GMT  
 Overflow exceptions when converting from Long to Integer?

Quote:
> I want to be able to convert a Long to Integer without checks for
overflow.
> In other words, I want only the lower 32-bits copied to the Integer.

First of all, that's not a conversion from Long to Integer. Copying the
lower 32 bits from a 64 bit array is not the same as converting from one
number format to another. Both Long and Integers are numbers, not bit
fields, and should be treated as such.

However since BitAnd doesn't support UInts (the proper data type for bit
fields) this will have to do.

Dim nIn As Long = Long.MaxValue

Dim nOut As Integer = CInt(nIn BitAnd &HFFFF)
                                     [3]            [2]        [1]

1: I actually wrote this bit mask as 64 bits  (&H0000FFFF), but the IDE
trimed the leading zeros.
2: This is used to apply the mask to the in value.
3: This is used to coherce the result of the bit mask into an integer.

--
Jonathan Allen

This industry is always changing and whining won't stop it,
so either bail out now or enjoy the ride.


Quote:
> Hello,

> This test gives an 'OverflowException':

> Dim myLong As Long = Int64.MaxValue
> Dim myInteger As Integer = CInt(myLong)
> Console.WriteLine("Initeger of {0} is {1}", myLong, myInteger)

> I want to be able to convert a Long to Integer without checks for
overflow.
> In other words, I want only the lower 32-bits copied to the Integer. Is
> there a neat way to do this?

> Thanks for any help.



Wed, 18 Jun 2003 12:09:57 GMT  
 Overflow exceptions when converting from Long to Integer?
Thanks but unfortunately doesn't seem to work. With

Dim nIn As Long = Long.MaxValue
Dim nOut As Integer = CInt(nIn BitAnd &HFFFFFFFF)

Note changed &HFFFF to &HFFFFFFFF as I needed 32-bits not 16, I still get
the overflow exception.
It appears that the sign bit is the problem as &H7FFFFFFF works, so I can
copy the bottom 31-bits but not the last bit.

Hmmm, there must be a simple answer to this.


Quote:
> > I want to be able to convert a Long to Integer without checks for
> overflow.
> > In other words, I want only the lower 32-bits copied to the Integer.

> First of all, that's not a conversion from Long to Integer. Copying the
> lower 32 bits from a 64 bit array is not the same as converting from one
> number format to another. Both Long and Integers are numbers, not bit
> fields, and should be treated as such.

> However since BitAnd doesn't support UInts (the proper data type for bit
> fields) this will have to do.

> Dim nIn As Long = Long.MaxValue

> Dim nOut As Integer = CInt(nIn BitAnd &HFFFF)
>                                      [3]            [2]        [1]

> 1: I actually wrote this bit mask as 64 bits  (&H0000FFFF), but the IDE
> trimed the leading zeros.
> 2: This is used to apply the mask to the in value.
> 3: This is used to coherce the result of the bit mask into an integer.

> --
> Jonathan Allen

> This industry is always changing and whining won't stop it,
> so either bail out now or enjoy the ride.



> > Hello,

> > This test gives an 'OverflowException':

> > Dim myLong As Long = Int64.MaxValue
> > Dim myInteger As Integer = CInt(myLong)
> > Console.WriteLine("Initeger of {0} is {1}", myLong, myInteger)

> > I want to be able to convert a Long to Integer without checks for
> overflow.
> > In other words, I want only the lower 32-bits copied to the Integer. Is
> > there a neat way to do this?

> > Thanks for any help.



Wed, 18 Jun 2003 12:55:14 GMT  
 Overflow exceptions when converting from Long to Integer?


Quote:

> Dim nIn As Long = Long.MaxValue

> Dim nOut As Integer = CInt(nIn BitAnd &HFFFF)
>                                      [3]            [2]        [1]

> 1: I actually wrote this bit mask as 64 bits  (&H0000FFFF), but the IDE
> trimed the leading zeros.

What ?????? 32 bits &HFFFFFFFF, 64 bits &HFFFFFFFFFFFFFFFF.

An integer in VB.NET is 32 bit, hence the mask should be &HFFFFFFFF



Wed, 18 Jun 2003 12:53:44 GMT  
 Overflow exceptions when converting from Long to Integer?
Hi Tony,

try to mask the Int32 sign bit, and subtract if necessary. eg:

Dim myInteger As Int32 = CInt((mylong BitAnd &HFFFFFFFFL) - ((mylong BitAnd
&H80000000L) * &H2))


Quote:
> Thanks but unfortunately doesn't seem to work. With

> Dim nIn As Long = Long.MaxValue
> Dim nOut As Integer = CInt(nIn BitAnd &HFFFFFFFF)

> Note changed &HFFFF to &HFFFFFFFF as I needed 32-bits not 16, I still get
> the overflow exception.
> It appears that the sign bit is the problem as &H7FFFFFFF works, so I can
> copy the bottom 31-bits but not the last bit.

> Hmmm, there must be a simple answer to this.



> > > I want to be able to convert a Long to Integer without checks for
> > overflow.
> > > In other words, I want only the lower 32-bits copied to the Integer.

> > First of all, that's not a conversion from Long to Integer. Copying the
> > lower 32 bits from a 64 bit array is not the same as converting from one
> > number format to another. Both Long and Integers are numbers, not bit
> > fields, and should be treated as such.

> > However since BitAnd doesn't support UInts (the proper data type for bit
> > fields) this will have to do.

> > Dim nIn As Long = Long.MaxValue

> > Dim nOut As Integer = CInt(nIn BitAnd &HFFFF)
> >                                      [3]            [2]        [1]

> > 1: I actually wrote this bit mask as 64 bits  (&H0000FFFF), but the IDE
> > trimed the leading zeros.
> > 2: This is used to apply the mask to the in value.
> > 3: This is used to coherce the result of the bit mask into an integer.

> > --
> > Jonathan Allen

> > This industry is always changing and whining won't stop it,
> > so either bail out now or enjoy the ride.



> > > Hello,

> > > This test gives an 'OverflowException':

> > > Dim myLong As Long = Int64.MaxValue
> > > Dim myInteger As Integer = CInt(myLong)
> > > Console.WriteLine("Initeger of {0} is {1}", myLong, myInteger)

> > > I want to be able to convert a Long to Integer without checks for
> > overflow.
> > > In other words, I want only the lower 32-bits copied to the Integer.
Is
> > > there a neat way to do this?

> > > Thanks for any help.



Wed, 18 Jun 2003 13:14:47 GMT  
 Overflow exceptions when converting from Long to Integer?


Quote:
> Thanks but unfortunately doesn't seem to work. With

> Dim nIn As Long = Long.MaxValue
> Dim nOut As Integer = CInt(nIn BitAnd &HFFFFFFFF)

> Note changed &HFFFF to &HFFFFFFFF as I needed 32-bits not 16, I still get
> the overflow exception.
> It appears that the sign bit is the problem as &H7FFFFFFF works, so I can
> copy the bottom 31-bits but not the last bit.

> Hmmm, there must be a simple answer to this.

Probably.  For a not-so-simple method you can always overlay the values:

Imports System.Runtime.InteropServices

Structure <StructLayout(LayoutKind.Explicit)> Tester
  Public <FieldOffset(0)> All64 As Int64
  Public <FieldOffset(0)> First32 As Int32
  Public <FieldOffset(4)> Second32 As Int32
End Structure

Dim t As Tester
t.All64 = Int64.MaxValue
msgbox(t.First32.tostring)
msgbox(t.Second32.tostring)



Wed, 18 Jun 2003 13:39:17 GMT  
 Overflow exceptions when converting from Long to Integer?
Hi Bob,


Quote:

> Imports System.Runtime.InteropServices

> Structure <StructLayout(LayoutKind.Explicit)> Tester
>   Public <FieldOffset(0)> All64 As Int64
>   Public <FieldOffset(0)> First32 As Int32
>   Public <FieldOffset(4)> Second32 As Int32
> End Structure

> Dim t As Tester
> t.All64 = Int64.MaxValue
> msgbox(t.First32.tostring)
> msgbox(t.Second32.tostring)

I like it !!  Kind of like the LSet you have when you haven't got LSet <bg>


Wed, 18 Jun 2003 13:46:44 GMT  
 Overflow exceptions when converting from Long to Integer?
Hi again Tony,

apart from my original answer and Bob's answer (BTW: I like Bob's answer
better), you could also go down the path of using copymemory api, using a
specific declare, eg:

Public Declare Sub CopyLowWordFromInt64toInt32 Lib "kernel32" Alias
"RtlMoveMemory" (ByRef Destination As Int32, ByRef Source As Int64, ByVal
Length As Int32)

Dim nIn As Long = Long.MaxValue
Dim nOut As Integer

CopyLowWordFromInt64toInt32(nOut, nIn, 4)


Quote:
> Thanks but unfortunately doesn't seem to work. With

> Dim nIn As Long = Long.MaxValue
> Dim nOut As Integer = CInt(nIn BitAnd &HFFFFFFFF)

> Note changed &HFFFF to &HFFFFFFFF as I needed 32-bits not 16, I still get
> the overflow exception.
> It appears that the sign bit is the problem as &H7FFFFFFF works, so I can
> copy the bottom 31-bits but not the last bit.

> Hmmm, there must be a simple answer to this.



Wed, 18 Jun 2003 13:54:37 GMT  
 Overflow exceptions when converting from Long to Integer?
Nice, definitely something to add to my notes.

--
Jonathan Allen

This industry is always changing and whining won't stop it,
so either bail out now or enjoy the ride.


Quote:



> > Thanks but unfortunately doesn't seem to work. With

> > Dim nIn As Long = Long.MaxValue
> > Dim nOut As Integer = CInt(nIn BitAnd &HFFFFFFFF)

> > Note changed &HFFFF to &HFFFFFFFF as I needed 32-bits not 16, I still
get
> > the overflow exception.
> > It appears that the sign bit is the problem as &H7FFFFFFF works, so I
can
> > copy the bottom 31-bits but not the last bit.

> > Hmmm, there must be a simple answer to this.

> Probably.  For a not-so-simple method you can always overlay the values:

> Imports System.Runtime.InteropServices

> Structure <StructLayout(LayoutKind.Explicit)> Tester
>   Public <FieldOffset(0)> All64 As Int64
>   Public <FieldOffset(0)> First32 As Int32
>   Public <FieldOffset(4)> Second32 As Int32
> End Structure

> Dim t As Tester
> t.All64 = Int64.MaxValue
> msgbox(t.First32.tostring)
> msgbox(t.Second32.tostring)



Wed, 18 Jun 2003 14:24:27 GMT  
 Overflow exceptions when converting from Long to Integer?
Sorry, I didn't realize that I had Ignore Integer Overflows still checked.

--
Jonathan Allen

This industry is always changing and whining won't stop it,
so either bail out now or enjoy the ride.


Quote:
> Thanks but unfortunately doesn't seem to work. With

> Dim nIn As Long = Long.MaxValue
> Dim nOut As Integer = CInt(nIn BitAnd &HFFFFFFFF)

> Note changed &HFFFF to &HFFFFFFFF as I needed 32-bits not 16, I still get
> the overflow exception.
> It appears that the sign bit is the problem as &H7FFFFFFF works, so I can
> copy the bottom 31-bits but not the last bit.

> Hmmm, there must be a simple answer to this.



> > > I want to be able to convert a Long to Integer without checks for
> > overflow.
> > > In other words, I want only the lower 32-bits copied to the Integer.

> > First of all, that's not a conversion from Long to Integer. Copying the
> > lower 32 bits from a 64 bit array is not the same as converting from one
> > number format to another. Both Long and Integers are numbers, not bit
> > fields, and should be treated as such.

> > However since BitAnd doesn't support UInts (the proper data type for bit
> > fields) this will have to do.

> > Dim nIn As Long = Long.MaxValue

> > Dim nOut As Integer = CInt(nIn BitAnd &HFFFF)
> >                                      [3]            [2]        [1]

> > 1: I actually wrote this bit mask as 64 bits  (&H0000FFFF), but the IDE
> > trimed the leading zeros.
> > 2: This is used to apply the mask to the in value.
> > 3: This is used to coherce the result of the bit mask into an integer.

> > --
> > Jonathan Allen

> > This industry is always changing and whining won't stop it,
> > so either bail out now or enjoy the ride.



> > > Hello,

> > > This test gives an 'OverflowException':

> > > Dim myLong As Long = Int64.MaxValue
> > > Dim myInteger As Integer = CInt(myLong)
> > > Console.WriteLine("Initeger of {0} is {1}", myLong, myInteger)

> > > I want to be able to convert a Long to Integer without checks for
> > overflow.
> > > In other words, I want only the lower 32-bits copied to the Integer.
Is
> > > there a neat way to do this?

> > > Thanks for any help.



Wed, 18 Jun 2003 14:23:46 GMT  
 Overflow exceptions when converting from Long to Integer?

Quote:
> What ?????? 32 bits &HFFFFFFFF, 64 bits &HFFFFFFFFFFFFFFFF.

> An integer in VB.NET is 32 bit, hence the mask should be &HFFFFFFFF

Congratulations, you finally caught me making a mistake.

--
Jonathan Allen

This industry is always changing and whining won't stop it,
so either bail out now or enjoy the ride.


Quote:



> > Dim nIn As Long = Long.MaxValue

> > Dim nOut As Integer = CInt(nIn BitAnd &HFFFF)
> >                                      [3]            [2]        [1]

> > 1: I actually wrote this bit mask as 64 bits  (&H0000FFFF), but the IDE
> > trimed the leading zeros.

> What ?????? 32 bits &HFFFFFFFF, 64 bits &HFFFFFFFFFFFFFFFF.

> An integer in VB.NET is 32 bit, hence the mask should be &HFFFFFFFF



Wed, 18 Jun 2003 14:25:19 GMT  
 Overflow exceptions when converting from Long to Integer?


Quote:
> Sorry, I didn't realize that I had Ignore Integer Overflows still checked.

> --

Bzzzzzttt.. wrong again Jonathan.  It had nothing to do with having Ignore
Integer Overflows still checked. Your code was wrong because you don't seem
to understand how many bits there are in hex notation.


Wed, 18 Jun 2003 17:38:02 GMT  
 Overflow exceptions when converting from Long to Integer?


Quote:

> Congratulations, you finally caught me making a mistake.

Wrong again <geez>. It's not that I've finally caught you, (hell, it seems
lately your more wrong than right), the thing here is that you finally
realised you were wrong (even if you still don't understand why)


Wed, 18 Jun 2003 17:39:44 GMT  
 Overflow exceptions when converting from Long to Integer?
Thanks for all your help. I've done lots of searching and found the
BitConverter class. The code:

    Sub Main()
        Dim myLong As Long = Int64.MaxValue
        myLong = Int64.MaxValue
        Dim myInteger As Integer =
BitConverter.ToInt32(BitConverter.GetBytes(myLong), 0)
        Console.WriteLine("(int) {0} = {1}", myLong, myInteger)
    End Sub

copies the lower 32-bits of  a Long to Integer which is exactly what I want.
Even looks quite elegant!
By changing to 'BitConverter.ToInt32(BitConverter.GetBytes(myLong), 4)',  I
can get the upper 32-bits as well.

Thanks,
Tony



Wed, 18 Jun 2003 23:26:29 GMT  
 Overflow exceptions when converting from Long to Integer?

Quote:
> Your code was wrong because you don't seem
> to understand how many bits there are in hex notation.

I wasn't concerned about that because it was a minor error and easily
caught. The fact that it doesn't work even with the correct mask because of
integer overflow checking is much more serious. Sure I made two mistakes,
but it's the one that can't be corrected that troubled me.

--
Jonathan Allen

This industry is always changing and whining won't stop it,
so either bail out now or enjoy the ride.


Quote:



> > Sorry, I didn't realize that I had Ignore Integer Overflows still
checked.

> > --

> Bzzzzzttt.. wrong again Jonathan.  It had nothing to do with having Ignore
> Integer Overflows still checked. Your code was wrong because you don't
seem
> to understand how many bits there are in hex notation.



Sat, 21 Jun 2003 09:58:03 GMT  
 
 [ 16 post ]  Go to page: [1] [2]

 Relevant Pages 

1. Long = Integer * Integer Overflow error

2. Converting integers and long integers to byte arrays

3. Converting integer (or long) data type to array of bytes

4. Help: converting long integer colour values to RGB

5. How to Convert a Long Integer (High/Lov)?

6. Convert long integer?

7. HELP: Converting to time_t (long integer) taking into account Daylight Hours

8. font constructor exception: overflow or underflow

9. - Exception (stack overflow) while implementing UserControl_AmbientChanged

10. Is there a limit on a integer variable (overflow error)

11. Integer overflow traps

12. unsigned 32 bits integer arithmetic / overflow

 

 
Powered by phpBB® Forum Software