VB.NET和C#的二进制移位的差异 [英] Binary Shift Differences between VB.NET and C#

查看:237
本文介绍了VB.NET和C#的二进制移位的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚发现了一些翻译数据之间的一个有趣的问题:

I just found an interesting problem between translating some data:

VB.NET: CByte(4)<< 8 返回4

VB.NET: CByte(4) << 8 Returns 4

但C#:(字节)4℃;&LT; 8 返回1​​024

But C#: (byte)4 << 8 Returns 1024

也就是说,为什么VB.NET:(CByte(4)&LT;&LT; 8).GetType()返回类型 {名称= 字节全名=System.Byte}

Namely, why does VB.NET: (CByte(4) << 8).GetType() return type {Name = "Byte" FullName = "System.Byte"}

然而,C#:((字节)4℃;&LT; 8).GetType()返回类型为 {NAME =的Int32全名= System.Int32的}

Yet C#: ((byte)4 << 8).GetType() returns type {Name = "Int32" FullName = "System.Int32"}

还有一个原因,为什么这两种对待二进制移位一样吗?从下面,有没有什么办法让C#位移位执行相同的VB.NET(使VB.NET执行,如C#你只是做 CINT(_____)&LT;&LT; 8 )?

Is there a reason why these two treat the binary shift the same? Following from that, is there any way to make the C# bit shift perform the same as VB.NET (to make VB.NET perform like C# you just do CInt(_____) << 8)?

推荐答案

据<一href="http://msdn.microsoft.com/en-us/library/a1sway8w.aspx">http://msdn.microsoft.com/en-us/library/a1sway8w.aspx字节不具有&其中;&其中;它定义为C#(仅INT,UINT,长,ULONG,这意味着它将使用implciit转换为一个类型,它可以使用,因此将其转换做位移位前为int。

According to http://msdn.microsoft.com/en-us/library/a1sway8w.aspx byte does not have << defined on it for C# (only int, uint, long and ulong. This means that it will use an implciit conversion to a type that it can use so it converts it to int before doing the bit shift.

<一个href="http://msdn.microsoft.com/en-us/library/7haw1dex.aspx">http://msdn.microsoft.com/en-us/library/7haw1dex.aspx说VB定义上字节的操作。为prevent溢出应用于口罩,您转移到适当的范围内把它,所以它实际上是在这种情况下,通过什么都没有发生变化。

http://msdn.microsoft.com/en-us/library/7haw1dex.aspx says that VB defines the operation on Bytes. To prevent overflow it applies a mask to your shift to bring it within an appropriate range so it is actually in this case shifting by nothing at all.

至于为什么C#没有定义的字节偏移我不能告诉你。

As to why C# doesn't define shifting on bytes I can't tell you.

要真正使其行为是相同的,你需要的只是掩盖你的移位数目由7个字节或15短裤(见信息第二个链接)。

To actually make it behave the same for other datatypes you need to just mask your shift number by 7 for bytes or 15 for shorts (see second link for info).

这篇关于VB.NET和C#的二进制移位的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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