Unsigned left shift in VB.NET?

This should be easy for people, but how can I remove the unsigned left shift in VB.NET when usingOption Strict

Maybe I'm doing it wrong, but trying to implement my own IP2Long function (I have my own reasons), I am testing things to make sure that I have a head wrapped around the conversion process properly. I tried several tests and everything seems to be causing errors.

Dim a As Int32
a = CUint(172 << 24) 'Constant expression not representable in type 'UInteger'
a = DirectCast((172 << 24), UInt32) 'Value of type 'Integer' cannot be converted to 'UInteger'
a = Convert.ToUInt32(172 << 24) 'Compiles, but throws an OverflowException

The latter is especially clogging. 172 << 24- this is only 2 855 681 152 people, which corresponds to the limit imposed by the UInt32 data type. My guess: .NET performs a left shift in signed mode, then tries to convert to unsigned, and this causes some error.

Basically, my question boils down to the following: why should unsigned numbers occasionally affect such hacks on the .NET platform? Is it really hard for Microsoft to create unsigned data types that are an integral part of the framework?

+2
source share
1 answer

The latter is especially clogging. 172 <24 - only 2 855 681 152, well under the limit imposed by the UInt32 Data Type. My guess is .NET does a left shift in signed mode, then tries to convert to unsigned, and this throws some kind of error.

This error is not an error. 172 takes 8 bits. You shift it by 24 bits, and the eighth bit - by the 32nd bit. This is a reserved sign bit. Therefore, it is technically crowded.


Handle integers like C #

VB.NET , #.

VB.NET Oveflow:

Project properties->Compile->Advanced Compiler Option->"Remove integer overflow checks"

vbc foo.vb /removeintchecks

, , , BitConverter :

''//This will work
a = BitConverter.ToInt32(BitConverter.GetBytes(172 << 24), 0) 

Visual Basic

, VB.NET unsigned.

+4

All Articles