VB.NET中的无符号左移? [英] Unsigned left shift in VB.NET?

查看:117
本文介绍了VB.NET中的无符号左移?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于人们来说,这应该很容易,但是我该如何在使用Option Strict时是VB.NET ?

This should be an easy one for folks, but how do I pull off an unsigned left shift in VB.NET while using Option Strict?

也许我做错了,但是在尝试实现自己的IP2Long功能(出于我的原因)时,我正在测试以确保正确地围绕转换过程.我尝试了一些测试,但所有测试似乎都会导致错误.

Maybe I am doing it wrong, but while trying to implement my own IP2Long function (I have my reasons), I'm testing things to make sure I have my head wrapped around the conversion process properly. I tried a few tests, and all seem to cause 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

最后一个特别令人困惑. 172 << 24仅为2,885,681,152,远低于UInt32数据类型所施加的限制.我的假设是.NET在有符号模式下进行左移,然后尝试将其转换为无符号,这会产生某种错误.

The last one is especially befuddling. 172 << 24 is a mere 2,885,681,152, well under the limit imposed by the UInt32 data type. My assumption is .NET is doing the left-shift in signed mode, then tries to convert, to unsigned, and this tosses up some kind of error.

基本上,我的问题可以归结为:为什么无符号数字有时必须像对.NET框架的黑客一样行事?对于Microsoft来说,使框架固有的无符号数据类型真的很难吗?

Basically, my question boils down to this: why do unsigned numerics have to act like such hacks to the .NET framework at times? Is it really that hard for Microsoft to make unsigned data types intrinsic to the framework?

推荐答案

最后一个特别令人困惑. 172<< 24仅是2,885,681,152, 远远超出了 UInt32数据类型.我的假设是 .NET正在签名左移 模式,然后尝试将其转换为 无符号的,这丢了一些 错误.

The last one is especially befuddling. 172 << 24 is a mere 2,885,681,152, well under the limit imposed by the UInt32 data type. My assumption is .NET is doing the left-shift in signed mode, then tries to convert, to unsigned, and this tosses up some kind of error.

此错误没有错. 172占用8位.您将其移位24位,而第8位现在是第32位.这是一个保留符号位.因此,它在技术上是溢出的.

This error is not wrong. 172 takes up 8 bits. You shift it 24 bits and the 8th bit is now the 32nd bit. This is a reserved sign bit. Therefore it is technically overflowing.

VB.NET 将检查整数溢出,这与 C#不同.

VB.NET will check for an integer overflow unlike in C#.

要使 VB.NET 忽略 OveflowExceptions异常,请转到:

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

编译,其中

vbc foo.vb /removeintchecks


显式状态位操作

如果确定要在代码中保留溢出检查,则必须使用


Explicit state bit operations

If you are determined to leave overflow checks in your code, you have to explicitly tell it you are using bit operations using the BitConverter Class:

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


Visual Basic文字

请记住,您可以添加字面量到您在VB.NET中的代码,并将常量明确声明为无符号.


Visual Basic literals

Also keep in mind you can add literals to your code in VB.NET and explicitly state constants as unsigned.

这篇关于VB.NET中的无符号左移?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆