代码之家  ›  专栏  ›  技术社区  ›  Fredou

如何在ulong上按位操作设置一个大常数?

  •  4
  • Fredou  · 技术社区  · 14 年前

    这很管用:

    Public Const test As ULong = 1 << 30
    

    这不太管用:

    Public Const test As ULong = 1 << 31
    

    它将创建此错误:

    “ulong”类型中不可表示的常量表达式

    我该怎么做?

    这确实有效:

    Public Const test As Long = 1 << 31
    

    但我必须用 乌龙

    2 回复  |  直到 9 年前
        1
  •  3
  •   Kelsey    14 年前

    请尝试以下操作:

    Public Const test As ULong = 1UL << 31 
    

    您需要显式地告诉编译器您正在对 ULong .

    C等效工程:

    public const ulong test  = 1UL << 31;
    
        2
  •  5
  •   Reed Copsey    14 年前

    你不能移动 1 << 31 有一个长数据类型,所以您会得到这个错误。

    然而,这是因为 1 ,作为整数文本,被视为Int32,这是默认的整数文本。

    您应该将其定义为:

    Public Const test As ULong = 1UL << 30
    Public Const test2 As ULong = 1UL << 31
    

    ul标志表示要使1成为无符号长。 See Type characters for details