How to set a large bitwise constant in ULong?

This works well:

Public Const test As ULong = 1 << 30 

This does not work beautifully:

 Public Const test As ULong = 1 << 31 

He creates this error:

Constant expression not representable in type 'ULong'

How can I make it work?

It works:

 Public Const test As Long = 1 << 31 

But I have to use ULong .

+4
source share
2 answers

Try the following:

 Public Const test As ULong = 1UL << 31 

You need to explicitly tell the compiler that you are performing the operation on ULong .

The equivalent C # works:

 public const ulong test = 1UL << 31; 
+3
source

You cannot shift 1 << 31 with a long data type to get this error.

However, this is due to the fact that 1 , as an integer literal, is considered as Int32, which is the default integer literal.

You should get around this by specifying this as:

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

The UL flag says to make 1 unsigned long. See Typical Symbols for details .

+5
source

Source: https://habr.com/ru/post/1302765/


All Articles