Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set a large constant with bitwise operation on ULong?

This works nicely:

Public Const test As ULong = 1 << 30

This doesn't work nicely:

Public Const test As ULong = 1 << 31

It creates this error:

Constant expression not representable in type 'ULong'

How do I make it work?

This does work:

Public Const test As Long = 1 << 31

But I have to use ULong.

like image 960
Fredou Avatar asked Feb 28 '23 11:02

Fredou


2 Answers

You cannot shift 1 << 31 with a Long datatype, so you get this error.

However, this is because 1, as an integer literal, is treated as an Int32, which is the default integer literal.

You should work around this by defining this as:

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

The UL flag says to make the 1 an unsigned long. See Type characters for details.

like image 144
Reed Copsey Avatar answered Mar 04 '23 16:03

Reed Copsey


Try the following:

Public Const test As ULong = 1UL << 31 

You need to explicitly tell the compiler you are doing the operation on a ULong.

The C# equivalent works:

public const ulong test  = 1UL << 31;
like image 44
Kelsey Avatar answered Mar 04 '23 17:03

Kelsey